liferay中使用liferay-ui进行文件上传

来源:互联网 发布:淘宝历史订单无故消失 编辑:程序博客网 时间:2024/06/05 01:13

 在liferay中使用liferay-ui进行文件上传比较容易,效果还不错,下面我们来看都需要哪些代码

1,jsp页面中,见下面代码

<%
 for (int i = 1; i <= 5; i++) {
 %>

  <tr>
   <td>
    <liferay-ui:message key="file" /> <%= i %>
   </td>
   <td>
    <input name="<portlet:namespace />msgFile<%= i %>" size="70" type="file" />
   </td>
  </tr>

 <%
 }
 %>

通过循环控制上传文件个数

2,我们再看action的部分,如下代码

List files = new ArrayList();

  if (attachments) {
   UploadPortletRequest uploadReq =
    PortalUtil.getUploadPortletRequest(req);

   for (int i = 1; i <= 5; i++) {
    File file = uploadReq.getFile("msgFile" + i);
    String fileName = uploadReq.getFileName("msgFile" + i);
    byte[] bytes = FileUtil.getBytes(file);

    if ((bytes != null) && (bytes.length > 0)) {
     ObjectValuePair ovp = new ObjectValuePair(fileName, bytes);

     files.add(ovp);
    }
   }
  }

将request中的文件存放在list中

3,最后在service进行保存,如下代码

if (files.size() > 0) {
   long companyId = issue.getCompanyId();
   String portletId = CompanyImpl.SYSTEM_STRING;
   long groupId1 = GroupImpl.DEFAULT_PARENT_GROUP_ID;
   long repositoryId = CompanyImpl.SYSTEM;
   String dirName = issue.getAttachmentsDir();

   try {
    try {
     dlService.deleteDirectory(
      companyId, portletId, repositoryId, dirName);
    }
    catch (NoSuchDirectoryException nsde) {
    }

    dlService.addDirectory(companyId, repositoryId, dirName);

    for (int i = 0; i < files.size(); i++) {
     ObjectValuePair ovp = (ObjectValuePair)files.get(i);

     String fileName = (String)ovp.getKey();
     byte[] byteArray = (byte[])ovp.getValue();

     try {
      dlService.addFile(
       companyId, portletId, groupId1, repositoryId,
       dirName + "/" + fileName, StringPool.BLANK,
       new String[0], byteArray);
     }
     catch (DuplicateFileException dfe) {
     }
    }
   }
   catch (RemoteException re) {
    throw new SystemException(re);
   }
  }
  这样通过documentlibrary的DLService进行文件保存

 

原创粉丝点击