liferay portlet实现文件下载的方法

java | 2019-09-13 10:02:39

liferay portlet实现文件下载的方法案例:


String name=FilePath;
name=name.substring(name.lastIndexOf("/")+1, name.length());
HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(resourceRequest);
HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(resourceResponse);
httpResponse.setContentType("applicationoctet-stream;charset=UTF-8");
String userAgent = httpRequest.getHeader("User-Agent").toLowerCase();
byte[] bytes = userAgent.contains("safari") ? name.getBytes("UTF-8") : name.getBytes("gbk");
name = new String(bytes, "ISO-8859-1");
httpResponse.setHeader("Content-disposition", String.format("attachment;filename=\"%s\"", name));
OutputStream out=resourceResponse.getPortletOutputStream();
//repository.getFile("", SVNRevision.HEAD.getNumber(), null, out);
//把文件流放入out中,此处省略
if(out!=null){
  out.flush();
  out.close();
}


登录后即可回复 登录 | 注册
    
关注编程学问公众号