Discussions

Web tier: servlets, JSP, Web frameworks: Opening pdf using jsp

  1. Opening pdf using jsp (3 messages)

    I am writing an application where a user can download files. These files can be of type pdf, doc, xls, csv or ppt. The jsp which has the links to the files calls a DownloadServlet which fetches the file and writes it to the outout stream. These files are not located under the WEB-INF folder but on another directory (eg. C:\downloads).
    When the user clicks on the link, it pops up a box which asks if they want to Save or Open the file. All files work fine when they are downloaded. When user tries to open a pdf file, it opens up Adobe Reader but then gives an error message-
    "There was an error opening this document. This file cannot be found."
    The doc & xls files open up fine.

    Here is the code to my download servlet -

    public class DownloadServlet extends HttpServlet {
        
        //Initialize global variables
        String fileName="";
        
        private static Logger log = Logger.getLogger(DownloadServlet.class.getName());
        
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
        }
        
        //Process the HTTP Get request
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                IOException {
            doPost(request,response);
        }
        
        //Process the HTTP Post request
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            log.debug("Entered DownloadServlet");
            OutputStream outStream = response.getOutputStream();
            fileName=request.getParameter("fileName");
            log.debug("Filename:"+fileName);
            String filePath = "C:\\test
    reference-material";
            File f=new File(filePath, fileName);
            String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
            log.debug("Filetype:"+fileType+";"+f.length());

            if (fileType.trim().equalsIgnoreCase("txt")) {
                response.setContentType( "text/plain" );
            } else if (fileType.trim().equalsIgnoreCase("doc")) {
                response.setContentType( "application/msword" );
            } else if (fileType.trim().equalsIgnoreCase("xls")) {
                response.setContentType( "application/vnd.ms-excel" );
            } else if (fileType.trim().equalsIgnoreCase("pdf")) {
                response.setContentType( "application/pdf" );
                log.debug("content type set to pdf");
            } else {
                response.setContentType( "application/octet-stream" );
            }

            response.setContentLength((int)f.length());
             response.setHeader("Content-Disposition","attachment; filename=\"SecurityPatterns.pdf\"");

            response.setHeader("Cache-Control", "no-cache");
            byte[] buf = new byte[8192];
            FileInputStream inStream = new FileInputStream(f);
            int sizeRead = 0;
            while ((sizeRead = inStream.read(buf, 0, buf.length)) > 0) {
                log.debug("size:"+sizeRead);
                outStream.write(buf, 0, sizeRead);
            }
            inStream.close();
            outStream.close();
            
        }
        
        //Get Servlet information
        public String getServletInfo() {
            return "DownloadServlet Information";
        }
    }

    Threaded Messages (3)

  2. Download component[ Go to top ]

    Check out Download servlet in JSOS:
    http://www.servletsuite.com/servlets.htm
  3. Download component[ Go to top ]

    Thanks, that works great.
    Although, I am curious to find out what I was doing wrong.
  4. Re: Opening pdf using jsp[ Go to top ]

    The following fixed the problem for me. response.setHeader("Cache-Control", "max-age=60"); Another possible solution is to change the content type from "application/pdf" to "application/octet-stream".