0

如何将文件上传到liferay 6.1中的文档和库

我在用

addFileEntry(long userId, long groupId, long repositoryId, long folderId, String sourceFileName, String mimeType, String title, String description, String changeLog, long fileEntryTypeId, Map<String,Fields> fieldsMap, File file, InputStream is, long size, ServiceContext serviceContext) 

DLFileEntryLocalServiceUtil

4

1 回答 1

0

公共无效文件上传(字符串文件名)抛出异常{

    // file
    File file = new File(filename);

    if (file.exists()) {


        long folderId = 12344;
        long repositoryId = 10180;

        // filename
        String sourceFileName = file.getName();

        // mimetype
        String mimeType = "";
        if (sourceFileName.substring(sourceFileName.length() - 4)
                .compareToIgnoreCase(".pdf") == 0) {
            mimeType = "application/pdf";
        } else {
            if (sourceFileName.substring(sourceFileName.length() - 4)
                    .compareToIgnoreCase(".xls") == 0) {
                mimeType = "application/xls";
            }
        }

        // for title
        String title = file.getName();
        // for description
        String description = file.getName();
        // changeLog
        String changeLog = "";
        // fileEntryTypeId
        long fileEntryTypeId = 0;

        // for fieldMap
        Fields feild = null;
        Map<java.lang.String, com.liferay.portlet.dynamicdatamapping.storage.Fields> fieldsMap = new HashMap<java.lang.String, com.liferay.portlet.dynamicdatamapping.storage.Fields>();
        fieldsMap.put("", feild);

        // for InputStream
        InputStream is = new FileInputStream(file);

        // for file size
        double bytes = file.length();
        long size = (long) (bytes / 1024);

        // service Context Object
        String uuid = UUID.randomUUID().toString();

        Date date = new Date();
        ServiceContext serviceContext = new ServiceContext();
        serviceContext.setUuid(uuid);
        serviceContext.setCreateDate(date);
        serviceContext.setModifiedDate(date);

        // PermissionChecking
        PrincipalThreadLocal.setName(10196);
        PermissionChecker permissionChecker = PermissionCheckerFactoryUtil
                .create(UserLocalServiceUtil.getUser(10196));
        PermissionThreadLocal.setPermissionChecker(permissionChecker);

        DLAppServiceUtil.addFileEntry(repositoryId, folderId,
                sourceFileName, mimeType, title, description, changeLog,
                is, size, serviceContext);
        /*
         * DLFileEntryLocalServiceUtil.addFileEntry(userId, groupId,
         * repositoryId, folderId, sourceFileName, mimeType, title,
         * description, changeLog, fileEntryTypeId, fieldsMap, file, is,
         * size, serviceContext);
         */
    }
}
于 2013-10-25T02:55:31.883 回答