使用 API 将文件保存到 liferay 6.1 中的 Document 目录 [英] Save file to Document directory in liferay 6.1 using API

查看:11
本文介绍了使用 API 将文件保存到 liferay 6.1 中的 Document 目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

试试这个代码片段

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

这篇关于使用 API 将文件保存到 liferay 6.1 中的 Document 目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆