When copying a file, jdk pushes it completely into memory. Code:

HttpURLConnection conn = null; URL urlSt = new URL(urlString); conn = (HttpURLConnection) urlSt.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-Length", Long.toString(fileSize)); OutputStream out = conn.getOutputStream(); InputStream in = new FileInputStream(fileName); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) >0) { out.write(buf, 0, len); } in.close(); out.close(); 

On the Internet, I found some solutions, but they did not help, but just started to throw out new ones that correspond to them.

  conn.setFixedLengthStreamingMode(); conn.setChunkedStreamingMode(); 

EXAMPLE: when using the second block I get this

 java.lang.IllegalStateException: Fixed length streaming mode set 

if a

 fileSize=1,7Гб setFixedLengthStreamingMode(fileSize) setChunkedStreamingMode(1024) 

When testing, we used the following data:

  • 800MB - RAM: 1024MB = Java heap space;
  • 970MB - RAM: 2024 = OK;
  • 1.7GB - RAM: 6144Mb = OK;
  • 4.1GB - RAM: 6144Mb = Java heap space;

QUESTION: how to send a large file and do not get the check.

  • So do not drive it into memory or what do you need? - BigTows
  • BigTows is your answer. need to mark as the only true. and never ask questions again ... D-side - now I will correct the question a little with the indication of the size of the tests with which the tests were conducted - Alex
  • one
    Try wrapping both input and output in BufferInputStream and BufferOutputStream. And also add the flush method, otherwise you just write to the outgoing stream, but send it only to close. - ezhov_da
  • @BigTows - currently sending ... - Alex
  • one
    @ D-side, but I have not yet had the opportunity to properly test this situation, as I will test more than 4GB on the file or at least -Xmx. (ran on 1.7GB but without specifying -Xmx). How to test accomplish your goal. - Alex

1 answer 1

The correct answer was all the same methods mentioned above. here is the code.

 HttpURLConnection conn = null; URL urlSt = new URL(urlString); conn = (HttpURLConnection) urlSt.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); // именно эта строка, в этом месте conn.setFixedLengthStreamingMode(fileSize) conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-Length", Long.toString(fileSize)); OutputStream out = conn.getOutputStream(); InputStream in = new FileInputStream(fileName); byte[] buf = new byte[102400]; int len = 0; while ((len = in.read(buf)) >0) { out.write(buf, 0, len); out.flush(); } in.close(); out.close(); 

Everything works as it should.

Tested with such data:

  • Data: 1.7GB; RAM (-xmx): 512MB = OK;