0

I try to upload an image to a server using the postman. I am using spring to make the rest api as the followings:

PostMan mutlipart request

  @RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
  public  ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
        @RequestBody  @RequestParam("image") MultipartFile image)
        throws  IOException {

But it throws an error:

      org.springframework.web.bind.MissingServletRequestParameterException: 
      Required MultipartFile parameter 'image' is not present

As you can see in the postman that key name is 'image' and in rest api is also @RequestParam("image").

Setting value in content type - Content-type = multipart/form-data,boundaries='--abc'

This is my spring config for multipart -

  @Bean
  public CommonsMultipartResolver multipartResolver() {

  CommonsMultipartResolver commonsMultipartResolver = new      CommonsMultipartResolver();
//commonsMultipartResolver.setMaxUploadSize(-1);
return commonsMultipartResolver;

}

What could be the problem?

Ashwani Tiwari
  • 1,497
  • 18
  • 28

5 Answers5

3

Please remove the header section

enter image description here

Please remove the - Content-Type : multipart/form-data;boundary='abc' setting in the header part of postman

Ashwani Tiwari
  • 1,497
  • 18
  • 28
2
@RestController
public class UserOfferController {

// upload image 
    @RequestMapping(value = "/uploadimage", method = RequestMethod.POST)
    public ResponseEntity<ResponseObjectBean> uploadFile(@RequestParam("uploadedFile") MultipartFile file) {
        int statusCode;
        String msg;
        Object data = null;
        long maxsize = configuredValue.getFileMaxAcceptedSize();

        if (!file.isEmpty()) {

                String name = file.getOriginalFilename();

                String imagePath = "path to save your image ";


                try {
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(imagePath)));
                    stream.write(bytes);
                    statusCode = 200;
                    msg = "DONE";
                    data = true;

                }  catch (Exception e) {
                    e.printStackTrace();
                    statusCode = 500;
                    msg = "FAIL";
                    data = false;

                }

        } else {
            statusCode = 500;
            msg = "FAIL";
            data = false;
        }
        responseData.setStatusCode(statusCode);
        responseData.setStatusMsg(msg);
        responseData.setData(data);
        return new ResponseEntity<ResponseObjectBean>(responseData, HttpStatus.OK);
    }

    }

Add these line in spring.xml

    <!-- mutipart upload configuration -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="1024" />
        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="2048" />
    </bean>

enter image description here

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
1

In addition to Abhijit Chowdhury answer, if you are using spring security still you can remove Content-Type and just keep your token in the header, no need to remove everything from the header.

Also, it is important to restart postman.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
0

1.Remove header section in POSTMAN.

2.In your API:

@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
      public  ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
            @RequestBody  @RequestParam("image") MultipartFile image)
            throws  IOException {}

add the following:

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

so it becomes:

@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public  ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
        @RequestBody  @RequestParam("image") MultipartFile image)
        throws  IOException {}
S.I.
  • 3,250
  • 12
  • 48
  • 77
George Ninan
  • 1,997
  • 2
  • 13
  • 8
-1

Replace @RequestBody @RequestParam("image") to just @RequestBody("image"). The first statement is not valid, see - Spring uploading files.

Kirill Liubun
  • 1,965
  • 1
  • 17
  • 35