Multipart File upload Spring Boot

前端 未结 6 727
栀梦
栀梦 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条回答
  • 2020-11-30 00:23
    @RequestBody MultipartFile[] submissions
    

    should be

    @RequestParam("file") MultipartFile[] submissions
    

    The files are not the request body, they are part of it and there is no built-in HttpMessageConverter that can convert the request to an array of MultiPartFile.

    You can also replace HttpServletRequest with MultipartHttpServletRequest, which gives you access to the headers of the individual parts.

    0 讨论(0)
  • 2020-11-30 00:31

    In Controller, your method should be;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
        public ResponseEntity<SaveResponse> uploadAttachment(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
    ....
    

    Further, you need to update application.yml (or application.properties) to support maximum file size and request size.

    spring:
        http:
            multipart:
                max-file-size: 5MB
                max-request-size: 20MB
    
    0 讨论(0)
  • 2020-11-30 00:33
       @Bean
        MultipartConfigElement multipartConfigElement() {
            MultipartConfigFactory factory = new MultipartConfigFactory();
            factory.setMaxFileSize("5120MB");
            factory.setMaxRequestSize("5120MB");
            return factory.createMultipartConfig();
        }
    

    put it in class where you are defining beans

    0 讨论(0)
  • 2020-11-30 00:33
    @RequestMapping(value="/add/image", method=RequestMethod.POST)
    public ResponseEntity upload(@RequestParam("id") Long id, HttpServletResponse response, HttpServletRequest request)
    {   
        try {
            MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request;
            Iterator<String> it=multipartRequest.getFileNames();
            MultipartFile multipart=multipartRequest.getFile(it.next());
            String fileName=id+".png";
            String imageName = fileName;
    
            byte[] bytes=multipart.getBytes();
            BufferedOutputStream stream= new BufferedOutputStream(new FileOutputStream("src/main/resources/static/image/book/"+fileName));;
    
            stream.write(bytes);
            stream.close();
            return new ResponseEntity("upload success", HttpStatus.OK);
    
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity("Upload fialed", HttpStatus.BAD_REQUEST);
        }   
    }
    
    0 讨论(0)
  • 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:

    <html>
    <body>
      <form action="/uploadFile" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="Upload"> 
      </form>
    </body>
    </html>
    

    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/

    0 讨论(0)
  • 2020-11-30 00:43

    Latest version of SpringBoot makes uploading multiple files very easy also. On the browser side you just need the standard HTML upload form, but with multiple input elements (one per file to upload, which is very important), all having the same element name (name="files" for the example below)

    Then in your Spring @Controller class on the server all you need is something like this:

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
        public @ResponseBody ResponseEntity<?> upload(
            @RequestParam("files") MultipartFile[] uploadFiles) throws Exception     
    {
        ...now loop over all uploadFiles in the array and do what you want
      return new ResponseEntity<>(HttpStatus.OK);
    }
    

    Those are the tricky parts. That is, knowing to create multiple input elements each named "files", and knowing to use a MultipartFile[] (array) as the request parameter are the tricky things to know, but it's just that simple. I won't get into how to process a MultipartFile entry, because there's plenty of docs on that already.

    0 讨论(0)
提交回复
热议问题