Я скачал zip-файл от провайдера, загрузка проходит нормально, затем мне нужно обработать zip-файл, структура zip-файла следующая:
zipFile.zip/MainFolder/MyFile.xml
Но когда я пытаюсь обработать файл, в моих журналах появляется эта ошибка:
java.io.IOException: Attempted read on closed stream
И я не понимаю, что не так, вот код, который всем этим занимается:
private fun downloadDataToDisk(
source: FileData,
name: String,
fileType: FileType
): Either {
val storingPath = prepareDirectory()
val fileCompleteEndpoint =
storingPath.flatMap { _ ->
val fileCompletedName =
when (fileType) {
FileType.DAY -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
FileType.FULL -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
}
fileCompletedName.right()
}
val downloadedFile =
fileCompleteEndpoint.flatMap { path ->
externalClient.downloadFile(path).mapLeft { mapClientError(it) }
}
val storedFileStatus =
downloadedFile.flatMap { inputStream ->
val storedFilePath = storingPath.flatMap { path -> storeFile(path, name, inputStream) }
storedFilePath
}
return storedFileStatus.flatMap { storedFilePath ->
storedFilePath.right()
}
}
private fun prepareDirectory(): Either {
return Either.conditionally(
File(properties.fileStoragePath).exists(),
{ directoryMissingError() },
{ properties.fileStoragePath }
)
}
private fun storeFile(
path: String,
fileName: String,
input: InputStream
): Either {
val filePath =
Either.catch {
val file = File(path, fileName)
if (!file.exists()) {
file.createNewFile()
input.use { stream -> file.outputStream().use { target -> stream.copyTo(target) } }
}
file.path
}
.mapLeft { fileStorageError(fileName, it) }
.tap { log.atInfo().log("Successfully extracted and stored dow jones file $fileName") }
return filePath
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... sed-stream
Исключение ввода-вывода попыталось прочитать в закрытом потоке ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Операция ввода-вывода в закрытом файле при потоковой передаче ответа от Fastapi
Anonymous » » в форуме Python - 0 Ответы
- 12 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Операция ввода-вывода в закрытом файле при потоковой передаче ответа от FastAPI
Anonymous » » в форуме Python - 0 Ответы
- 9 Просмотры
-
Последнее сообщение Anonymous
-