Every time flush the output stream object in the while loop after reading some data. you can set a long value and check. if that limit reaches you can flush the data in the output stream object, so that system will flushes that much amount of data and free the memory allocated for that so will not come the out of memory error.
ServletOutputStream sos = response.getOutputStream();
long byteRead = 0;
try {
byte[] buf = new byte[8291];
while (true) {
int r = is.read(buf);
if (r == -1)
break;
sos.write(buf, 0, r);
byteRead +=r;
if(byteRead > 1024*1024){ //flushes after 1mb
byteRead = 0;
sos.flush();
}
}
} finally {
if(sos != null){
sos.flush();
}
try{is.close();}catch(Exception e){}
try{sos.close();}catch(Exception e){}
}