Sending Files To Browser in JSP

Discussion in 'JSP' started by pradeep, Feb 21, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    <%
    // fetch the file
    String filename = "companySecret.txt";
    String filepath = "C:\\";
    
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + '"');
    
    java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);
    
    int i;
    while ((i=fileInputStream.read()) != -1) 
    {
        out.write(i);
    }
    
    fileInputStream.close();
    out.close();
    %>
     
  2. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
  3. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    Hi,
    I think it is a stupid problem. But I do not know how it can be cracked:
    I want need the user to be able to download a file that is stored somewhere in the server.
    I found some solutions like:
    http://wiki.apache.org/myfaces/Sending_Files
    If I integrate this code in my action or actionListener method it does not works.
    The reason is that the result for the response
    HttpServletResponse response =
    ( HttpServletResponse ) context.getExternalContext().getResponse();
    is always null.
    Have anyone a tip?
    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice