I am attempting to download over 100mb video mp4 file by using below code. But i am getting "java.lang.OutOfMemoryError: Java heap space" while downloading.
Using jdk1.7 & Spring MVC.
I have refered below SO links but none works for me.
how to download large files without memory issues in java
java.lang.OutOfMemoryError: Java heap space while downloading a large file from an URL
Please help me to resolve this.
OutputStream outStream = null;
FileInputStream inputStream = null;
try {
ServletContext context = request.getSession().getServletContext();
String fileName = Integer.toString(fileId);
filePath = fileName+".mp4";
File downloadFile = new File(filePath);
inputStream = new FileInputStream(new File(filePath));
// get MIME type of the file
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",downloadFile.getName());
response.setHeader(headerKey, headerValue);
outStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0, bytesBuffered = 0;
while((bytesRead = inputStream.read(buffer)) > -1) {
outStream.write(buffer, 0, bytesRead);
bytesBuffered += bytesRead;
if (bytesBuffered > 1024 * 1024) { //flush after 1MB
bytesBuffered = 0;
outStream.flush();
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outStream != null) {
outStream.flush();
outStream.close();
}
}