• [Java][Liferay] File system in liferay


    EditFileEntryAction.java

    protected FileEntry updateFileEntry(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse)
            throws Exception {
        /* 此处强转获取uploadPortletRequest,用于获取InputStream,也可以使用如下代码:
         * HttpServletRequest request = serviceContext.getRequest();
         * UploadRequest uploadRequest = PortalUtil.getUploadServletRequest(request);
         * inputStream = uploadRequest.getFileAsStream(fieldName);
         */
        UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    
        ...
    
        // 获取folderId, 如果自己创建的话,要走DLFolderLocalServiceUtil.java
        if (folderId > 0) {
            Folder folder = DLAppServiceUtil.getFolder(folderId);
    
            if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
                throw new NoSuchFolderException("{folderId=" + folderId + "}");
            }
        }
    
        InputStream inputStream = null;
    
        try {
            String contentType = uploadPortletRequest.getContentType("file");
    
            // inputStream.available()用于获取size
            long size = uploadPortletRequest.getSize("file");
    
            ...
    
            // 获取inputStream
            inputStream = uploadPortletRequest.getFileAsStream("file");
    
            ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), uploadPortletRequest);
    
            FileEntry fileEntry = null;
    
            // Add file entry
    
            fileEntry = DLAppServiceUtil.addFileEntry(
                repositoryId, folderId, sourceFileName, contentType, title,
                description, changeLog, inputStream, size, serviceContext);
    
            // Update file entry and checkin
    
            fileEntry = DLAppServiceUtil.updateFileEntryAndCheckIn(
                    fileEntryId, sourceFileName, contentType, title,
                    description, changeLog, majorVersion, inputStream, size,
                    serviceContext);
    }
    

    DLAppServiceUtil.java

    public FileEntry addFileEntry(long repositoryId, long folderId, String sourceFileName, String mimeType, String title,
            String description, String changeLog, InputStream is, long size, ServiceContext serviceContext)
            throws PortalException, SystemException {
    
                ...
    
                File file = null;
    
                try {
                    /* 创建tempFile,inputStream读取的文件放在tomcat-7.0.62/temp/xxxfile
                     * 根据inputStream创建一个tempFile,然后存储对应的关系到数据库,文件根据数据库中的路径存放在bundle/data/document_library下
                     */
                    file = FileUtil.createTempFile(is);
    
                    return addFileEntry(repositoryId, folderId, sourceFileName, mimeType, title,
                            description, changeLog, file, serviceContext);
                } catch (IOException ioe) {
                    throw new SystemException("Unable to write temporary file", ioe);
                } finally {
                    // 不论addFile是否成功都会删除临时文件
                    FileUtil.delete(file);
                }
            }
        }
        ...
    }
    

    文件路径在数据库中的dlfileentry中存储,与bundle/data/document_library的对应关系如下:

    Table Column companyid folderid treepath name
    Path document_library/ companyid folderid /folderid/

    存储的文件名会有1。0,2.0之类的,标记的是文件的版本,具体在dlfileversion这张表中

    ...待续

  • 相关阅读:
    centos 7更新yum源与更新系统
    IE报错:模块"scrrun.dll"已加载,但对DllRegisterServer的调用失败,错误代码为0x80004005
    translate和replace的区别
    windows10降低IE版本
    powerDesigner 报Unable to connect SQLState=08004 解决方法
    as php交互
    帝国cms模板list.var使用程序代码
    如何用PHP代码实现灵动标签的功能
    【转】PHP调试利器XDebug的安装与使用
    一个不错的资料大全 amfphp
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/6005224.html
Copyright © 2020-2023  润新知