我正在尝试使用http PUT方法上传文件,
我成功使用OkHttp尝试,但使用Retrofit失败。
这是我的代码: 静态最终字符串MEDIA_TYPE =“ image / jpeg”
// file is a file path
RequestBody requestBody = getRequestBody(file);
OkHttpClient client = getOkHttpClient();
Request request = new Request.Builder()
.url(url)
.put(requestBody)
.build();
okhttp3.Response response = response = client.newCall(request).execute();
private RequestBody getRequestBody(String filePath) {
MediaType contentType = MediaType.parse(MEDIA_TYPE);
return RequestBody.create(contentType, new File(filePath));
}
上面的代码成功。
什么是翻新?
我尝试并失败了: 公共接口UploadFileService { 字符串CONTENT_TYPE =“ image / jpeg”;
/** base url, just for Retrofit usage demand. */
String BASE_URL = "https://not.used.net/";
/** Must be consistent with the following uploadFile annotation. */
String UPLOAD_FILE_HTTP_METHOD = "PUT";
@Multipart
@Headers("Content-Type:" + CONTENT_TYPE)
@PUT()
Observable<Response<MinaResponse<LocalAlbum.DummyResponse>>> uploadFile(@Url String url, @Part RequestBody fileBody);
}
答案 0 :(得分:0)
删除Multipart批注并将Body应用于fileBody, 我成功了!
@Headers("Content-Type:" + CONTENT_TYPE)
@PUT()
Observable<Response<MinaResponse<LocalAlbum.DummyResponse>>> uploadFile(@Url String url, @Body RequestBody fileBody);