0
我有一個java下載文件的方法,但是通過java代碼下載比使用任何瀏覽器下載文件要慢。使用Java優化下載速度
我的代碼如下,
public void downloadFile(String downloadURL) {
System.out.println("Starting file download .....");
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
URL u = new URL(downloadURL);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
String fileName = uc.getHeaderField("Content-Disposition");
fileName = YouTubeDownloader.parseResponse(fileName, "filename=\"", "\"");
System.out.println(fileName);
if (uc.getHeaderField("Content-Length") == null) {
System.out.println("File Length zero!!!!");
}
float fileSize = Float.parseFloat(uc.getHeaderField("Content-Length"));
in = new BufferedInputStream(uc.getInputStream());
System.out.println("File size : " + fileSize);
fout = new FileOutputStream("c:\\Dinesh\\" + fileName);
byte data[] = new byte[1024];
int count = 0;
int downloaded = 0;
while ((count = in.read(data, 0, 1024)) != -1) {
downloaded += count;
int downloadPercentage = (int) ((downloaded/fileSize) * 100);
System.out.println(downloadPercentage);
fout.write(data, 0, count);
}
System.out.println("File download completed.....");
} catch (Exception e) {
System.out.println(e);
}
}
是因爲較低的緩衝區大小的下載速度太慢?
我可以增加緩衝區大小還是如何優化相同的緩衝下載速度?
我需要修改哪些東西?
在此先感謝。
除非你正在寫一個緩慢的設備,其性能應該由網絡速度/服務器傳輸速率的限制。緩衝輸出流可能會有所幫助,因此可能會將讀取大小增加到8192字節。 – 2013-03-19 13:31:17