Multipart File upload Spring Boot

前端 未结 6 852
栀梦
栀梦 2020-11-29 23:48

Im using Spring Boot and want to use a Controller to receive a multipart file upload. When sending the file I keep getting the error 415 unsupported content type

6条回答
  •  旧时难觅i
    2020-11-30 00:34

    You can simply use a controller method like this:

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity uploadFile(
        @RequestParam("file") MultipartFile file) {
    
      try {
        // Handle the received file here
        // ...
      }
      catch (Exception e) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
      }
    
      return new ResponseEntity<>(HttpStatus.OK);
    } // method uploadFile
    

    Without any additional configurations for Spring Boot.

    Using the following html form client side:

    
    
      

    If you want to set limits on files size you can do it in the application.properties:

    # File size limit
    multipart.maxFileSize = 3Mb
    
    # Total request size for a multipart/form-data
    multipart.maxRequestSize = 20Mb
    

    Moreover to send the file with Ajax take a look here: http://blog.netgloo.com/2015/02/08/spring-boot-file-upload-with-ajax/

提交回复
热议问题