Exclude file for temporary files

I am creating a java application (Spring and JSF and PrimeFaces). I upload the file to the server, however, if I click the "next" button right after the file upload is complete, I will 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) 

If I wait a couple of seconds, everything will be fine. I assume that the boot file should have time to copy itself to a temporary folder, does anyone have an idea how to solve this problem. I am using Tomcat 7.

Here is my download method:

 public StreamedContent getDownloadFile() { InputStream inputStream = new ByteArrayInputStream(selectedBook.getBookText().getText().getBytes()); return new DefaultStreamedContent(inputStream, "text/plain", selectedBook.getTitle() + ".txt", BookBean.encoding); } 

Thanks, Luke.

+4
source share
2 answers

Based on the stack trace, it looks like you are storing an instance of UploadedFile as a property of the class, which itself is Serializable . It is not right. You should capture the contents of the downloaded file immediately in the file listening method <p:fileUpload handleFileUpload> (or the submit button if you use <p:fileUpload mode="simple"> ). Keep it in a more permanent place. For instance. local disk file system or database, or perhaps even as a byte[] bean property. Then pass the file name of the local file system of the disk or PK database or byte[] to have a handle to download the file.

To summarize, just make sure your Serializable bean support is completely free of the UploadedFile property and this problem will disappear.

+8
source

Without any code, the first thing that comes to mind is thread synchronization, so your button action will wait for the download process to complete.

EDIT: By the way you describe it, your action seems to be invoked too soon, so the file has not been correctly written yet.

One way around this is to check if the file exists:

 File file = new File(<path_to_tmp_file>); if(file.exists()){ //Download file } 
+1
source

Source: https://habr.com/ru/post/1498755/


All Articles