问题
I need to make a form where users can upload big files (>200Mo). I wanted to launch the uploads in separate threads so users can launch 3-4 uploads and then do something else. My problem is the generated .tmp file is deleted when i run the secondary thread. I use Struts2.
What struts2 gives me:
private String uploadContentType;
private String uploadFileName;
private File upload;
I transfer those information to my thread using its constructor
MyThread thread=new MyThread (sourceName, uploadFileName, upload, user, database);
thread.start();
In the run() method:
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);
File destFile =new File(UPLOAD_DIRECTORY, myFileFileName);
FileUtils.copyFile(myFile, destFile);
And the error:
Src Filename:
C:\***myeclipsepath***\upload_1949ed75_1002_4ccf_b198_
25faff66563a_00000003.tmp
Dst File name: books.xml
java.io.FileNotFoundException:
C:\***myeclipsepath***\upload_1949ed75_1002_4ccf_b198_
25faff66563a_00000003.tmp (Le fichier spécifié est introuvable)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at org.apache.commons.io.FileUtils.doCopyFile(FileUtils.java:1068)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1021)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:968)
at bo.threads.MyThread .run(MyThread .java:68)
When i debug step-by-step i see that the .tmp file disappear when i call thread.start().
So how can i upload files on other threads than the main one ?
回答1:
Running multiple threads in a WebApp is basically never required and almost always a bad idea.
You are probably not aware that you can upload multiple files concurrently. You can also tweak your configuration settings to allow files with a size higher than the default threshold (both per-file and per-request).
The only thing remaining to accomplish your requirement of letting the user do something else after they started uploading can be achieved by either:
- opening the upload page in a new tab, then run a standard submit (user then change previous tab manually)
- open the upload action in a new tab with
target="_blank"(user then change previous tab manually) - upload through AJAX (but with huge size you might encounter limits and problems).
I'd go with solution n.2.
EDIT
Thank you for your advice, it should work but how can I close automatically the tab in my action ?
There are many ways, for example you can return a JSP consisting of the following content:
<script>
window.close();
</script>
But consider informing the user of the positive (or negative) outcome of the operation with a more descriptive, non-self-closing page, or (if you close the tab) with a listener of some kind from the other tab (that would be perfect, and that would be a completely new question, so try making this work before).
来源:https://stackoverflow.com/questions/31782910/upload-a-file-in-a-separate-thread-in-a-java-webapp