P
P
prizrak392017-04-18 23:06:17
Java
prizrak39, 2017-04-18 23:06:17

How to properly implement Spring file upload?

Hello.
It is necessary to implement the rest service for uploading files to the server.
Here is the controller method code:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile multipartFile) {
 
        if (multipartFile == null)
            return new ResponseEntity<String>("Error", HttpStatus.NOT_FOUND);
 
        try {
            int read = 0;
            File newFile = new File("/" + multipartFile.getName());
 
            FileOutputStream fos = new FileOutputStream(newFile);
            CountingOutputStream out = new CountingOutputStream(fos);
            byte[] bytes = new byte[1024];
 
            while ((read = multipartFile.getInputStream().read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
 
            out.flush();
            out.close();
 
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return new ResponseEntity("" + "/" + multipartFile.getName(), HttpStatus.OK);
    }

Declared in the config file:
@Configuration
@EnableWebMvc
@ComponentScan("com.kidsdays.server")
public class WebConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
 
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new ObjectMapper());
        converter.setSupportedMediaTypes(Arrays.asList(new MediaType[] {MediaType.APPLICATION_JSON, MediaType.MULTIPART_FORM_DATA}));
 
        converters.add(converter);
    }
 
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000);
        return new CommonsMultipartResolver();
    }

The application starts without errors, but the result of the call is an error
HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-19
@sergey-gornostaev

Add the enctype attribute to the form from which the file is submitted:

<form enctype="multipart/form-data" method="post" action="/uploadFile">
    <input type="file" name="file">
</form>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question