android 为什么客户端MultiPartFormDataContent的文件上传未完成?

von4xj4u  于 2023-02-06  发布在  Android
关注(0)|答案(1)|浏览(113)

我将一个文件分块到byteArray,并希望将每个块上传到ktor客户端MultiPartFormDataContent的单独请求,以上传一些块文件,但在阅读ByteArray后,文件未上传。

以下是我的请求:

override suspend fun uploadChunks(
        token: String,
        file: ByteArray,
        fileId: Long,
        chunkIndex: String,
        fileType: Int,
        fileName: String,
    ): NetworkUploadChunks? = try {
        
        KtorModule.provideKtor.use {
            it.post(".../uploadChunks") {
                headers {
                    bearerAuth(token)
                }
                setBody(MultiPartFormDataContent(
                    formData {
                        append("FileId", fileId)
                        append("FileChunkId", chunkIndex)
                        append("FileType", fileType)
                        append(
                            "FileChunk", file,
                            Headers.build {
                                append(
                                    HttpHeaders.ContentType, when (fileType) {
                                        0 -> ContentType.Video.MP4
                                        else -> {
                                            ContentType.Text.CSV
                                        }
                                    }
                                )
                                append(
                                    HttpHeaders.ContentDisposition,
                                    "filename=\"${fileName}\""
                                )
                            },
                        )
                    },
                    boundary = "WebAppBoundary"
                ))

                onUpload { bytesSentTotal, contentLength ->
                    println("Sent $bytesSentTotal bytes from $contentLength")
                }

            }.body()
        }
    } catch (cause: ClientRequestException) {
        Log.d("clientRequestException:", cause.message)
        null
    } catch (cause: ProtocolException) {
        Log.d("ProtocolException:", cause.message.toString())
        null
    } catch (cause: CancellationException) {
        Log.d("CancellationException:", cause.message.toString())
        null
    }

它是用于上载状态的logcat:

2023-01-08 13:55:46.256 18174-18376/de.artifici... V/Logger Ktor =>: REQUEST: http://gizo.ml/api/v1/Trip/uploadChunks
    METHOD: HttpMethod(value=POST)
    COMMON HEADERS
    -> Accept: application/json; application/json
    -> Accept-Charset: UTF-8
    -> Authorization: Bearer eyJhbGciOiJ...
    CONTENT HEADERS
    -> Content-Length: 1299875
    -> Content-Type: multipart/form-data; boundary=WebAppBoundary
    BODY Content-Type: multipart/form-data; boundary=WebAppBoundary
    BODY START
    [request body omitted]
    BODY END
2023-01-08 14:00:08.746 18174-18255/de.artificient.gizo V/Logger Ktor =>: REQUEST http://....ml/api/v1/Trip/uploadChunks failed with exception: java.io.IOException: unexpected end of stream on http://...ml/...

正如您所看到的,所有字节数组值都被读取,但没有上载

w6lpcovy

w6lpcovy1#

在研究问题后,我找到了解决方案,也许会有帮助。
将LogLevel从“全部”更改为其他类型(信息、正文、标题或无)

HttpClient(engine) {

    if (configuration.isHttpLoggingEnabled) {
        install(Logging) {
            logger = Logger.SIMPLE
            level = LogLevel.INFO // changed from LogLevel.ALL
        }
    }
 }

相关问题