【发布时间】:2017-05-24 22:27:46
【问题描述】:
我正在使用此代码将 Liferay (6.2) 上的服务器上的现有文件下载到本地电脑:
`
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);
}
}
`
此代码仅适用于小文件。但是下载大文件(cca 2GB)会抛出javax.portlet.PortletException: Error occurred during request processing: Java heap space。
如何修复此代码以使其也适用于较大的文件? 我想合适的方法是对大文件使用某种缓冲区,我尝试了一下,但之后即使是较小的文件也不起作用。
【问题讨论】:
-
使用命令行选项 -Xmx 运行 Java,该选项设置堆的最大大小。
-
是否可以使用某种实现而不是此设置来做到这一点?堆大小更改是否有任何负面影响?
-
因为我不知道如何在远程 Liferay 服务器上使用它。我只是创建了部署在服务器上的
.war文件。
标签: java download liferay httprequest