我使用 JSF 2.2 和 Primefaces 5.3。我想将上传的文件保存在磁盘上。下面你可以看到我是如何尝试做到这一点的。当我按下 Send
按钮时,我得到一个 java.nio.file.NoSuchFileException
异常(在我的帖子末尾我还放了一个完整的堆栈跟踪):
当我跟踪堆栈跟踪时,我看到异常是由这一行引起的(我认为是这样):try(InputStream input = uploadFile.getInputstream()){
这对我来说很奇怪。我可以获得上传文件的名称(通过 uploadFile.getFileName()
)但我无法获得文件的大小(通过 uploadFile.getSize()
)并且我无法获得 InputStream
(通过 uploadFile.getInputstream()
)。在这两种情况下,我都会收到 NoSuchFileException
异常。
附加信息:我必须上传大文件(即 200 MB 或更多)。当然,当我上传小文件时,我得到了同样的异常(exception)。
您知道为什么我会遇到此异常以及如何解决此问题吗?
这是我上传文件的页面的一部分:
<h:form>
<p:growl id="messages" showDetail="true" />
<p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign">
<p:outputLabel for="file" value="File:" />
<p:fileUpload id="file" fileLimit="1"
fileUploadListener="#{dataController.handleFileUpload}"
mode="advanced" dragDropSupport="true" sizeLimit="1000000000"
uploadLabel="Upload" cancelLabel="Delete" allowTypes="/(\.|\/)(csv|binetflow)$/" />
<p:commandButton id="buttonSend" value="Send"
action="#{dataController.send()}" update="messages"/>
</p:panelGrid>
</h:form>
这是 CDI bean,它是上述页面的 Controller :
@Named
@ViewScoped
public class DataController implements Serializable {
private static final long serialVersionUID = 1383572529241805730L;
public void handleFileUpload(FileUploadEvent event){
uploadFile=event.getFile();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", event.getFile().getFileName() + " is uploaded"));
}
public void send(){
try(InputStream input = uploadFile.getInputstream()){
Path folder=Paths.get("F:/Files");
String filename = FilenameUtils.getBaseName(uploadFile.getFileName());
String extension = FilenameUtils.getExtension(uploadFile.getFileName());
Path file = Files.createTempFile(folder, filename + "-", "." + extension);
Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Uploaded file successfully saved in " + file));
} catch (IOException e1) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", null));
e1.printStackTrace();
}
}
private UploadedFile uploadFile;
}
这是完整的堆栈跟踪:
10:40:45,822 ERROR [stderr] (default task-24) java.nio.file.NoSuchFileException: [PATH]\Wildfly_10\WILDFLY_HOME\standalone\tmp\MasterProject.war\undertow1357918758070690245upload
10:40:45,823 ERROR [stderr] (default task-24) at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
10:40:45,823 ERROR [stderr] (default task-24) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
10:40:45,823 ERROR [stderr] (default task-24) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
10:40:45,823 ERROR [stderr] (default task-24) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
10:40:45,823 ERROR [stderr] (default task-24) at java.nio.file.Files.newByteChannel(Files.java:361)
10:40:45,824 ERROR [stderr] (default task-24) at java.nio.file.Files.newByteChannel(Files.java:407)
10:40:45,824 ERROR [stderr] (default task-24) at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
10:40:45,824 ERROR [stderr] (default task-24) at java.nio.file.Files.newInputStream(Files.java:152)
10:40:45,824 ERROR [stderr] (default task-24) at io.undertow.servlet.spec.PartImpl.getInputStream(PartImpl.java:63)
10:40:45,824 ERROR [stderr] (default task-24) at org.primefaces.model.NativeUploadedFile.getInputstream(NativeUploadedFile.java:45)
10:40:45,824 ERROR [stderr] (default task-24) at com.system.controller.DataController.send(DataController.java:163)
10:40:45,824 ERROR [stderr] (default task-24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:40:45,825 ERROR [stderr] (default task-24) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
10:40:45,825 ERROR [stderr] (default task-24) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
10:40:45,825 ERROR [stderr] (default task-24) at java.lang.reflect.Method.invoke(Method.java:498)
10:40:45,825 ERROR [stderr] (default task-24) at javax.el.ELUtil.invokeMethod(ELUtil.java:308)
10:40:45,825 ERROR [stderr] (default task-24) at javax.el.BeanELResolver.invoke(BeanELResolver.java:415)
10:40:45,825 ERROR [stderr] (default task-24) at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
10:40:45,825 ERROR [stderr] (default task-24) at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
10:40:45,825 ERROR [stderr] (default task-24) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
10:40:45,826 ERROR [stderr] (default task-24) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
10:40:45,826 ERROR [stderr] (default task-24) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
10:40:45,826 ERROR [stderr] (default task-24) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
10:40:45,826 ERROR [stderr] (default task-24) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
10:40:45,826 ERROR [stderr] (default task-24) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
10:40:45,826 ERROR [stderr] (default task-24) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
10:40:45,826 ERROR [stderr] (default task-24) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
10:40:45,827 ERROR [stderr] (default task-24) at javax.faces.component.UICommand.broadcast(UICommand.java:315)
10:40:45,827 ERROR [stderr] (default task-24) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
10:40:45,827 ERROR [stderr] (default task-24) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
10:40:45,827 ERROR [stderr] (default task-24) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
10:40:45,827 ERROR [stderr] (default task-24) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
10:40:45,827 ERROR [stderr] (default task-24) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
10:40:45,828 ERROR [stderr] (default task-24) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
10:40:45,828 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
10:40:45,828 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
10:40:45,828 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
10:40:45,829 ERROR [stderr] (default task-24) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
10:40:45,829 ERROR [stderr] (default task-24) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:40:45,829 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
10:40:45,829 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
10:40:45,829 ERROR [stderr] (default task-24) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:40:45,830 ERROR [stderr] (default task-24) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
10:40:45,830 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
10:40:45,830 ERROR [stderr] (default task-24) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
10:40:45,830 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
10:40:45,831 ERROR [stderr] (default task-24) at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
10:40:45,831 ERROR [stderr] (default task-24) at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
10:40:45,831 ERROR [stderr] (default task-24) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:40:45,831 ERROR [stderr] (default task-24) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
10:40:45,831 ERROR [stderr] (default task-24) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:40:45,832 ERROR [stderr] (default task-24) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:40:45,832 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
10:40:45,832 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
10:40:45,832 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
10:40:45,833 ERROR [stderr] (default task-24) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
10:40:45,833 ERROR [stderr] (default task-24) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
10:40:45,833 ERROR [stderr] (default task-24) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
10:40:45,833 ERROR [stderr] (default task-24) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
10:40:45,833 ERROR [stderr] (default task-24) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
10:40:45,834 ERROR [stderr] (default task-24) at java.lang.Thread.run(Thread.java:745)
更新:
我注意到了一些新东西。如果我将
send()
方法的内容移动到 handleFileUpload()
方法(下面的代码) - 文件将保存在磁盘上:@Named
@ViewScoped
public class DataController implements Serializable {
private static final long serialVersionUID = 1383572529241805730L;
public void handleFileUpload(FileUploadEvent event){
uploadFile=event.getFile();
try(InputStream input = uploadFile.getInputstream()){
Path folder=Paths.get("F:/Files");
String filename = FilenameUtils.getBaseName(uploadFile.getFileName());
String extension = FilenameUtils.getExtension(uploadFile.getFileName());
Path file = Files.createTempFile(folder, filename + "-", "." + extension);
Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Uploaded file successfully saved in " + file));
} catch (IOException e1) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", null));
e1.printStackTrace();
}
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", event.getFile().getFileName() + " is uploaded"));
}
public void send(){
}
private UploadedFile uploadFile;
}
但是如果用户按下
Send
按钮,我想将上传的文件保存在磁盘上,而不是更早(即不是在用户按下 Upload
按钮时),因此有必要将文件保存在 send()
方法中。我还注意到,当我使用
<p:fileUpload>
组件的简单模式(下面的修改形式)时,一切正常(即当用户按下按钮时文件被保存)。我必须为 uploadFile
bean 中的 DataController
对象创建 getter 和 setter 才能使用 #{dataController.uploadFile}
。我不得不将 enctype="multipart/form-data"
属性添加到 <h:form>
组件中(没有这个属性就行不通)。<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign">
<p:outputLabel for="file" value="File:" />
<p:fileUpload id="file" value="#{dataController.uploadFile}" mode="simple" skinSimple="true" sizeLimit="1000000000" allowTypes="/(\.|\/)(csv|binetflow)$/"/>
<p:commandButton value="Submit" ajax="false" actionListener="#{dataController.send()}" disabled="false" />
</p:panelGrid>
</h:form>
这个解决方案的问题是:我需要
<p:fileUpload>
组件的高级模式。不明白为什么我在使用高级模式时无法访问
send()
方法中上传的文件。我使用 @ViewScoped
范围,所以它应该可以工作。 最佳答案
您的具体问题是因为您试图读取在先前请求中上传的上传文件。当 HTTP 请求完成时,一个体面的容器将清除与 HTTP 请求相关的所有临时数据。这显然也涵盖了 WildFly 的非读取上传文件(但不确定其他人的行为如何)。
最好的办法是直接保存它们,如有必要,保存在某个临时位置。然后,在最后的操作方法中,将它们移动/重命名为所需的永久位置/名称。为了清除最终用户在上传一堆文件后从不调用最终操作方法的情况下的“忘记”文件,最好的办法是跟踪 session 范围 bean 中的所有 File
并在 file.delete()
注释中执行 @PreDestroy
方法。一个具体的例子可以在这里找到:How to handle and delete "forgotten" uploaded files?
如果上传表单是 View 范围的,并且您正在使用 OmniFaces,那么您也可以使用 OmniFaces CDI @ViewScoped
,当用户通过导航或关闭选项卡/窗口卸载页面时,ojit_a 的 @PreDestroy
已经运行。