Retrofit okHttp 上传图片总是不行

这是Retrofit接口代码

https://img3.mukewang.com/5cbd2f44000194c406830092.jpg

Retrofit 调用代码
https://img1.mukewang.com/5cbd2f460001047106970239.jpg

Retrofit怎么弄都不行 不能成功,但是我用 ( useLibrary 'org.apache.http.legacy')的httpclient可以上传,以下是调用方法:

https://img1.mukewang.com/5cbd2f4c0001654607080348.jpg

这是php服务器端接收图片代码

https://img1.mukewang.com/5cbd2f4e0001c96504590443.jpg


呼如林
浏览 1069回答 5
5回答

明月笑刀无情

我是这样写的 @Part("image\"; filename=\"image.png\" ") RequestBody image

米琪卡哇伊

&nbsp; Map<String, RequestBody> photoRequestBodyMap = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; photoRequestBodyMap.put("Content", RequestBody.create(MediaType.parse("text/plain"), et_body.getText().toString()));&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < picList.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = new File(picList.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String key = "image\";filename=\"" + file.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photoRequestBodyMap.put(key, requestBody);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; netApiInterface&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .postMoment(photoRequestBodyMap)我是这样的。关于图片上传时要用的key,网上有多种说法。我这边,实测 String key = "image";filename="" + file.getName();可行

慕的地10843

方法注解只使用POST,参数注解也只使用BODY,以及单独构造MultipartBody进行上传。retrofit2.0对上传文件支持并不那么简洁,应该去熟悉okhttp

慕标琳琳

如果你用的是Retrofit2.0 那 Part注解后面 后面应该是MultipartBody.Part 而不是RequestBody
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java