Exception when uploading Multipart from the Angularjs controller to the Spring Boot server

Asked

Viewed 243 times

0

I’m having trouble sending files (images, pdfs) from the client side to the server side of my app.

Whenever I try to send a file, Spring returns this exception:

status:"Bad Request"
exception:"org.springframework.web.multipart.support.MissingServletRequestPartException"
message:"Required request part 'file' is not present"
path:"/upload"
status:400

The Spring controller looks like this:

@RestController
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping(value = "", 
                    method = RequestMethod.POST)
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) {

        try {
            if(file != null) {
                System.out.println("Arqiuivo recebido!");
            }
        }

        catch (Exception e) {
            System.out.println("Deu erro!");
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    } 
}

And I also set up Multipart to solve like this:

 @Bean
 public MultipartResolver multipartResolver() {
     return new CommonsMultipartResolver();
 }

In the client side, as I am using the module ng-file-upload, I am using the following div that appears on the developer’s github:

<div class="button" ngf-select ng-model="imagem" name="file" ngf-pattern="'image/*'"
  ngf-accept="'image/*'" ngf-max-size="20MB" ngf-min-height="100" 
  ngf-resize="{width: 100, height: 100}">Select
</div>

And in the Angularjs controller, I have the following:

Upload.upload({
    url: 'upload',
    data: {file: $scope.imagem}
}).progress(function (evt) {

}).success(function (data, status, headers, config) {

});

I haven’t been successful so far. Could someone help me?

EDIT

I tried to use Postman to send a pro server image, but it gives the same exception when I try to send it through my client:

inserir a descrição da imagem aqui

1 answer

0

It seems all right with your java code, maybe it’s something in your client, you already tried to send the file by curl or Postman?

  • I added a screen print of trying to send a file through Postman, but it also gives the same exception when I try to send it through my client. Do you have any idea if it is necessary to configure something beyond the MultipartResolver in Spring for the upload to work?

  • Actually with spring-boot you do not need to use this multipartresolver, see only here http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#howto-Multipart-file-upload-Configuration. Maybe you just have to remove the multiparthresolver.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.