Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
sridevisd@gmail.com - 27 Oct 2005 22:57 GMT
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
Dave Glasser - 27 Oct 2005 23:08 GMT
sridevisd@gmail.com wrote on 27 Oct 2005 14:57:12 -0700 in
comp.lang.java.programmer:

>Hi,
>
[quoted text clipped - 29 lines]
>
>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().

Signature

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

sridevisd@gmail.com - 28 Oct 2005 21:58 GMT
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
Dave Glasser - 28 Oct 2005 22:21 GMT
sridevisd@gmail.com 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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.