临时文件的文件未找到异常 [英] File not found exception for temporary files
问题描述
我正在构建 Java 应用程序(Spring & JSF & PrimeFaces).我将文件上传到服务器,但是,如果我在文件上传结束后立即单击下一步"按钮,则会出现此错误:
I'm building java app (Spring & JSF & PrimeFaces). I upload a file to the server, however, if I click "next" button right after the file upload ends, I get this error:
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:UsersLukeAppDataLocalTempupload__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.
If I wait a couple of seconds, all works fine. I assume that the upload file needs to have time to copy itself to the temporary folder, does anyone have an idea how to resolve this problem. I use Tomcat 7.
这是我的下载方法:
public StreamedContent getDownloadFile()
{
InputStream inputStream = new ByteArrayInputStream(selectedBook.getBookText().getText().getBytes());
return new DefaultStreamedContent(inputStream, "text/plain", selectedBook.getTitle() + ".txt", BookBean.encoding);
}
谢谢,卢克.
推荐答案
根据堆栈跟踪,您似乎将 UploadedFile
实例存储为类的属性,该类由本身可序列化
.这个不对.您应该在 <p:fileUpload handleFileUpload>
文件上传侦听器方法中立即抓取上传的文件内容(如果您使用的是 <p:fileUpload mode="simple">
).将其存放在更永久的位置.例如.本地磁盘文件系统,或数据库,或者甚至作为 byte[]
bean 属性.然后传递本地磁盘文件系统文件名,或数据库 PK,或 byte[]
来代替,以便有一个句柄来下载文件.
Based on the stack trace, it looks like that you're storing the UploadedFile
instance as a property of a class which is by itself Serializable
. This is not right. You should be grabbing the uploaded file content immediately in the <p:fileUpload handleFileUpload>
file upload listener method (or the submit button in case you are using <p:fileUpload mode="simple">
). Store it at a more permanent location. E.g. the local disk file system, or the database, or maybe even as a byte[]
bean property. And then pass the local disk file system filename, or database PK, or the byte[]
around instead in order to have a handle to download the file back.
总而言之,只要确保您的 Serializable
支持 bean 完全没有 UploadedFile
属性,这个问题就会消失.
Summarized, just make sure that your Serializable
backing bean is completely free of a UploadedFile
property and this problem shall disappear.
这篇关于临时文件的文件未找到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!