原来已经做好的一个portlet,使用的是jsp实现的,后来需要增加文件下载的功能,以下是我的解决办法:
- 开发一个portlet继承com.liferay.portlet.JSPPortlet;
- 覆盖processAction方法,如下:
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);
}
}
3. 修改portlet-ext.xml文件如下:
<portlet>
<portlet-name>100</portlet-name>
<display-name>download</display-name>
<portlet-class>cn.javaedu.portlet.DownloadFile</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/portlet/ext/test/view.jsp</value>
</init-param>
<expiration-cache>300</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
<portlet-name>100</portlet-name>
<display-name>download</display-name>
<portlet-class>cn.javaedu.portlet.DownloadFile</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/portlet/ext/test/view.jsp</value>
</init-param>
<expiration-cache>300</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
4. 在需要下载的页面上的链接如下写:
<a href='<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>"> </portlet:actionURL>' >download</a>
当用户点击download的下载链接时,系统就会提示用户保存文件
转载于:https://blog.51cto.com/wolfoxer/9989