Android Multipart HTTP Post does not send the File's MIME type

后端 未结 6 1267
挽巷
挽巷 2021-02-05 21:53

Trying to figure what\'s wrong with my codings. I followed a blog post from here.

I managed to get the codes to actually upload the file to a PHP web service. However, f

6条回答
  •  無奈伤痛
    2021-02-05 21:55

    I just had the same issue when updating HttpClient from version 4.1 to version 4.4. Neither removing HttpMultipartMode.BROWSER_COMPATIBLE nor replacing with other alternative options did help.

    A solution that requires the minimum change for me is to explicitly specify the filename when constructing a FileBody, i.e. replacing

    new FileBody(file, mimeType)
    

    with

    new FileBody(file, filename, mimeType, charset)
    

    As for version HttpClient 4.4, the reason might be that new FileBody(file, mimeType) assigns null to the filename field rather than uses file.getName(). Then, when executing formatMultipartHeader in HttpMultipart, the filename of the part that is going to be formatted is checked if not null in BROWSER_COMPATIBLE mode. If null, then the whole body is ignored.

提交回复
热议问题