在Java Web应用程序的单独线程中上传文件 [英] Upload a file in a separate thread in a java webapp
问题描述
我需要制作一个表格,用户可以在其中上传大文件(> 200Mo).我想在单独的线程中启动上载,以便用户可以启动3-4次上载,然后执行其他操作.我的问题是,当我运行辅助线程时,将删除生成的.tmp文件. 我使用Struts2.
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.
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();
在run()方法中:
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);
错误:
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)
逐步调试时,我看到当我调用thread.start()时,.tmp文件消失了.
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 ?
推荐答案
基本上从不需要在WebApp中运行多个线程,而且几乎总是一个坏主意.
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:
- 在新标签页中打开上传页面,然后运行标准提交(用户然后手动更改前一个标签页)
- 使用
target="_blank"
在新标签中打开上传操作(用户然后手动更改前一个标签) - 通过AJAX上传(但尺寸过大,可能会遇到限制和问题).
- 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).
我会选择解决方案n.2.
I'd go with solution n.2.
编辑
谢谢您的建议,它应该可以使用,但是我如何在操作中自动关闭该标签?
有很多方法,例如,您可以返回包含以下内容的JSP:
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).
这篇关于在Java Web应用程序的单独线程中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!