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

335 views Asked by At

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?

1

There are 1 answers

0
Nadir Fouka On BEST ANSWER

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 !