(contentType.startsWith("multipart/form-data"))) {
req = new UploadServletRequest(req);
}
在UploadServletRequest类中,则将所有当前请求参数进行了提取,并对上传的文件做了临时存储。实际上内部是采用Apache Commons Upload进行的封装。
这样在PortletAction的procesAction处理中,可以对上传附件进行业务处理和存储。 当然,你可以通过ActionRequest来强制自己获取原始的UploadServletRequest对象,可以如下操作:
public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception {
//注意这里面是获取UploadPortletRequest对象
UploadPortletRequest urequest = PortalUtil.getUploadPortletRequest(req);
File f = urequest.getFile("file1");
}
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
HttpServletRequest httpReq = reqImpl.getHttpServletRequest();
UploadServletRequest urequest = (UploadServletRequest)httpReq;
置于在Action处理阶段,获取到UploadServletRequest对象后存储附件、过滤附件类型等操作就很容易实现了,此处不再累赘叙述。
不过在附件上传中都会涉及到一个问题:控制附件上传的大小和类型。对于类型的控制,liferay没有提供任何控制和扩展实现机制,这个只能开发人员在外围自己实现。
PortletURL portletURL = renderResponse.createActionURL();
portletURL.setWindowState(WindowState.MAXIMIZED);
portletURL.setParameter("struts_action", "/venus/template/fileupload/FileUpoadAction");
portletURL.setParameter("cmdx", "uploadfile");
portletURL.setParameter("cmd", "add");
%>
<form name="testForm" method="post"
action="<%= portletURL.toString() %>"
enctype="multipart/form-data">
</form>