Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to download file in liferay 6.2 ?

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 4.02k
    Comment on it

    Hello Guy's
    Below code will help you to download file from server using liferay 6.2.
    Here I am writing a portlet to download file.
    For better understanding create portlet in your eclipse and follow below steps :
    Step: 1
    Edit view.jsp and put below code

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
    <%@ taglib prefix="liferay-portlet" uri="http://liferay.com/tld/portlet" %>
    
     <portlet:defineObjects />
    
     <a href="<portlet:resourceURL/>">Download</a>
    
    

    When you click on "Download" link, It's call to serveResource(ResourceRequest request, ResourceResponse response) which is defined in below class.

    Step: 2
    Create DownloadFile.java and put below code:

    package com.bhagwan;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.portlet.ResourceRequest;
    import javax.portlet.ResourceResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
    import com.liferay.portal.util.PortalUtil;
    import javax.portlet.PortletException;
    import com.liferay.util.bridges.mvc.MVCPortlet;
    
    public class DownloadFile extends MVCPortlet
    {
    
    @Override
        public void serveResource(ResourceRequest request, ResourceResponse response)
                throws IOException, PortletException {
            try {
                File file = new File("/casefile.xlsx");
                InputStream in = new FileInputStream(file);
                HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(response);
                HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
                ServletResponseUtil.sendFile(httpReq,httpRes, file.getName(), in, "application/download");
                in.close();
    
            } catch (Exception e) {
    
                e.printStackTrace();
            }       
        }
    
    }
    

    You have any quires, Please comment on blog.
    Thank you

 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: