zoukankan      html  css  js  c++  java
  • [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这张表中

    ...待续

  • 相关阅读:
    cjson库的使用以及源码阅读
    Map集合 把map 集合 转成Set的方法
    JAVA 吃货联盟
    第二本 第六章 接口 采用面向接口编程组装一台计算机
    java 类和对象
    Jdk安装和环境配置
    Spring mvc 同类之间方法的互相跳转 "redirect:/manage/ManageUser";
    关于Mybatis参数传值问题(常用) 个人比较推荐第二种哦,可以减少代码量,唯一要注意的是自己传递的参数个数个顺序就好
    数据库拼接字符模糊查询语句(mybatis中运用(xml文件))
    ExpandableListAdapter实现的三程常用方式
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/6005224.html
Copyright © 2011-2022 走看看