Controller:
@RequestMapping(value = "/upload")
@ResponseBody
public String uploadKxf(MultipartFile file) {
LOGGER.debug("uploadKxf:" + file.getSize());
try {
File tmp = new File("d:/logs/", file.getOriginalFilename());
file.transferTo(tmp);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
return Result.ok("sss");
}
activity:
private void uploadImg() { File file = new File("storage/emulated/0/DCIM/Camera/IMG20160822143525.jpg"); OkHttpClient okHttpClient = OkHttpUtils.getOkHttpClient(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.1.3:8080/") .addConverterFactory(new ToStringConverterFactory()).client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) //Gson .build(); OtherServierApi request = retrofit.create(OtherServierApi.class); // create RequestBody instance from file RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); Call<String> call= request.upImg(body); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.e("log","response:"+ response.body() ); } @Override public void onFailure(Call<String> call, Throwable t) { Log.e("log","Throwable:"+ t.getMessage() ); } }); }
OtherServierApi:
@Multipart @POST("merchant/upload.do") Call<String> upImg(@Part MultipartBody.Part file);