Results 1 to 4 of 4

Thread: pdf view

  1. #1

    Default pdf view

    I am trying to create a generic view class that is sent the file path and mimetype and the document is diplayed inline for text/pdf/doc etc and downloaded as binary for zip etc.
    It works fine for all types in Firefox but pdfs don't work in IE. It looks like Acrobat Reader loads but the browser stays blank. Here is the code.

    public class GenericDocView extends AbstractView
    {
    final static int BUFFER_SIZE = 4 * 1024; // 4K buffer

    protected void renderMergedOutputModel(Map model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
    {
    String path = (String) model.get("path");
    String mimeType = (String) model.get("mimeType");
    File file = new File(getServletContext().getRealPath(path));
    if(file.exists())
    {
    //returns the doc inline
    httpServletResponse.setContentType(mimeType);
    httpServletResponse.setHeader("Content-Disposition", "inline; filename=" + file.getName());
    ServletOutputStream out = httpServletResponse.getOutputStream();
    InputStream in = null;
    try
    {
    in = new BufferedInputStream(new FileInputStream(file));
    byte[] buf = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = in.read(buf)) != -1)
    {
    out.write(buf, 0, bytesRead);
    }
    }
    finally
    {
    if (in != null) in.close();
    }
    out.flush();
    }
    else
    {
    throw new Exception("File no longer exists on the server.");
    }
    }
    }

  2. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    it's not a Spring issue.

    http://www.lowagie.com/iText/faq.html#msie

    Regards,
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3

    Default

    The above code works fine from a servlet but not as a view. I made these changes and it still doesn't work.

    final static int BUFFER_SIZE = 4 * 1024; // 4K buffer

    protected void renderMergedOutputModel(Map model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
    {
    String path = (String) model.get("path");
    String mimeType = (String) model.get("mimeType");
    File file = new File(getServletContext().getRealPath(path));
    if(file.exists())
    {
    //returns the doc inline
    httpServletResponse.setContentType(mimeType);
    httpServletResponse.setHeader("Content-Disposition", "inline; filename=" + file.getName());
    ServletOutputStream out = httpServletResponse.getOutputStream();
    InputStream in = null;
    try
    {
    in = new BufferedInputStream(new FileInputStream(file));
    byte[] buf = new byte[BUFFER_SIZE];
    int bytesRead;
    int totalBytesRead = 0;

    while ((bytesRead = in.read(buf)) != -1)
    {
    out.write(buf, 0, bytesRead);
    totalBytesRead += bytesRead;
    }
    httpServletResponse.setContentLength(totalBytesRea d);
    out.flush();
    }
    finally
    {
    if (in != null) in.close();
    }
    }
    else
    {
    throw new Exception("File no longer exists on the server.");
    }
    }

  4. #4

    Default

    FYI I was able to get this functionality by putting the code in the controller instead of a view. The code looks something like this.

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    File file = new File(((WebApplicationContext)ctx).getServletContex t().getRealPath(path));
    response.setContentType(mimeType);
    response.setHeader("Content-disposition", "inline; filename=" + file.getName());
    ServletOutputStream out = response.getOutputStream();
    InputStream in = null;
    try
    {
    in = new BufferedInputStream(new FileInputStream(file));
    byte[] buf = new byte[BUFFER_SIZE];
    int bytesRead;
    int totalBytesRead = 0;

    while ((bytesRead = in.read(buf)) != -1)
    {
    out.write(buf, 0, bytesRead);
    totalBytesRead += bytesRead;
    }
    response.setContentLength(totalBytesRead);
    out.flush();
    }
    finally
    {
    if (in != null) in.close();
    }
    return null;
    }

Similar Threads

  1. Replies: 9
    Last Post: Nov 1st, 2005, 10:36 PM
  2. Replies: 0
    Last Post: Aug 29th, 2005, 03:23 PM
  3. PageCompononentListener Hooks...JIRA issue?
    By amcauley in forum Swing
    Replies: 9
    Last Post: Apr 15th, 2005, 06:10 AM
  4. Content Provider vs View Model
    By Martin Kersten in forum Swing
    Replies: 21
    Last Post: Mar 10th, 2005, 02:25 PM
  5. refreshing the view
    By Jurijus Jarmakas in forum Swing
    Replies: 2
    Last Post: Feb 8th, 2005, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •