Content-Disposition - Trying to open a pdf file from a servlet - getting error 'file not found'

Discussion in 'Java' started by sridevisd@gmail.com, Oct 27, 2005.

  1. Guest

    Hi,

    I am developing an application using WSAD.

    I have to open a pdf file from a servlet.

    My code goes this way:

    public void doPost(HttpServletRequest req, HttpServletResponse
    response)
    throws ServletException, IOException {

    response.setContentType("application/pdf");
    response.setHeader("Content-disposition",
    "attachment; filename=\"" +"example.pdf" +"\"");

    try
    {
    /*PrintWriter out = response.getWriter();
    out.println("Welcome to Acrobat Reader");
    out.flush();
    out.close();*/
    }catch (IOException ioe)
    { System.out.println(ioe);
    }catch(Exception e){
    System.out.println(e);
    }

    }
    The Acrobat Reader is up but gives the message:
    "There was an error opening this document. This file cannot be found"

    Where should I place the 'example.pdf' file so it can read it.

    2. How can I open the Acrobat Reader inside the browser, instead of an
    attachment.
    if I uncomment the out.println, or use ServletOutputStream it is
    writing the output in the browser and not in a pdf file.

    Pls help.

    Thanks in advance

    Sridevi
     
    , Oct 27, 2005
    #1
    1. Advertising

  2. Dave Glasser Guest

    wrote on 27 Oct 2005 14:57:12 -0700 in
    comp.lang.java.programmer:

    >Hi,
    >
    >I am developing an application using WSAD.
    >
    >I have to open a pdf file from a servlet.
    >
    >My code goes this way:
    >
    >public void doPost(HttpServletRequest req, HttpServletResponse
    >response)
    > throws ServletException, IOException {
    >
    > response.setContentType("application/pdf");
    > response.setHeader("Content-disposition",
    > "attachment; filename=\"" +"example.pdf" +"\"");
    >
    > try
    > {
    > /*PrintWriter out = response.getWriter();
    > out.println("Welcome to Acrobat Reader");
    > out.flush();
    > out.close();*/
    > }catch (IOException ioe)
    > { System.out.println(ioe);
    > }catch(Exception e){
    > System.out.println(e);
    > }
    >
    >}
    >The Acrobat Reader is up but gives the message:
    >"There was an error opening this document. This file cannot be found"
    >
    >Where should I place the 'example.pdf' file so it can read it.


    The way you're doing it above, you need to read the PDF file from the
    disk and write it to the output stream yourself. You're only writing a
    string of text and then closing the connection, which is what's
    causing the "cannot be found" error.

    >
    >2. How can I open the Acrobat Reader inside the browser, instead of an
    >attachment.


    Use "inline; filename=example.pdf" as the Content-Disposition header.

    If the example.pdf file exists somewhere where it can be served from a
    simple URL, rather than being generated dynamically, then you can
    redirect the browser to it with HttpServletResponse.sendRedirect().


    --
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net

    If you're a musician, check out RPitch Relative Pitch
    Ear Training Software.

    http://rpitch.sourceforge.net
     
    Dave Glasser, Oct 27, 2005
    #2
    1. Advertising

  3. Guest

    Re: Content-Disposition - Trying to open a pdf file from a servlet - getting error 'file not found'

    Thanks Dave. The 'inline'(inline; filename=\""+"example.pdf"+"\"")
    worked when I read the pdf file and wrote to the output stream.

    But the 'attachment' (attachment; filename=\"" +"example.pdf" +"\"")
    is not working still.

    Thanks
    Sridevi
     
    , Oct 28, 2005
    #3
  4. Dave Glasser Guest

    Re: Content-Disposition - Trying to open a pdf file from a servlet - getting error 'file not found'

    wrote on 28 Oct 2005 13:58:01 -0700 in
    comp.lang.java.programmer:

    >Thanks Dave. The 'inline'(inline; filename=\""+"example.pdf"+"\"")
    >worked when I read the pdf file and wrote to the output stream.
    >
    >But the 'attachment' (attachment; filename=\"" +"example.pdf" +"\"")
    >is not working still.


    Does the HTML link that invokes the URL have a target that opens
    another browser window? For example, "<a target='_blank' href='...'"
    or "<form target='_blank'? If so, I've encountered problems with IE
    under WinXP SP2 when trying to download something as an attachment
    that way. You have to make sure that whenever you're using
    "attachment" that the target is the same browser window (the default).
    It won't wipe out the contents, but rather should just bring up the
    save dialog.

    --
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net

    If you're a musician, check out RPitch Relative Pitch
    Ear Training Software.

    http://rpitch.sourceforge.net
     
    Dave Glasser, Oct 28, 2005
    #4
  5. spatil02

    Joined:
    Jan 30, 2012
    Messages:
    1
    try this ,
    HttpServletResponse response = waa.getHttpServletResponse();
    ServletOutputStream out = response.getOutputStream();

    response.reset();
    response.resetBuffer();
    response.setContentType( ​
    "application/octet-stream" );

    response.setHeader(​
    "Content-disposition", "inline;filename=anyfilename.pdf" );

    response.setHeader(
    "Cache-Control", "max-age=0");
    response.setContentLength((
    int)file.length());

    // bos = new BufferedOutputStream(out);


    FileInputStream fileInputStream = ​
    new FileInputStream(file);
    byte[] buff = new byte[(int) file.length()];
    fileInputStream.read(buff);

    out.write(buff);
     
    spatil02, Jan 30, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Schmid

    Dynamic Content-Disposition file naming.

    Martin Schmid, May 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    526
    Martin Schmid
    May 7, 2004
  2. ben
    Replies:
    0
    Views:
    1,069
  3. Sergio Juan
    Replies:
    1
    Views:
    1,024
    mohammad.h.ashmawi
    Apr 29, 2011
  4. siarsky
    Replies:
    1
    Views:
    813
    Jol Padgett
    Feb 10, 2004
  5. Doug McCann
    Replies:
    1
    Views:
    1,037
    William Brogden
    Aug 5, 2004
Loading...

Share This Page