使用API​​将文件保存到liferay 6.1中的Document目录

时间:2013-07-29 08:28:05

标签: file upload liferay liferay-6

我需要将上传的文件保存在Document&的子目录中。来自web-form portlet的liferay中的媒体文件夹 我已经扩展了Web表单portlet,但文件已成功上传到数据库&不在文件和媒体文件夹。
我尝试使用代码将文件上传到文档目录,但没有成功,请帮忙。

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

String title = file.getName();

DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(themeDisplay.getScopeGroupId(), 0, "Test");

ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),actionRequest);

Map<String, Fields> fieldsMap = new HashMap<String, Fields>();

long fileEntryTypeId = DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;

FileInputStream inputStream = new FileInputStream(file);

DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.addFileEntry(themeDisplay.getUserId(), 10153, dlFolder.getRepositoryId(), 
                            dlFolder.getRepositoryId(), title, file.getContentType(), title, "fileDesc", "sss",
                            fileEntryTypeId, fieldsMap, file, inputStream, file.length(), serviceContext);

inputStream.close();

DLFileEntryLocalServiceUtil.updateFileEntry(themeDisplay.getUserId(), dlFileEntry.getFileEntryId(), title, file.getContentType(),
        title, "fileDesc", "comment", true, dlFileEntry.getFileEntryTypeId(), fieldsMap, file, null, file.length(), serviceContext);

2 个答案:

答案 0 :(得分:1)

试试此代码段

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);

File file = uploadRequest.getFile("file");
String contentType = MimeTypesUtil.getContentType(file);

InputStream inputStream  = new FileInputStream(file);

Folder folderName = DLAppLocalServiceUtil.getFolder(parentRepositoryId, 
                                                    parentFolderId, 
                                                    "Folder Name");
long folderId = folderName.getFolderId();
long repositoryId = folderName.getRepositoryId();

FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(themeDisplay.getUserId(), 
                                                         repositoryId, 
                                                         folderId, 
                                                         file.getName(), 
                                                         contentType, 
                                                         "File Name", 
                                                         "description", 
                                                         "changeLog", 
                                                         inputStream, 
                                                         file.length(), 
                                                         serviceContext);

答案 1 :(得分:0)

我知道这是一个老问题,但今天我遇到了类似的问题。我使用DLFileEntryLocalServiceUtil,我必须同时调用addFileEntry()updateFileEntry()才能正确创建资产。

请参阅Liferay DLFileEntryLocalServiceUtil.addFileEntry does not create AssetEntry record