【问题标题】:How can I upload file in liferay我如何在liferay中上传文件
【发布时间】:2014-12-17 11:57:33
【问题描述】:

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

但没有成功。

谁能在这里复制粘贴代码?

提前致谢

【问题讨论】:

  • 你能提供你试过的代码吗?
  • 感谢回复我编辑我的问题
  • 文件夹Uploads 是否存在?如果在此代码执行时您尚未创建该文件夹,则会出现错误。
  • 是的,这是存在的。但我更新了我的代码看看..
  • 使用编辑之前提供的代码(确保子文件夹存在)和新添加的源,上传文件没有问题。我必须强调,我并没有只是将我的文件控制器放在 jsp 文件中。 将源移动到实际的操作方法并在那里继续努力。

标签: liferay liferay-6


【解决方案1】:

如果有人偶然发现:

上传文件.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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2014-12-17
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多