Forums de discussion

Error Java Heap Space while downloading a large file (GB) from server

Michaela Bamburova, modifié il y a 6 années.

Error Java Heap Space while downloading a large file (GB) from server

New Member Publications: 4 Date d'inscription: 23/05/17 Publications récentes
I am using this code to download an existing file from the server on Liferay (6.2) into a local pc:

File file = getFile(diskImage.getImageType(), diskImage.getId());

HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
HttpServletResponse httpResp = PortalUtil.getHttpServletResponse(response);

httpResp.setContentType("application/octet-stream");
httpResp.setHeader("Content-Transfer-Encoding", "binary");
httpResp.setHeader("Content-Length", String.valueOf(file.length()));
httpResp.setHeader("Content-Disposition", "attachment; filename=" + file.getName());

try (InputStream input = new FileInputStream(file)) {
ServletResponseUtil.sendFile(httpReq, httpResp, file.getName(), input, "application/octet-stream");
} catch (Exception e) {
throw new FilesManagerException(e);
}
}

This code works fine only for small files. But downloading large files (cca 2Gemoticon throws javax.portlet.PortletException: Error occurred during request processing: Java heap space.

How to fix this code so it works properly for larger files as well? I guess that the suitable approach would be to use some kind of a buffer for large files and I try it but it wouldn't work even for the smaller files afterwards.
Michaela Bamburova, modifié il y a 6 années.

RE: Error Java Heap Space while downloading a large file (GB) from server (Réponse)

New Member Publications: 4 Date d'inscription: 23/05/17 Publications récentes
So I used PortletResponseUtil.sendFile(...); I implemented serveResource() with above mentioned method. It seems that everything is working fine.