原来已经做好的一个portlet,使用的是jsp实现的,后来需要增加文件下载的功能,以下是解决办法:
- 开发一个portlet继承com.liferay.portlet.JSPPortlet;
- 覆盖processAction方法,如下:
-
java 代码
public class DownloadFile extends JSPPortlet { public void processAction(ActionRequest req,ActionResponse res){ File file = new File("your file "); InputStream is = new FileInputStream(file); com.liferay.util.servlet.ServletResponseUtil.sendFile(((ActionResponseImpl)res).getHttpServletResponse(), file.getName(), is); } }
-
修改portlet-ext.xml文件如下:
<portlet> <portlet-name>100portlet-name> <display-name>downloaddisplay-name> <portlet-class>cn.javaedu.portlet.DownloadFileportlet-class> <init-param> <name>view-jspname> <value>/portlet/ext/test/view.jspvalue> </init-param> <expiration-cache>300expiration-cache> <supports> <mime-type>text/htmlmime-type> </supports> <resource-bundle>com.liferay.portlet.StrutsResourceBundleresource-bundle> <security-role-ref> <role-name>power-userrole-name> </security-role-ref> <security-role-ref> <role-name>userrole-name> </security-role-ref> </portlet>
-
在需要下载的页面上的链接如下写:
当用户点击download的下载链接时,系统就会提示用户保存文件<a href='<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>"> </portlet:actionURL>' >download</a>