• 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");
            }
        }
    }


  • 相关阅读:
    synchronous_commit 参数的再次说明
    ubuntu 16.04 + zabbix 3.4 + postgresql pg_monz
    ubuntu 16.04 + zabbix 3.4 + postgresql shell
    ubuntu 16.04 + zabbix 3.4 + postgresql UserParameter
    ubuntu 16.04 + zabbix 3.4 + postgresql libzbxpgsql
    ubuntu 16.04 + zabbix 3.4 + zabbix agent
    ubuntu 16.04 + zabbix 3.4 + zabbix proxy
    ubuntu 16.04 + zabbix 3.4 + zabbix server
    apt-get、apt-cache的一些日常操作
    ubuntu 16.04 apt-get source 替换为 aliyun
  • 原文地址:https://www.cnblogs.com/starcrm/p/6053851.html
Copyright © 2020-2023  润新知