1

I am using springboot 2.2.6.RELEASE with commons-fileupload 1.4 and i have disabled spring.servlet.multipart as follows:

spring.servlet.multipart.enabled = false

my controllers is as follows :

@RequestMapping(value = "/UploadFileServlet", method = RequestMethod.POST)
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (isMultipart) {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
            factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
            factory.setFileCleaningTracker(null);
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);

            String imageFileName = request.getParameter("imageFileName");

            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    try (InputStream uploadedStream = item.getInputStream();
                         OutputStream out = new FileOutputStream(imageFileName);) {
                        IOUtils.copy(uploadedStream, out);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Issue : upload.parseRequest always returns an empty list

Bob
  • 157
  • 1
  • 11
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

1 Answers1

0

I found out the solution, I had a file upload filter for primefaces library that was not configured for specific url pattern, so it was stealing the request, after I configured the url pattern for it, the issue was solved :

@Bean
    public FilterRegistrationBean FileUploadFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new org.primefaces.webapp.filter.FileUploadFilter());
        registration.setName("PrimeFaces FileUpload Filter");
        registration.addUrlPatterns("/faces/*");
        registration.addUrlPatterns("*.xhtml");
        return registration;
    }
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498