upload file springboot Required request part 'file' is not present

前端 未结 4 534
感情败类
感情败类 2020-12-14 16:09

I want to add an upload function to my spring boot application; this is my upload Rest Controller

package org.sid.web;

import java.io.BufferedOutputStream         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 16:36

    This is how your request in Postman should look like:

    My sample code:

    application.properties

    #max file and request size 
    spring.http.multipart.max-file-size=10MB
    spring.http.multipart.max-request-size=11MB
    

    Main Application Class:

    Application.java

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    Rest controller class:

    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    
        @Controller
        @RequestMapping("/fileupload")
        public class MyRestController {
    
        @RequestMapping(value = "/file", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
            public @ResponseBody String myService(@RequestParam("file") MultipartFile file,
                    @RequestParam("id") String id) throws Exception {
    
        if (!file.isEmpty()) { 
    
               //your logic
                            }
    return "some json";
    
                    }
        }
    

    pom.xml

    //...
    
    
            org.springframework.boot
            spring-boot-starter-parent
            1.5.2.RELEASE
             
        
    
    ....
    
    
    
    
                org.springframework.boot
                spring-boot-starter-web-services
    
    
    //...
    

提交回复
热议问题