[Java][Liferay] File system in liferay

EditFileEntryAction.javajava

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的對應關係以下:tomcat

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

存儲的文件名會有1。0,2.0之類的,標記的是文件的版本,具體在dlfileversion這張表中code

...待續ip

相關文章
相關標籤/搜索