在Liferay上传文件
任何人都可以帮助我如何使用DLFileEntry将文件上传到文档和媒体文件夹中 我搜索但没有得到确切的代码。我怎样才能做到这一点。 我只是把我的文件控制器放在jsp文件中。 我尝试使用以下代码
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
String sourceFileName = uploadRequest.getFileName("fileName");
System.out.println("file name " + sourceFileName);
File file = uploadRequest.getFile("fileName");
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
System.out.println("User Id " + themeDisplay.getUserId() + ": " + themeDisplay.getScopeGroupId());
long FOLDER_ID = 0;
long repositoryId = themeDisplay.getScopeGroupId();
long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
List<Folder> lFolder = DLAppServiceUtil.getFolders(repositoryId, parentFolderId);
for (Folder folder : lFolder) {
System.out.println(lFolder);
System.out.println(folder.getFolderId());
}
//ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);
ServiceContext serviceContext = ServiceContextFactory.getInstance(FileEntry.class.getName(), actionRequest);
System.out.println("hello");
long defaultRepoId = DLFolderConstants.getDataRepositoryId(themeDisplay.getScopeGroupId(),DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
DLFileEntry dlFileEntry=DLFileEntryLocalServiceUtil.addFileEntry(themeDisplay.getUserId(),themeDisplay.getScopeGroupId(), defaultRepoId, 12518, sourceFileName, MimeTypesUtil.getContentType(file), "fileTitle", "fileDesc", "sss",0,null,file,null,file.length(),serviceContext);
DLFileEntryLocalServiceUtil.updateFileEntry(themeDisplay.getUserId(), dlFileEntry.getFileEntryId(), sourceFileName, MimeTypesUtil.getContentType(file), "fileTitle", "fileDesc", "comment", true, dlFileEntry.getFileEntryTypeId(), null,file, null, file.length(), serviceContext);
但没有取得成功。
可以在这里复制粘贴代码吗?
提前致谢
答案 0 :(得分:1)
万一有人偶然发现:
upload_file.jsp:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<portlet:actionURL name='uploadFile' var="uploadFileURL" windowState="normal" />
<aui:form action="<%= uploadFileURL %>" method="POST" name="fm" enctype="multipart/form-data">
<aui:fieldset>
<aui:input type="file" name="file-to-upload"/>
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
</aui:fieldset>
</aui:form>
MyPortlet.java:
public class MyPortlet extends MVCPortlet {
//action method
public void uploadFile(ActionRequest request, ActionResponse response)
throws Exception {
UploadPortletRequest uploadRequest
= PortalUtil.getUploadPortletRequest(request);
ServiceContext serviceContext = ServiceContextFactory.getInstance(
MyPortlet.class.getName(), uploadRequest);
this.uploadFileEntity(serviceContext, uploadRequest);
response.setRenderParameter("mvcPath", "/html/view.jsp");
}
// Create a folder called "A_FOLDER" in Documents & Media
private void uploadFileEntity(ServiceContext serviceContext,
UploadPortletRequest request)
throws PortalException, SystemException {
String filename = "";
String description = "File description";
try{
// serviceContext.scopeGroupId is the groupId of a site
long repositoryId = DLFolderConstants.getDataRepositoryId(
serviceContext.getScopeGroupId(),
DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
Folder f = DLAppLocalServiceUtil.getFolder(
repositoryId, 0L, "A_FOLDER");
long folderId = f.getFolderId();
File file = request.getFile("file-to-upload");
filename = request.getFileName("file-to-upload");
String mimeType = MimeTypesUtil.getContentType(file);
FileEntry entry = DLAppLocalServiceUtil.addFileEntry(serviceContext.getUserId(),
repositoryId, folderId, filename,
mimeType, filename, description, "",
file, serviceContext
);
}catch(PortalException e){
_log.error("An exception occured uploading file: "
+ e.getMessage());
throw e;
}catch(SystemException e ){
_log.error("An exception occured uploading file: "
+ e.getMessage());
throw e;
}
}
private static Log _log = LogFactoryUtil.getLog(MyPortlet.class);
}