我正在尝试通过JSF 2.2 <h:inputFile>
上传文件。当我在嵌入式Tomcat 8.0.23上运行我的代码时一切正常。当我尝试在独立的Tomcat 8.0.23上运行它并调用Part.getInputStream()
捕获FileNotFoundException
。
.xhtml:
<h:form enctype="multipart/form-data" id="formId">
<h:inputFile value="#{bean.file}" >
</h:form>
config .java:
@Configuration
public class JsfConfig implements ServletContextAware {
@Bean
public ServletRegistrationBean facesServletRegistration() {
FacesServlet servlet = new FacesServlet();
MultipartConfigElement multipartConfigElement = new MultipartConfigElement(null, 1024*1024*5, 1024*1024*20, 1024*1024*30);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, new String[] {"*.jsf", "*.xhtml"});
servletRegistrationBean.setName("FacesServlet");
servletRegistrationBean.setMultipartConfig(multipartConfigElement);
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}
}
的.java:
private Part file; //get, set
private byte[] content; //get, set
public void save(){
content = getContentFromFile(file);
//..
}
private byte[] getContentFromFile(Part file) {
try {
return IOUtils.toByteArray(file.getInputStream());
} catch (IOException ex) {
LOG.error("Read from file failed: " + ex);
return null;
}
}
错误:
java.io.FileNotFoundException: /home/user01/apps/apache-tomcat-8.0.23/work/Catalina/localhost/app-web/upload_650b964a_0ff5_4c2f_8f95_07baba2cdc3b_00000121.tmp (No such file or directory)
更新 我在Windows和Linux上测试了这个错误。