我有一个处理大文件(下载,上传)的应用程序。
我正在使用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 );


私人int BufferAdapterBandwidth(String string)抛出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()   ;
}


它为我工作!

07-27 19:15