有没有人能在下面的代码中帮助处理不恰当的阻塞方法调用?特别是这里显示的几行警告。
Files.createDirectories(targetLocation.parent)
val fileStream = FileOutputStream(targetLocation.toString())
try {
workbook.write(fileStream)
} catch (ex: Exception) {
throw FileStorageException("Could not store file $cleanPath. Please try again!", ex)
} finally {
workbook.close()
fileStream.close()
}
完整代码在这里
interface LogsExcelService {
suspend fun storeContentLogsToExcelFile(contentLogs: Collection<ContentLog>, filePath:String):String}
@Service
internal class LogsExcelServiceImpl(
private val location: Path
) : LogsExcelService {
override suspend fun storeContentLogsToExcelFile(contentLogs: Collection<ContentLog>, filePath:
String): String {
val generalInfoRows = arrayOf(
"COMPANY NAME",
"PARTNER NAME",
"BRAND NAME",
"PERIOD",
"AMOUNT OF DAYS",
"ADVERTISING SPACE",
"QUANTITY OF SIDES",
"ADVERTISING FORMAT"
)
val boardColumns = arrayOf(
"BOARD ID",
"BOARD NAME",
"SHOWN X TIMES",
"AMOUNT OF CARS"
)
val logColumns = arrayOf(
"",
"CONTENT ID",
"CONTENT NAME",
"STARTED AT",
"ENDED AT",
"VARIANT",
"BOARD ID",
"BOARD NAME",
"BLOCK ID",
"BLOCK NAME",
"BOARD ADDRESS"
)
val workbook: Workbook = XSSFWorkbook()
val createHelper = workbook.creationHelper
val sheet: Sheet = workbook.createSheet("Result")
val headerFont: Font = workbook.createFont()
headerFont.bold = true
headerFont.color = IndexedColors.ROYAL_BLUE.getIndex()
val headerCellStyle = workbook.createCellStyle()
headerCellStyle.setFont(headerFont)
val generalHeaderColumn: Row = sheet.createRow(0)
for(col in generalInfoRows.indices){
val generalInfoCell = generalHeaderColumn.createCell(col)
generalInfoCell.setCellValue(generalInfoRows[col])
generalInfoCell.cellStyle = headerCellStyle
}
val boardHeaderRow: Row = sheet.createRow(12)
for (col in boardColumns.indices){
val boardCell = boardHeaderRow.createCell(col)
boardCell.setCellValue(boardColumns[col])
boardCell.cellStyle = headerCellStyle
}
val logHeaderRow: Row = sheet.createRow(27)
for (col in logColumns.indices) {
val cell = logHeaderRow.createCell(col)
cell.setCellValue(logColumns[col])
cell.cellStyle = headerCellStyle
}
val dataCellStyle = workbook.createCellStyle()
dataCellStyle.dataFormat = createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss")
var rowIdx = 28
for (log in contentLogs) {
val row = sheet.createRow(rowIdx++)
row.createCell(1).setCellValue(log.id)
row.createCell(2).setCellValue(log.content.name)
val startedAt = row.createCell(3)
val endedAt = row.createCell(4)
row.createCell(5).setCellValue(log.variantOrder)
row.createCell(6).setCellValue(log.board.id)
row.createCell(7).setCellValue(log.board.name)
val block = getBlock(log.board, log.content)
row.createCell(8).setCellValue(block.id)
row.createCell(9).setCellValue(block.name)
row.createCell(10).setCellValue(log.board.description)
startedAt.setCellValue(log.startedAt)
startedAt.cellStyle = dataCellStyle
endedAt.setCellValue(log.endedAt)
endedAt.cellStyle = dataCellStyle
}
val cleanPath: String = StringUtils.cleanPath(filePath)
withContext(Dispatchers.IO) {
val targetLocation = location.resolve(cleanPath)
Files.createDirectories(targetLocation.parent)
val fileStream = FileOutputStream(targetLocation.toString())
try {
workbook.write(fileStream)
} catch (ex: Exception) {
throw FileStorageException("Could not store file $cleanPath. Please try again!", ex)
} finally {
workbook.close()
fileStream.close()
}
}
return "files/$filePath"
}
private fun getBlock(board: BoardAggregate, content: ContentAggregate): BoardBlock {
val label = content.labels.firstOrNull() ?: throw LabelNotFoundException()
return board.blocks.firstOrNull { block ->
block.labels.map { it.id }.contains(label.id)
} ?: throw BlockNotFoundException()
}
}
暂无答案!
目前还没有任何答案,快来回答吧!