改造2 RequestBody内容长度大于文件大小 [英] Retrofit 2 RequestBody Content Length Greater Than File Size

查看:314
本文介绍了改造2 RequestBody内容长度大于文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Api,要求我先发送一个文件大小(以字节为单位),然后再发送实际文件.当我在文件上调用file.length()以在第一个Api调用中发送时,它将返回1996.

I have an Api which requires me to send up the size of a file in bytes before I then send up the actual file. When I call file.length() on the file to send up in the first Api call, it returns 1996.

当我将文件打包成RequestBody时,contentLength()变为2556.然后,服务器拒绝此操作,并说这些大小必须匹配.

When I then package the file up into a RequestBody the contentLength() becomes 2556. The server then rejects this, saying that these sizes must match.

这是创建RequestBody的代码.

val requestBody = RequestBody.create(
                        MediaType.parse("image/jpeg"),
                        avatarFile)

  • RequestBody中添加了哪些内容以增加其内容长度?
  • 我应该在第一次通话时发送RequestBody的大小来解决此问题吗?
    • What is being added in the RequestBody to increase its content length?
    • Should I just send up the size of the RequestBody on the first call to get around this problem?
    • 编辑

      在这里,我第一次调用该API以最初发送尺寸:

      Here is where I call the API the first time to send the size initially:

      return authService.updateAvatar(
                          AvatarMetadataRequest(
                                  size = avatarFile.length().toInt(),
                                  crc = profileImageProvider.getFile()!!.checksum()!!.toInt()))
      

      然后是我第二次实际上传文件的地方:

      And then this is where I call it the second time when the file actually gets uploaded:

              val avatarFile = profileImageProvider.getFile()
      
              val requestBody = RequestBody.create(
                      MediaType.parse("image/jpeg"),
                      avatarFile)
              return authService.uploadAvatar(
                      id.split("/").last(),
                      MultipartBody.Part.createFormData("avatar",
                              profileImageProvider.getFileName(),
                              requestBody))
      

      事实证明requestBodycontentLength()是正确的.因此,调用MultipartBody.Part.createFormData时必须修改或增加Content-Length.

      It turns out the contentLength() of the requestBody is correct. So the Content-Length must be being modified or increased when the MultipartBody.Part.createFormData is called.

      推荐答案

      在头像上传API方面,这似乎是一个错误.该头像将作为MultipartData上传,因此永远不会等于该文件的原始大小.这是因为多部分请求除了文件外还将有其他数据.

      This seems to be a mistake on the part of the avatar upload API. The avatar is being uploaded as MultipartData and so will never equal the raw size of the file. This is because a multipart request will have additional data besides the file.

      这是一个示例多部分请求:

      Here is an example multipart request:

      --------------------------0b880724ca8aacd6
      Content-Disposition: form-data; name="file1"; filename="test.txt"
      Content-Type: text/plain
      
      test test test
      
      --------------------------0b880724ca8aacd6--
      

      如您所见,文件的内容仅为test test test.其他所有内容都是由多部分请求添加的其他数据.

      As you can see, the content of the file is just test test test. Everything else is additional data added by the multipart request.

      由于所有额外数据,磁盘上的原始大小为15个字节,而请求的Content-Length为202个.

      The raw size on disk is 15 bytes, while the Content-Length of the request is 202, due to all the extra data.

      因此,头像上传API不仅应检查整个请求的Content-Length,还应提取与文件有关的特定部分,并仅检查该部分中包含的数据大小.

      So the avatar upload API should not just be checking the Content-Length of the entire request, but rather extracting the specific part relating to the file and just checking the size of the data contained in that part.

      如果您无法控制头像上传API的行为,则可以通过计算分段请求添加的总开销大小并将其添加到文件大小中来尝试计算总Content-Length.

      If you have no control over the behaviour of the avatar upload API, then you can attempt to calculate the total Content-Length by figuring out the total size of the overhead added by the multipart request and adding that to your file size.

      这篇关于改造2 RequestBody内容长度大于文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆