1

我需要从 web-form portlet 将上传的文件保存在 liferay 中 Document & Media 文件夹的子目录中。
我已经扩展了 web 表单 portlet 来执行此操作,但文件已成功上传到数据库中,而不是 Document & Media 文件夹中。
我尝试使用以下代码将文件上传到文档目录中,但没有成功,请帮忙。

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);
4

2 回答 2

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);
于 2013-07-29T12:58:06.313 回答
0

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

请参阅Liferay DLFileEntryLocalServiceUtil.addFileEntry 不创建 AssetEntry 记录

于 2013-11-29T17:57:37.843 回答