java 多线程上传文件,在Java Web应用程序的单独线程中上传文件

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.(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 ?

解决方案

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:

window.close();

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值