临时文件的文件未找到例外 [英] File not found exception for temporary files
问题描述
我正在构建java app(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:\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.
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);
}
谢谢,
卢克。
Thanks, Luke.
推荐答案
基于堆栈跟踪,看起来您将 UploadedFile
实例存储为属性一个类本身 Serializable
。这个不对。你应该在< 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屋!