Web からファイルをダウンロードする 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);
}
}
バッファサイズが小さいためにダウンロード速度が遅すぎませんか?
バッファ サイズを増やすことはできますか、またはダウンロード速度を遅くするためにバッファ サイズを最適化する方法はありますか?
何を変更する必要がありますか?
前もって感謝します。