Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Save file to Document directory in liferay 6.1 using API

    • 0
    • 3
    • 3
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.97k
    Comment on it

    File Upload in Document Library of Liferay:- First we need to create the folder in the Document Library of Liferay. In my case the folder is created with dentalnoteDocuments name.

    In below code AddInfo.jsp we can take simple AUI FORM to insert some AUI INPUT tags to insert the patient information and use the AUI:INPUT type file to select the file from the local system after the form submission the call will go to the AddPatient.java file and there we can get the UploadPortletRequest object from the actionRequest.

     UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    

    Then after we can validate the uploadRequest object to ensure that the file size is greater then 0 and uploaded file name should not be null. if it is true then the file is uploaded in the dentalnoteDocuments (which I have created manually from administrator account in Liferay ) folder inside the Liferay Document Library.

    AddInfo.jsp

    <aui:form id="addPatientInfo" name="addPatientInfo" action="<%= addPatientInfo.toString() %>"  method="post" enctype="multipart/form-data">
        <div class="patient-details clearfix">
            <ul class="clearfix">
            <h2>Add Patient Details</h2>
                    <li>
                     <aui:input label="First Name" fieldParam="fname" name="fname" type="text" >
                    </aui:input>
                </li>
                <li>
                     <aui:input label="Last Name" name="lname" fieldParam="lname" type="text" />
                </li>
                <li>
                     <label>DOB</label>
                    <div class="select-box datepicker">
                            <i><img src="/NewTheme1-theme/images/dentalnote&#95;images/calendar.png"></i>
                  <input name="dob" id="startDate" type="text" />    
    
                </div>
                        </li>
                            <li>
                             <aui:input label="Phone" name="phone" fieldParam="phone" type="text" >
                            <aui:validator name="required" errorMessage="Please Phone No." />
                            <aui:validator name="maxLength">10</aui:validator>
                             <aui:validator name="digits"></aui:validator>
                             </aui:input>
                            </li>
                            <li>
                                <label>Clinic Location</label>
                                <div class="select-box">
                                    <i class="arrow"></i>
                                    <select  name="location" fieldParam="location">
                                        <option value="">---</option>
                                        <option value="delhi">Delhi</option>
                                        <option value="dehradun">Dehradun</option>
                                    </select> 
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="border-box clearfix" id="fileTags">
                            <aui:input type="file" name="file" id="uploadFile" >
                            <aui:validator name="acceptFiles">'jpg,jpeg'</aui:validator>
                            </aui:input> 
                    </div> 
    
                    <div class="add-tag">
                    <div class="view clearfix">
                      <input type="submit"  value="Save" />
                      <input type="button" value="View Details" id="viewDTLS" onclick="patientProfile();" />
    
                    </div>
                    </div>
    </aui:form>
    

    AddPatient.java

    public class AddPatient extends MVCPortlet {
    
    
    @ProcessAction(name = "addPatientInfo")
        public void addPatientInfo(ActionRequest actionRequest,ActionResponse actionResponse)
        {
    
            UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    
            try{
    
    
    
    
    
                    /************* To Insert in Document Library Table ******************/
                        if(uploadRequest.getFileName("file")!=null && uploadRequest.getSize("file")>0)
                        {
                            long fileEntryId=0;
                            FileEntry fileEntry=Utils.saveFile(uploadRequest, actionRequest, actionResponse, "file", fileEntryId);
    
                        }
    
    
    
    
            }
            catch(Exception e){System.out.println(e);}
    
        }
    
    }
    

    Utils.java

    package com.dentalnote.util;
    import java.awt.image.BufferedImage;
    import java.io.BufferedInputStream;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.imageio.ImageIO;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    
    import com.liferay.portal.kernel.repository.model.FileEntry;
    import com.liferay.portal.kernel.repository.model.FileVersion;
    import com.liferay.portal.kernel.repository.model.Folder;
    import com.liferay.portal.kernel.upload.UploadPortletRequest;
    import com.liferay.portal.kernel.util.StreamUtil;
    import com.liferay.portal.kernel.util.WebKeys;
    import javax.portlet.PortletPreferences;
    import com.liferay.portal.model.User;
    import com.liferay.portal.service.ServiceContext;
    import com.liferay.portal.service.ServiceContextFactory;
    import com.liferay.portal.theme.ThemeDisplay;
    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
    import com.liferay.portlet.documentlibrary.util.DLUtil;
    
    
    
    public class Utils {
    
        public static FileEntry saveFile(UploadPortletRequest uploadRequest, ActionRequest actionRequest, ActionResponse actionResponse, String parameterName,long fileEntryId)
        {
            boolean isFileAcceptable = false;
            System.out.println("saveFile:::  "+parameterName);
            ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
            User user = themeDisplay.getUser();
    
            String fileURL = null;
            FileEntry fileEntry = null;
            try 
            {           
                String userName = user.getScreenName();
                System.out.println(uploadRequest.getSize(parameterName));
    
                if (uploadRequest.getSize(parameterName)!=0) {
    
    
    
                    String sourceFileName = uploadRequest.getFileName(parameterName);
                    InputStream in = null;
    
                    try {
                        String contentType = uploadRequest.getContentType(parameterName);
                        long size = uploadRequest.getSize(parameterName);
                        PortletPreferences portletPreferences = null;
                        portletPreferences =actionRequest.getPreferences();
                        String[] mimeTypes = DLUtil.getMediaGalleryMimeTypes(portletPreferences, actionRequest);
                        int i =sourceFileName.lastIndexOf('.');
                        String extension = "";
                        if (i > 0) {
                            extension = sourceFileName.substring(i+1);
                        }
                        if(contentType.indexOf("image") == -1)
                        {
                            List<String> extensionList=new ArrayList<String>();
                            extensionList.add("pdf");
                            extensionList.add("doc");
                            extensionList.add("docx");
                            extensionList.add("odt");
                            if(extensionList.contains(extension))
                            {
                                isFileAcceptable = true;
                            }
    
                        }
                        else
                            isFileAcceptable = true;
    
    
                        if(isFileAcceptable)
                        {
                            File file = uploadRequest.getFile(parameterName);
                            in = new BufferedInputStream(uploadRequest.getFileAsStream(parameterName));
                            long repositoryId = DLFolderConstants.getDataRepositoryId(themeDisplay.getSiteGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
                            Folder folder=DLAppServiceUtil.getFolder( repositoryId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "dentalnoteDocuments" ) ;
                            System.out.println("contentType:::  "+contentType +" : "+sourceFileName);
                            if (contentType.indexOf("jpeg") != -1) {
                                sourceFileName = sourceFileName.replace(extension, "png");
                                BufferedImage sourceImage = ImageIO.read(in);  
                                contentType = "image/png";
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                ImageIO.write(sourceImage, "png", baos);
                                in = new ByteArrayInputStream(baos.toByteArray());
    
                            }
    
    
                            ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),actionRequest);
    
    
    
                            String fileTitle =userName+"-"+Long.toString(System.currentTimeMillis());
    
    
                                // Add file entry
                                fileEntry = DLAppServiceUtil.addFileEntry(
                                        repositoryId, folder.getFolderId(), sourceFileName, contentType, fileTitle,
                                        fileTitle, fileTitle, in, size, serviceContext);
    
                                AssetPublisherUtil.addAndStoreSelection(
                                        actionRequest, DLFileEntry.class.getName(),
                                        fileEntry.getFileEntryId(), -1);
    
    
    
                        }
    
    
    
                    }
                    catch (FileNotFoundException e) {
                        //jsonObject.put("errorid", 1); 
    
                        e.printStackTrace();
                    } 
                    catch (IOException e1) {
                        //jsonObject.put("errorid", 1); 
    
                        e1.printStackTrace();
                    }
                    catch(Exception e){
                        //jsonObject.put("errorid", 1); 
                        e.printStackTrace();
                    }
                    finally {
                        StreamUtil.cleanUp(in);
    
                    }
                }
            }
            catch (NullPointerException e) {
                e.printStackTrace();
            }
            catch(Exception e){
                    e.printStackTrace();
            }
    
            return fileEntry;
        }
    
    
    
    
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: