我怎么能一次下载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