Today we will discuss how to upload multiple files in Single browse . We already discuss how to upload a Single file in my previous blog How to upload file in Liferay. Here we create a application where user can select multiple documents and upload in a single Click. Before reading this blog it is highly recommended to read my previous blogs on Document and Media Portlet in Detal , Upload Documents Programatically in Liferay and How to upload files in Liferay.
So lets start this step by step:-
Step 1:-Create Liferay Project and Portlet
Create a Liferay Project in eclipse and then create a MVC portlet in the Project.For theses you may refer Step 1 and Step 2 of my previous blog Here .
Step 2:-Create Your Form
Open view.jsp and paste this code:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<portlet:actionURL var="uplaodURL" name="uploadDocument"></portlet:actionURL> | |
<b>Please Upload a Document</b> | |
<form action="<%=uplaodURL%>" method="post" enctype="multipart/form-data"> | |
<input type="file" name="uploadedFile" multiple="multiple"> | |
<input type="Submit" name="Submit"> | |
</form> |
Explanation:-
Here we set multiple="multiple" so that we can upload multiple files simultaneously.
Step 3:-Create portlet.properties
Create a property file in src with name portlet.properties and paste this content:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileupload.folder.name=File_Upload | |
fileupload.folder.description=This folder is create for Upload documents |
Step 4:-Entry for thumbnails
If file is too large liferay not created thumbnail because default maximum size for thumbnail creation of file is 100MB only.Due to this a error comes as "File is too large for preview or thumbnail generation ".So open portal-ext.properties and paste this line and restart the Server.
dl.file.entry.previewable.processor.max.size=-1
Step 5:-Create method in Java Class
Open your Java class and Paste this Content:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.document.upload; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import javax.portlet.ActionRequest; | |
import javax.portlet.ActionResponse; | |
import javax.portlet.PortletException; | |
import com.liferay.portal.kernel.exception.PortalException; | |
import com.liferay.portal.kernel.exception.SystemException; | |
import com.liferay.portal.kernel.repository.model.Folder; | |
import com.liferay.portal.kernel.upload.FileItem; | |
import com.liferay.portal.kernel.upload.UploadPortletRequest; | |
import com.liferay.portal.kernel.util.WebKeys; | |
import com.liferay.portal.service.ServiceContext; | |
import com.liferay.portal.service.ServiceContextFactory; | |
import com.liferay.portal.theme.ThemeDisplay; | |
import com.liferay.portal.util.PortalUtil; | |
import com.liferay.portlet.documentlibrary.model.DLFileEntry; | |
import com.liferay.portlet.documentlibrary.model.DLFolder; | |
import com.liferay.portlet.documentlibrary.model.DLFolderConstants; | |
import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
import com.liferay.util.portlet.PortletProps; | |
public class DocumentUpload extends MVCPortlet { | |
private static String ROOT_FOLDER_NAME = PortletProps.get("fileupload.folder.name"); | |
private static String ROOT_FOLDER_DESCRIPTION = PortletProps.get("fileupload.folder.description"); | |
private static long PARENT_FOLDER_ID = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID; | |
public void uploadDocument(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException,PortletException, PortalException, SystemException | |
{ | |
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); | |
createFolder(actionRequest, themeDisplay); | |
fileUpload(themeDisplay, actionRequest); | |
} | |
public Folder createFolder(ActionRequest actionRequest,ThemeDisplay themeDisplay) | |
{ | |
boolean folderExist = isFolderExist(themeDisplay); | |
Folder folder = null; | |
if (!folderExist) { | |
long repositoryId = themeDisplay.getScopeGroupId(); | |
try { | |
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFolder.class.getName(), actionRequest); | |
folder = DLAppServiceUtil.addFolder(repositoryId,PARENT_FOLDER_ID, ROOT_FOLDER_NAME,ROOT_FOLDER_DESCRIPTION, serviceContext); | |
} catch (PortalException e1) { | |
e1.printStackTrace(); | |
} catch (SystemException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
return folder; | |
} | |
public boolean isFolderExist(ThemeDisplay themeDisplay){ | |
boolean folderExist = false; | |
try { | |
DLAppServiceUtil.getFolder(themeDisplay.getScopeGroupId(), PARENT_FOLDER_ID, ROOT_FOLDER_NAME); | |
folderExist = true; | |
System.out.println("Folder is already Exist"); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
return folderExist; | |
} | |
public Folder getFolder(ThemeDisplay themeDisplay){ | |
Folder folder = null; | |
try { | |
folder =DLAppServiceUtil.getFolder(themeDisplay.getScopeGroupId(), PARENT_FOLDER_ID, ROOT_FOLDER_NAME); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
return folder; | |
} | |
public void fileUpload(ThemeDisplay themeDisplay,ActionRequest actionRequest){ | |
UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest); | |
Map<String, FileItem[]> files= uploadPortletRequest.getMultipartParameterMap(); | |
Folder folder = getFolder(themeDisplay); | |
InputStream is; | |
File file; | |
String title, description,mimeType; | |
long repositoryId; | |
for (Entry<String, FileItem[]> file2 : files.entrySet()) { | |
FileItem item[] =file2.getValue(); | |
try { | |
for (FileItem fileItem : item) { | |
title = fileItem.getFileName(); | |
description = title +" is added via programatically"; | |
repositoryId = themeDisplay.getScopeGroupId(); | |
mimeType = fileItem.getContentType(); | |
file = fileItem.getStoreLocation(); | |
is =fileItem.getInputStream(); | |
try { | |
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest); | |
DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), title, mimeType, | |
title, description, "", is, file.getTotalSpace(), serviceContext); | |
} catch (PortalException e) { | |
e.printStackTrace(); | |
} catch (SystemException e) { | |
e.printStackTrace(); | |
} | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Explanation:-
- Here we fetch all the information related to file by UploadPortletRequest object and passing "uploadedFile" which is same as name attribute in our view.jsp for input type file.
- All the method used in this class are already explained in my previous blog you can check it Here
Step 6:-Check Output
Now deploy your portlet add to page and Check the output as:-
You can Download Source code from How to Upload Multiple Files in Liferay.
Hope this will Help....
Related Post:-
No comments:
Post a Comment