Skip to content Skip to sidebar Skip to footer

Resolving Java.lang.outofmemory Error Causing Half Of The File To Be Uploaded To Server

I am trying to upload large video files to the server. I wrote a piece of code which works well for the image so I thought I should work it for the video too. I wrote the below cod

Solution 1:

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = newbyte[bufferSize];
byte byt[]=newbyte[bufferSize];

fileInputStream.read(byt);

You are not writing the bytes read into byt out to the outputstream. Removing the first read into byt should fix a problem. But you will come across another problem where a file over 1MB only the first 1MB will be uploaded.

A proper way to copy a stream is to do something similar to

byte[] buf = new byte[ 1024 ];
int read = 0;
while( ( read = in.read( buf ) ) != -1 ) {
    out.write( buf, 0, read );
}

Solution 2:

avoid buffering the whole POST data in RAM by adding conn.setChunkedStreamingMode(0); after : con.setDoOutput(true); hope this help :)

Post a Comment for "Resolving Java.lang.outofmemory Error Causing Half Of The File To Be Uploaded To Server"