Cannot use Apache Commons FileUpload with Spring Boot multipart.resolve-lazily

做~自己de王妃 提交于 2020-01-25 10:01:08

问题


I'm building a REST API using Spring Boot framework. There is one endpoint where users will be able to upload a large file (~1GB). I'm using Streaming API from Apache Commons FileUpload.

I only want to enable streaming only at that endpoint. Therefore, I configure my Spring Boot as follow:

spring.servlet.multipart.enabled = true
spring.servlet.multipart.resolve-lazily = true
spring.servlet.multipart.max-file-size = 2GB
spring.servlet.multipart.max-request-size = 2GB

And here is my endpoint:

@PostMapping(path = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> importData(HttpServletRequest request) throws IOException, FileUploadException {
    System.out.println("Streaming...");

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart) {
        return ResponseEntity.badRequest().build();
    }

    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iterStream = upload.getItemIterator(request);
    while (iterStream.hasNext()) {
        System.out.println("Iterating...");

        FileItemStream item = iterStream.next();

        if (!item.isFormField()) {

            String name = item.getFieldName();
            System.out.println("Field name is: " + name);

            try (InputStream uploadedStream = item.openStream();
                 OutputStream out = new FileOutputStream("file.zip")) {

                IOUtils.copy(uploadedStream, out);
            }

        } else {

            try (InputStream stream = item.openStream()) {
                String formFieldName = item.getFieldName();
                String formFieldValue = Streams.asString(stream);
                System.out.println(String.format("Form field found - %s: %s", formFieldName, formFieldValue));
            }
        }
    }

    return ResponseEntity.ok("Data streamed successfully.");
}

The method is called and the code is executed. However, the FileItemIterator is empty, there for the while loop is not executed.

This code works perfectly if I set spring.servlet.multipart.enabled = false. But I don't want to do that because as I said, I don't want to disable Spring Boot Multipart Resolver globally, but only at this endpoint.

So, what is wrong with my code? Why is the FileItemIterator is empty?

Thank you for your help.


回答1:


I solved my problem.

I think the problem is at Spring StandardServletMultipartResolver. When I switch to CommonsMultipartResolver (as configured below), my code works perfectly.

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setDefaultEncoding("UTF-8");
    resolver.setResolveLazily(true);
    return resolver;
}


来源:https://stackoverflow.com/questions/54813440/cannot-use-apache-commons-fileupload-with-spring-boot-multipart-resolve-lazily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!