- 建立liferay portlet(略);
- 在portlet对应jsp页面编写如下:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@page contentType="text/html; charset=utf-8" %> <portlet:defineObjects /> <!-- 注意用的是resourceURL --> <portlet:resourceURL var="download"/> <a href="<%=download %>">点击下载文件</a>
- 界面如下:
- 在portlet后台代码如下:
public class test003 extends MVCPortlet { public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { System.out.println("下载"); //直接用本地文件了... File file = new File("c:\\a.txt"); byte[] buff =new byte[(int) file.length()]; InputStream in = new FileInputStream(file); in.read(buff); HttpServletRequest request = PortalUtil .getHttpServletRequest(resourceRequest); HttpServletResponse response = PortalUtil .getHttpServletResponse(resourceResponse); /** * sendFile方法算是Liferay封装的一个工具类 * request:略... * response:略... * fileName:文件名... * byte:略... * contentType:文件类型 */ ServletResponseUtil.sendFile(request, response, "a.txt", buff, ContentTypes.APPLICATION_OCTET_STREAM); } }
- 点击超链接
- 文件下载完成。
liferay中资源文件下载
最新推荐文章于 2020-09-27 14:41:03 发布