我怎麼能一次下載2到3個文件,當1完成它拾取另一個?現在它可以完成它需要做的事情,但是如果一次下載30個視頻,則需要一段時間,所以我希望它一次下載2或3個視頻。如何一次完成多個任務?
try {
URL url;
byte[] buf;
int byteRead, byteWritten = 0;
url = new URL(getFinalLocation(fAddress));;
outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\\" + localFileName));
uCon = url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((byteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
byteWritten += byteRead;
}
System.out.println("Downloaded Successfully.");
//System.out.println("File name:\"" + localFileName + "\"\nNo ofbytes :" + byteWritten);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
嘗試線程 - https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html – radoh