80,351
社区成员
发帖
与我相关
我的任务
分享
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody description,
@Part MultipartBody.Part file)
@Multipart
@POST("/rest/services/file")
Call<BaseResponse<String>> uploadFileWithRequestBody(@Part("description") RequestBody description,
@Part MultipartBody.Part file);
File file = new File("/storage/emulated/0/Android/data/com.smart.teach/files/Thumbnail/20171108151210.jpg");
String path ="/image/desk/product";
Gson gson = new Gson();
JsonObject jObj = new JsonObject();
jObj.addProperty("name","20171108105505.jpg");
jObj.addProperty("path",path);
JsonArray jsonArray = new JsonArray();
jsonArray.add(jObj);
RequestBody bodyjson = RequestBody.create(ApiHelper.mediaType, gson.toJson(jsonArray));
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/jpg"), file);
MultipartBody.Part body =
MultipartBody.Part.createFormData("file", file.getName(), requestFile);
Call<BaseResponse<String>> baseResponseCall =
ApiHelper.getInstance().getApiService().uploadFileWithRequestBody(bodyjson, body);
baseResponseCall.enqueue(new Callback<BaseResponse<String>>() {
@Override
public void onResponse(Call<BaseResponse<String>> call, Response<BaseResponse<String>> response) {
Log.i(TAG,"上传成功");
}
@Override
public void onFailure(Call<BaseResponse<String>> call, Throwable t) {
Log.e(TAG,"上传失败");
}
});