Fork me on GitHub

[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这张表中

...待续

posted @ 2016-10-27 19:35  种花生的读书人  阅读(276)  评论(0编辑  收藏  举报

该博客仅作为记录笔记,转载随意