[英]Upload/Download big file with Apache Commons 4.5.5 with Java 11
我有一個處理大文件(下載、上傳)的應用程序。 我正在使用 apache-commons 來處理 http 傳輸。 問題是本地下載速度快了 100。 但是,一旦部署了代碼,文件的下載就會變得太慢。 代碼太簡單了;
int lenghtBytes = 1024 * 1024;
File outFile = this.file.getDossier ();
out = new FileOutputStream (outFile);
this.panel.jProgressBarTransfert.setMaximum (100);
byte [] buffer = new byte [lenghtBytes];
int l;
long bitTransfered = 0;
while ((l = in.read (buffer, 0, buffer.length))! = -1) {
out.write (buffer, 0, l); // We write on the disc.
bitTransfered + = l; // Update the number of bits transferred.
this.update (bitTransfered, httpResponse.getEntity (). getContentLength ());
}
in.close ();
在本地:當我將 lenghtBytes 設置為 1024 * 1024 時,下載速度立即生效。
在生產中:如果 1024 或 1024 * 1024 沒有變化
你有想法嗎?
將可用的最大字節數傳遞給您的客戶端(配置帶寬);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setChunkedStreamingMode(this.BufferAdapterBandwidth(url) / 100 );
private int BufferAdapterBandwidth(String string) throws IOException { // TODO 自動生成的方法存根
HttpURLConnection httpConn2 = (HttpURLConnection) new URL(string.replace("https", "http")).openConnection();
BufferedInputStream streamBW = new BufferedInputStream(httpConn2.getInputStream());
AppletAWS.println("Nombre de bytes max qu'ont peut avoir :" + ( streamBW.available() == 0 ? 1024*100 : streamBW.available() ) );
return streamBW.available() == 0 ? 1024*100 : streamBW.available() ;
}
它為我工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.