Save file to Document directory in liferay 6.1 using API

拟墨画扇 提交于 2019-11-30 16:17:37

问题


I need to save a uploaded file in sub directory of Document & Media folder in liferay from web-form portlet.
I have extended the web Form portlet to do so, but file is getting uploaded successfully in database & not in Document & Media folder.
I tried following code to upload the file in document directory but no success please help .

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

回答1:


Try this code snippet

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



回答2:


I know it's an old question, but I had similar issue today. I used DLFileEntryLocalServiceUtil, and I had to call both addFileEntry() and updateFileEntry() in order to correctly create the asset.

See Liferay DLFileEntryLocalServiceUtil.addFileEntry does not create AssetEntry record



来源:https://stackoverflow.com/questions/17919271/save-file-to-document-directory-in-liferay-6-1-using-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!