Failed to upload image from Android to java server

I am trying to implement a photo upload function using Android Retrofit + SpringMVC. The Java server cannot answer the Retrofit API call. The following code snippet is shown below:

ApiInterface

@Multipart @POST("user/profileImage") Call<ResponseBody> uploadImage(@Part MultipartBody.Part image, @Part("name") RequestBody name); 

uploadToServer

 public void uploadToServer(){ //Get retrofit client Retrofit retrofit = ApiClient.getClient(); //Get API interface ApiInterface apiInterface = retrofit.create(ApiInterface.class); // Get image parts MultipartBody.Part imageParts = bitmapToMultipart(imageBitmap); //Get image name RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "ProfileImage"); //Call image upload API Call<ResponseBody> call = apiInterface.uploadImage(imageParts,name); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { ResponseBody body = response.body(); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { t.printStackTrace(); } }); } 

bitmapToMultipart

 public MultipartBody.Part bitmapToMultipart(Bitmap imageBitmap){ File file = null; try { //create a file to write bitmap data file = new File(this.getCacheDir(), "imageBitmap"); file.createNewFile(); //Convert bitmap to byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); //write the bytes in file FileOutputStream fos = new FileOutputStream(file); fos.write(bitmapdata); fos.flush(); fos.close(); }catch(IOException e){ e.printStackTrace(); } RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile); return body; } 

SpringMVC Java Controller

 @Controller @RequestMapping("/user") public class UserController{ @RequestMapping(value = "/profileImage", method = RequestMethod.POST) public @ResponseBody String imageUploader(@RequestParam("image") MultipartFile image, @RequestBody RequestBody name)throws Exception{ return ""; } } 

The problem is this: The request does not reach the java server.

+5
source share
4 answers

In your uploadToServer () function, the media type should be "multipart / form-data" instead of "text / plain" for the field name ...

 //Get image name RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), "ProfileImage"); 

In your form bitmapToMultipart (), the media type should be "multipart / form-data". ("image / *" should also work, but if not "multipart / form-data" will definitely work)

refer - How to upload an image file in Upgrade 2

And in your spring controller you should use @RequestParam instead of @Requestbody

 @Controller @RequestMapping("/user") public class UserController{ @RequestMapping(value = "/profileImage", method = RequestMethod.POST) public @ResponseBody String imageUploader(@RequestParam("image") MultipartFile image, @RequestParam String name)throws Exception{ return ""; } } 
+4
source

Change

 RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file); 

to

 RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 

those. bitmapToMultipart function should be such

 public MultipartBody.Part bitmapToMultipart(Bitmap imageBitmap){ File file = null; try { //create a file to write bitmap data file = new File(this.getCacheDir(), "imageBitmap"); file.createNewFile(); //Convert bitmap to byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); //write the bytes in file FileOutputStream fos = new FileOutputStream(file); fos.write(bitmapdata); fos.flush(); fos.close(); }catch(IOException e){ e.printStackTrace(); } RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile); return body; } 
0
source

ApiInterface

 @Multipart @POST("user/profileImage") Call<ResponseBody> uploadImage(@Part("image") MultipartBody.Part image, @Part("name") RequestBody name); 

uploadToServer

 public void uploadToServer(){ //Get retrofit client Retrofit retrofit = ApiClient.getClient(); //Get API interface ApiInterface apiInterface = retrofit.create(ApiInterface.class); // Get image parts MultipartBody.Part imageParts = bitmapToMultipart(imageBitmap); //Get image name RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "ProfileImage"); //Call image upload API Call<ResponseBody> call = apiInterface.uploadImage(imageParts,name); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { ResponseBody body = response.body(); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { t.printStackTrace(); } }); } 

bitmapToMultipart

 public MultipartBody.Part bitmapToMultipart(Bitmap imageBitmap){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), bitmapdata); MultipartBody.Part body = MultipartBody.Part.createFormData("upload", "name", reqFile); return body; } 

SpringMVC Java Controller

 @Controller @RequestMapping("/user") public class UserController{ @RequestMapping(value = "/profileImage", method = RequestMethod.POST) public @ResponseBody String imageUploader(@RequestParam("image") MultipartFile image, @RequestParam String name)throws Exception{ return ""; } } 
0
source
  • you must first check the android client download file in order. ru: use compression quality 80

    imageBitmap.compress (Bitmap.CompressFormat.JPEG, 80, bos);

  • change MediaType and debug requestBody client has data
  • Debug server check data request
0
source

Source: https://habr.com/ru/post/1273625/


All Articles