4

我正在尝试上传多个文件。

val ktorVersion = "1.5.0"
val serializationVersion = "1.0.1"

我就是这样做的:

  override suspend fun uploadFiles(
    binaryFiles: Map<String,ByteArray>
  ): BaseResponse<List<String>> {
    return client.submitForm {

      url(fileUploadUrl)
      method = HttpMethod.Post
      body = MultiPartFormDataContent(
        formData {
          headers{
            append("Content-Type", "application/json")
            append("Authorization", "Bearer $token}")
          }
          binaryFiles.entries.forEach {
            append(
              key = "files",
              value = it.value,
              headers = Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${it.key}")
              }
            )
          }
        }
      )
    }
  }

但它抛出异常

kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
    at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
    at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:130)
    at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
    at io.ktor.client.features.json.serializer.KotlinxSerializerKt.buildSerializer(KotlinxSerializer.kt:82)
    at io.ktor.client.features.json.serializer.KotlinxSerializerKt.access$buildSerializer(KotlinxSerializer.kt:1)
    at io.ktor.client.features.json.serializer.KotlinxSerializer.writeContent$ktor_client_serialization(KotlinxSe
    at io.ktor.client.features.json.serializer.KotlinxSerializer.write(KotlinxSerializer.kt:26)
    at io.ktor.client.features.json.JsonFeature$Feature$install$1.invokeSuspend(JsonFeature.kt:150)
    at io.ktor.client.features.json.JsonFeature$Feature$install$1.invoke(JsonFeature.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:123)
    at io.ktor.client.features.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:106)
    at io.ktor.client.features.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invokeSuspend(HttpRequestLifecycle.kt:37)
    at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:79)
    at io.ktor.client.HttpClient.execute(HttpClient.kt:187)
    at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:104)
    at com.example.package.core.data.network.services.upload.FileUploadApiImpl.uploadFiles(FileUploadApi.kt:99)

我也尝试过这种方式,但遇到了同样的问题:

  suspend fun uploadFilesTest(
    binaryFiles: Map<String,ByteArray>
  ): BaseResponse<List<String>> {
    return client.post(fileUploadUrl) {
      headers {
        append("Content-Type", ContentType.Application.Json)
        append("Authorization", "Bearer $token")
      }
      body = MultiPartFormDataContent(
        formData {
          binaryFiles.entries.forEach {
            this.appendInput(
              key = "files",
              size = it.value.size.toLong(),
              headers = Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${it.key}")
              },
            ){ buildPacket { writeFully(it.value) }}
          }
        }
      )
    }
  }
4

2 回答 2

4

查看您说您遇到相同问题的最后一个片段,您似乎正在像这样在标题中指定内容类型

append("Content-Type", ContentType.Application.Json)

但是您还希望它是多部分的,方法是将 body 设置为MultiPartFormDataContent也需要定义为标头content-type:multipart/form-data; ...,因此可能是这种冲突导致了问题。

实际上我只是在我的代码中自己尝试过,这对我来说似乎是个问题。

于 2021-02-09T15:00:06.583 回答
0

我为 Android 做了这个,但它也可能适用于 ktor。首先,将此行添加到构建项目 gradle 中的依赖项中:

classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.21"

还将这些添加到构建应用程序 gradle 中:

plugins {
    ...
    id 'kotlinx-serialization'
}
dependencies {
    ...
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2'
}
于 2021-08-29T04:13:20.897 回答