![](/img/trans.png)
[英]Using a servlet, how do you download multiple files from a database and zip them for client download
[英]How to download multiple files from different URLs using recursiveaction?
我使用 ExecutorService 同時從不同的 URL 下載文件,但與順序下載相比,它花費的時間並不短; 因此,我想使用 RecursiveAction。 下面是要應用的代碼:
平行類:
String[] links;
File[] files;
public Parallel(String[] link, File[] files) {
this.links = link;
this.files = files;
}
@Override
public void run() {
try {
for (int i = 0; i <= 3; i++) {
URL url = new URL(links[i]);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
double fileSize = (double) http.getContentLengthLong();
BufferedInputStream bis = new BufferedInputStream(http.getInputStream());
FileOutputStream fos = new FileOutputStream(this.files[i]);
BufferedOutputStream bos = new BufferedOutputStream(fos, 600000);
byte[] buffer = new byte[1024];
double downloadedData = 0.00;
int readData = 0;
while ((readData = bis.read(buffer, 0, 1024)) >= 0) {
bos.write(buffer, 0, readData);
downloadedData += readData;
}
bos.close();
bis.close();
System.out.println(this.files[i] + " -> done");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
在主類中,我只提供文件的鏈接和路徑並開始執行
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Runnable worker = new Parallel(links, files);
executor.execute(worker);
executor.shutdown();
任何幫助表示贊賞!
您在這里沒有正確使用並發。
你應該做的是這樣的:
String link;
File file;
public Parallel(String link, File file) {
this.link = link;
this.file = files;
}
@Override
public void run() {
try {
URL url = new URL(link);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
double fileSize = (double) http.getContentLengthLong();
BufferedInputStream bis = new BufferedInputStream(http.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos, 600000);
byte[] buffer = new byte[1024];
double downloadedData = 0.00;
int readData = 0;
while ((readData = bis.read(buffer, 0, 1024)) >= 0) {
bos.write(buffer, 0, readData);
downloadedData += readData;
}
bos.close();
bis.close();
System.out.println(file + " -> done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
進而:
String[] links;
File[] files;
//...
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (int i = 0; i <= 3; i++) {
Runnable worker = new Parallel(links[i], files[i]);
executor.execute(worker);
}
executor.shutdown();
然后每次下載實際上都會有自己的線程。
在您的情況下,所有下載都會得到一個線程,所有這些都依次發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.