Apache FileUpload 不适用于 springboot:ServletFileUpload.parseRequest 始终为空
Apache FileUpload doesn't work with springboot : ServletFileUpload.parseRequest is always empty
我正在使用 springboot 2.2.6.RELEASE 和 commons-fileupload 1.4
我已经禁用了 spring.servlet.multipart 如下:
spring.servlet.multipart.enabled = false
我的控制器如下:
@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);
}
}
问题:upload.parseRequest总是returns一个空列表
我找到了解决方案,我有一个未针对特定 url 模式配置的 primefaces 库的文件上传过滤器,因此在我配置 url 模式后它窃取了请求为此,问题已解决:
@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;
}
我正在使用 springboot 2.2.6.RELEASE 和 commons-fileupload 1.4 我已经禁用了 spring.servlet.multipart 如下:
spring.servlet.multipart.enabled = false
我的控制器如下:
@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);
}
}
问题:upload.parseRequest总是returns一个空列表
我找到了解决方案,我有一个未针对特定 url 模式配置的 primefaces 库的文件上传过滤器,因此在我配置 url 模式后它窃取了请求为此,问题已解决:
@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;
}