Liferay7 BPM门户开发之28: Portlet文件上传,及实体类同步更新上传
抓住核心 。
Liferay文件上传的核心就是使用UploadPortletRequest类
继承关系
java.lang.Object
extended byjavax.servlet.ServletRequestWrapper
extended byjavax.servlet.http.HttpServletRequestWrapper
extended bycom.liferay.util.servlet.UploadPortletRequest
实现关系:
Implemented Interfaces:
- javax.servlet.http.HttpServletRequest
- javax.servlet.ServletRequest
单一文件上传
jsp代码
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> <portlet:defineObjects /> <portlet:actionURL name="upload" var="uploadFileURL"></portlet:actionURL> <aui:form action="<%= uploadFileURL %>" enctype="multipart/form-data" method="post"> <aui:input type="file" name="fileupload" /> <aui:button name="Save" value="Save" type="submit" /> </aui:form>
java:
import java.io.File; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import org.apache.commons.io.FileUtils; import com.liferay.portal.kernel.upload.UploadPortletRequest; import com.liferay.portal.util.PortalUtil; import com.liferay.util.bridges.mvc.MVCPortlet; public class RelatedFilesPortlet extends MVCPortlet { private final static int ONE_GB = 1073741824; private final static String baseDir = "/tmp/uploaded/"; private final static String fileInputName = "fileupload"; public void upload(ActionRequest request, ActionResponse response) throws Exception { UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request); long sizeInBytes = uploadRequest.getSize(fileInputName); if (uploadRequest.getSize(fileInputName) == 0) { throw new Exception("文件为空!"); } // 从uploadRequest获得File对象 File uploadedFile = uploadRequest.getFile(fileInputName); String sourceFileName = uploadRequest.getFileName(fileInputName); // 存储文件的目录 File folder = new File(baseDir); // 对当前目录做可用空间检查 if (folder.getUsableSpace() < ONE_GB) { throw new Exception("空间不足1GB!"); } // 最终的文件路径 File filePath = new File(folder.getAbsolutePath() + File.separator + sourceFileName); // 保存文件到物理路径 FileUtils.copyFile(uploadedFile, filePath); //以下是保存在文档库 ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest); String contentType = MimeTypesUtil.getContentType(uploadedFile); InputStream inputStream = new FileInputStream(uploadedFile); //要保存的文档库路径 Folder folderName = DLAppLocalServiceUtil.getFolder(parentRepositoryId, parentFolderId, "Folder Name"); long folderId = folderName.getFolderId(); long repositoryId = folderName.getRepositoryId(); //保存到文档库 FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(themeDisplay.getUserId(), repositoryId, folderId, uploadedFile.getName(), contentType, "File Name", "description", "changeLog", inputStream, uploadedFile.length(), serviceContext); } }
关于文档库参数说明的解释:
http://blog.csdn.net/lan861698789/article/details/8122203
和实体类一起保存文件的Demo
public void addYourEntity(ActionRequest request, ActionResponse response) throws Exception { UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request); long artistId = ParamUtil.getLong(uploadPortletRequest, "artistId"); String name = ParamUtil.getString(uploadPortletRequest, "name"); int year = ParamUtil.getInteger(uploadPortletRequest, "year"); //更多的实体类信息 ...... InputStream inputStream = uploadPortletRequest.getFileAsStream("file"); ServiceContext serviceContext = ServiceContextFactory.getInstance( YourEntity.class.getName(), uploadPortletRequest); try { YourEntityServiceUtil.addYourEntity( artistId, name, year, inputStream, serviceContext); SessionMessages.add(request, "YourEntityAdded"); String redirect = ParamUtil.getString( uploadPortletRequest, "redirect"); response.sendRedirect(redirect); } catch (Exception e) { SessionErrors.add(request, e.getClass().getName()); if (e instanceof YourEntityNameException || e instanceof PrincipalException) { response.setRenderParameter( "jspPage", "/html/YourEntitys/edit_YourEntity.jsp"); } else { response.setRenderParameter("jspPage", "/html/error.jsp"); } } }
关于作者:
王昕(QQ:475660)
在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
目前维护的开源产品:https://gitee.com/475660