1

I am currently trying to upload a file from an android client using retrofit2 to a server using Spring Boot and its REST api.

CLIENT

I specifiy the upload method as described here: https://github.com/square/retrofit/issues/1063

public interface RetroRespondService {

    @Multipart
    @POST("/v1/answers")
    public Call<ResponseDTO> sendPictures(@Part("file\"; filename=\"image.png")RequestBody image);
}

In another class the method to provide the actual image is declared: (Now its just a test scenario. When image uploading is actually accomplished it will get more sophisticated.)

public void performAnswerRequest() {

    try {
        if (mRetrofit == null) {
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(DataHolder.getHostName())
                    .build();
        }

        //load test image
        AssetManager manager = getAssets();
        File file = new File(getFilesDir(), "image.png");
        Utility.writeBytesToFile(new BufferedInputStream(manager.open("heart.png")), file);

        RetroRespondService requestService = mRetrofit.create(RetroRespondService.class);
        RequestBody image= RequestBody.create(MediaType.parse("multipart/form-data"), file);
        Call<ResponseDTO> response = requestService.sendPictures(image);
        response.enqueue(new AsyncAnswerResponse());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

SERVER

What I actually do not know is, how to properly get the image on the spring side.

@RequestMapping(value = API_VERSION + "/answers", method = RequestMethod.POST)
@ResponseBody
ResponseEntity<ResponseDTO> addAnswers(@RequestParam("file\"; filename=\"image.png") MultipartFile answers) throws DBEntryDoesNotExistException, EvaluationException, ParticipantException {
   // In fact I have set a brake point here. Never entered the method yet, though
   System.out.println("Yay!")
   return null;
}

ERROR

Request: localhost:8080/v1/answers raised org.springframework.web.bind.MissingServletRequestParameterException:
Required MultipartFile parameter 'file"; filename="image.png' is not present

Since wireshark reports that in fact a request of size 1894 Bytes was send and this is the size of the image i want to upload I strongly believe the the data is actually transmitted but cannot be decoded from the server.

I have also seen this answers: How to config "CommonsMultipartResolver" in spring4 without xml to upload file

and subsequently implemented this class on the server side:

@Configuration
public class MultipartConfiguration {

    @Bean
    public CommonsMultipartResolver multipartResolver() {
       CommonsMultipartResolver resolver=new CommonsMultipartResolver();
       resolver.setDefaultEncoding("utf-8");
       resolver.setMaxUploadSize(1048576);
       return resolver;
    }
}

If you have any pointers in how to solve this I would appreciate your answer tremendously :) If there are any questions left unanswered feel free to ask away.

Btw.: Sending and receiving JSON encoded data works just fine in both directions.

Community
  • 1
  • 1
zetain
  • 361
  • 4
  • 15

0 Answers0