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

S

sridevisd

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
 
D

Dave Glasser

(e-mail address removed) 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
 
S

sridevisd

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
 
D

Dave Glasser

(e-mail address removed) 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
 
Joined
Jan 30, 2012
Messages
1
Reaction score
0
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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top