by Guest » 13 Jan 2025, 11:36
Ich habe eine ZIP-Datei von einem Anbieter heruntergeladen, der Download funktioniert gut und dann muss ich die ZIP-Datei verarbeiten. Die Struktur der ZIP-Datei ist wie folgt:
Aber wenn ich versuche, die Datei zu verarbeiten, wird in meinen Protokollen dieser Fehler angezeigt:
Code: Select all
java.io.IOException: Attempted read on closed stream
Und ich verstehe nicht, was los ist, hier ist der Code, der sich mit all dem befasst:
Code: Select all
private fun downloadDataToDisk(
source: FileSource,
name: String,
fileType: FileType
): Either {
val filePath =
prepareDirectory().flatMap { _ ->
val fullFill =
when (fileType) {
FileType.DAY -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
FileType.FULL -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
}
fullFill.right()
}
val downloadedFile = filePath.flatMap { path ->
externalClient.loadDowJonesFile(path).mapLeft{ mapClientError(it) }
}
val storedFileStatus = downloadedFile.flatMap { inputStream ->
filePath.flatMap { path ->
inputStream.use { storeFile(path, name, it) }
}
}
return storedFileStatus.flatMap { filePath }
}
Code: Select all
private fun prepareDirectory(): Either {
return Either.conditionally(
File(properties.fileStoragePath).exists(),
{ directoryMissingError() },
{ properties.fileStoragePath }
)
}
Code: Select all
private fun storeFile(
path: String,
fileName: String,
input: InputStream
): Either {
val filePath =
Either.catch {
val file = File(path, fileName)
if (!file.exists()) {
file.createNewFile()
file.outputStream().use { target -> input.copyTo(target) }
}
file.path
}
.mapLeft { fileStorageError(fileName, it) }
.tap { log.atInfo().log("Successfully downloaded file $fileName") }
return filePath
}
```
Ich habe eine ZIP-Datei von einem Anbieter heruntergeladen, der Download funktioniert gut und dann muss ich die ZIP-Datei verarbeiten. Die Struktur der ZIP-Datei ist wie folgt:
[code]zipFile.zip/MainFolder/MyFile.xml[/code]
Aber wenn ich versuche, die Datei zu verarbeiten, wird in meinen Protokollen dieser Fehler angezeigt:
[code]java.io.IOException: Attempted read on closed stream[/code]
Und ich verstehe nicht, was los ist, hier ist der Code, der sich mit all dem befasst:
[code]private fun downloadDataToDisk(
source: FileSource,
name: String,
fileType: FileType
): Either {
val filePath =
prepareDirectory().flatMap { _ ->
val fullFill =
when (fileType) {
FileType.DAY -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
FileType.FULL -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
}
fullFill.right()
}
val downloadedFile = filePath.flatMap { path ->
externalClient.loadDowJonesFile(path).mapLeft{ mapClientError(it) }
}
val storedFileStatus = downloadedFile.flatMap { inputStream ->
filePath.flatMap { path ->
inputStream.use { storeFile(path, name, it) }
}
}
return storedFileStatus.flatMap { filePath }
}
[/code]
[code]private fun prepareDirectory(): Either {
return Either.conditionally(
File(properties.fileStoragePath).exists(),
{ directoryMissingError() },
{ properties.fileStoragePath }
)
}
[/code]
[code]private fun storeFile(
path: String,
fileName: String,
input: InputStream
): Either {
val filePath =
Either.catch {
val file = File(path, fileName)
if (!file.exists()) {
file.createNewFile()
file.outputStream().use { target -> input.copyTo(target) }
}
file.path
}
.mapLeft { fileStorageError(fileName, it) }
.tap { log.atInfo().log("Successfully downloaded file $fileName") }
return filePath
}
```
[/code]