我正在构建java app(Spring& JSF& PrimeFaces)。我上传文件到服务器,但是,如果我在文件上传结束后点击“下一步”按钮,我会收到此错误:
Aug 24, 2013 8:12:34 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/codekata] threw exception [Request processing failed; nested exception is org.springframework.webflow.execution.repository.FlowExecutionRestorationFailureException: A problem occurred restoring the flow execution with key 'e1s3'] with root cause
java.io.FileNotFoundException: C:\Users\Luke\AppData\Local\Temp\upload__6f71235a_140b1bdd246__8000_00000011.tmp (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.commons.fileupload.disk.DiskFileItem.readObject(DiskFileItem.java:709)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1891)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
java.lang.Thread.run(Thread.java:722)
如果我等了几秒钟,一切正常。 我假设上传文件需要有时间将自己复制到临时文件夹,是否有人知道如何解决此问题。我使用Tomcat 7。
这是我的下载方法:
public StreamedContent getDownloadFile()
{
InputStream inputStream = new ByteArrayInputStream(selectedBook.getBookText().getText().getBytes());
return new DefaultStreamedContent(inputStream, "text/plain", selectedBook.getTitle() + ".txt", BookBean.encoding);
}
谢谢, 路加。
答案 0 :(得分:8)
基于堆栈跟踪,看起来您将UploadedFile
实例存储为类本身的属性Serializable
。这个不对。您应该在<p:fileUpload handleFileUpload>
文件上传监听器方法(或使用<p:fileUpload mode="simple">
时的提交按钮)中抓取上传的文件内容立即。将其存放在更永久的位置。例如。本地磁盘文件系统或数据库,甚至可能是byte[]
bean属性。然后传递本地磁盘文件系统文件名,数据库PK或byte[]
,以便有一个句柄来下载文件。
总结一下,只需确保你的Serializable
支持bean完全没有UploadedFile
属性,这个问题就会消失。
答案 1 :(得分:1)
没有任何代码,首先想到的是同步线程,以便你的按钮操作将等待上传过程完成。
编辑:从您描述的方式来看,您的操作显然过早被调用,因此文件尚未正确编写。
解决此问题的一种方法是检查文件是否存在:
File file = new File(<path_to_tmp_file>);
if(file.exists()){
//Download file
}