简体   繁体   中英

Upload/Download big file with Apache Commons 4.5.5 with Java 11

I have an application that processes large files (download, upload). I'm using apache-commons to handle http transmissions. The problem is the download is 100 faster locally. However, once the code is deployed, the download of the files gets too slow. The code is too simple;

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 ();

In local: when I set lenghtBytes to 1024 * 1024 the download speed takes effect immediately.
on production : no change if 1024 or 1024 * 1024

Do you have an idea?

Passe the max bytes available to your client ( config BandWidth) ;

HttpURLConnection httpConn  = (HttpURLConnection) url.openConnection();             httpConn.setChunkedStreamingMode(this.BufferAdapterBandwidth(url) / 100 );     

private int BufferAdapterBandwidth(String string ) throws IOException { // TODO Auto-generated method stub

    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()   ;
}

Its work for Me !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM