当前位置: 首页 > 知识库问答 >
问题:

Retrofit2上载带有Multipart或RequestBody的图像失败

葛炜
2023-03-14

但没有一个真正成功。

第一种方式通过@multipart:

interface Api {   
@Multipart
@POST("/upload-file")
fun uploadFile(@Part("file") file: RequestBody,
               @Part("data") id: FileUploadModel): Single<Response<AdvertisementResponse>>
}

和方法:

 override fun uploadFile(
    id: FileUploadModel,
    filePath: String
): Single<Response<AdvertisementResponse>> {

    val requestFile = RequestBody.create(MediaType.parse("image/*"), file)

    return api.uploadFile(requestFile, id)
}
 @POST("/upload-file")
 fun uploadFile(@Body requestBody: RequestBody): Single<Response<AdvertisementResponse>>
override fun uploadFile(
    id: FileUploadModel,
    filePath: String
): Single<Response<AdvertisementResponse>> {

    val file = File(filePath)
    val builder = MultipartBody.Builder()
    builder.setType(MultipartBody.FORM)

    val requestFile = RequestBody.create(MediaType.parse("image/*"), file)

    builder.addFormDataPart("file", file.name, requestFile)
    builder.addFormDataPart("data", id.toJson())

    return api.uploadFile(builder.build())
}
@Multipart
@POST("/upload-file")
fun uploadFile(@Part("file") file: MultipartBody.Part,
               @Part("data") id: FileUploadModel): Single<Response<AdvertisementResponse>>
 override fun uploadFile(
    id: FileUploadModel,
    filePath: String
): Single<Response<AdvertisementResponse>> {

    val file = File(filePath)
    val requestFile = RequestBody.create(MediaType.parse("image/*"), file)
    val multipartFile = MultipartBody.Part.createFormData("file", file.name, requestFile)

    return api.uploadFile(multipartFile, id)
}

2020-08-15 13:52:35.828 D/OKHttpInterceptor:-->POST http://####/advertence 2020-08-15 13:52:35.829 D/OKHttpInterceptor:Content-Type:Application/JSON 2020-08-15 13:52:35.829 D/OKHttpInterceptor:Content-Length:233 202 0-08-15 13:52:35.829 D/OKHttpInterceptor:{“businessname”:“MGSKHSGMHz”,“email”:“test@email.com”,“阿联酋”:“阿布扎比”,“Image”:“EB-4AE0-B642-BE2733CFEDBE969168674235591475.jpg”,“注意”:“”,“所有者名称”:“MHZM”,“电话”:“1111”,“网站”:“”}2020-08-15 13:52:35.829 D/OKHttpInterceptor:-->结束帖子(233字节正文)2020-08-15 13:52:36.233 D/OKHttpInterceptor:<--201创建http://#####/广告(403ms)2020-08-15 13:52:36.233 D/OKHttpInterceptor:x-power-by:Express 2020-power-by:Express 08-15 13:52:36.234 D/OKHttpInterceptor:Access-Control-Allow-Origin:*2020-08-15 13:52:36.234 D/OKHttpInterceptor:Content-Type:Application/JSON;Charset=UTF-8 2020-08-15 13:52:36.234 D/OKHttpInterceptor:Content-Length:303 202 0-08-15 13:52:36.235 D/OKHttpInterceptor:ETAG:W/“12F-HAPHR6BG9NZRHDMQ2EMPT2YPCAK”2020-08-15 13:52:36.235 D/OKHttpInterceptor:DATE:Sat,2020年8月15日10:52:37 GMT 2020-08-15 13:52:36.235 D/OKHttpInterceptor:Connection:keep-alive 2020-08-15 13:52:36.237数据“:{”_id“:”5f37be7552e7000012180324“,”商业名称“:”mgskhsgmhz“,”电子邮件“:”test@email.com“,”阿联酋“:”阿布扎比“,”图像“:”/storage/emulated/0/pictures/1768cbfe-e5eb-4ae0-b642-b2733cfedbe969168674235591475.jpg“,”注“:”“ownername”:“MHZM”,“电话”:“1111”,“网站”:“},”haserror“:false}2020-08-15 13:52:36.237 D/OKHttpInterceptor:<--END HTTP(303字节正文)2020-08-15 13:52:36.244 D/123:Sent:5F37BE7552E7000012180324 2020-08-15 13:52:36.278 D/OKHttpInterceptor:-->POST HTTP://####/Upload-File2020-08-15 13:52:36.278 D/OKHttpInterceptor:Content-Type:multipart/form-data;边界=894CBE1B-0FC4-47C6-849D-3764193C214F 2020-08-15 13:52:36.279D/OKHttpInterceptor:内容长度:104305

我想知道为什么改型不能完成这样的请求。

谢谢你的任何建议

干杯

共有1个答案

宋耀
2023-03-14

该问题是由于无法从磁盘读取文件造成的。

在Android sdk 29中,将Android:requestlegacyexternalstorage=“true”添加到Android清单中对我来说很有效。

<application
android:name=".MyApplication"
android:requestLegacyExternalStorage="true" ...

除了添加 ion之外,android:name=“android.permission.read_external_storage” />权限。

 类似资料:
  • 我的代码: 我使用map是因为@PartMap注释--我想上传多个文件。我的服务器返回http代码200,但没有上传任何文件。Api调用已经过测试--如果我们的web应用程序使用它,它可以正确工作。你知道我做错了什么吗

  • 清单权限 Android显示器输出

  • 我正在尝试上传图像与修改库。我就是这样上传的: 请求代码: 答复: 如果我是通过客户端浏览器(如postman或DHC)发布的,则请求与上面相同,并且我得到一个成功的XML响应。 请看我在邮递员客户端上尝试的截图。它是成功的。

  • 我想使用Android数据绑定设置图像。我已经阅读了很多关于这方面的教程,但图片仍然没有出现。 在我的模型中,我有以下方法: 我还应该提到“III”、“Image-”url永远不会打印,所以问题不在于Glide。 这是我的xml: maleIcon是一个url字符串

  • 我试图使用AlamoFire将图像(avatarImage)作为formData参数类型上载到服务器,但每次我尝试发布时都会生成一个“数据无法读取,因为它的格式不正确”错误。我不太清楚我做错了什么。 PS:我正在使用multipartFormData上载b/c,我还需要发送一些其他参数(一旦我能够上载图像)。

  • 我想在ImageView中将图像居中。ImageViews用于列表中图片的缩略图,我使用了宽度和高度为90的固定视口,使它看起来更干净。这些图片使用背景加载,所以我不知道图片放入ImageView时的尺寸。当然,我不想拉伸(或裁剪)图像。我在加载图像时使用preserveRatio。有什么方法可以让图像居中,而不是左上角?图像的最长边总是90度。