jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes error outputting a pdf file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "error outputting a pdf file" Watch "error outputting a pdf file" New topic

error outputting a pdf file

clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Trying to open the pdf file mentioned in the code.The init method is called but an error page is dispayed.Where am i going wrong.Plz help.I'm using jsdk2.0 to test.
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletPdf extends HttpServlet

public void doPost (HttpServletRequestrequest,
HttpServletResponse response)
throws ServletException, IOException

BufferedInputStream bis = null;
BufferedOutputStream bos = null;

ServletOutputStream out =
response.getOutputStream ();

String title = "Simple Servlet Output";


String fileURL ="C:/servletxml/melly/ch03.pdf";

response.setHeader("Content-disposition","attachment; filename=" +"Example.pdf" );
try {
URL url = new URL( fileURL );
// Use Buffered Stream for reading/writing.
bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
} catch( MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch( IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
if (bos != null)
Sunil Karumuri

Joined: Oct 20, 2003
Posts: 3
I don't think URL class can be used here. URL is for across the web.
It should if you could rewrite your BufferInputStream Constructor to
bis = new BufferedInputStream(new FileInputStream(fileURL));
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Thanx,will do the needful and get back to u.
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Still getting an error page displayed even after using the FileInputStream stuff.I'm using jsdk2.0 with the servletrunner utility.No probs with that na.Plz help.
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Will I have to use upgraded version of jsdk or will it work with jsdk2.0
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Where am i going wrong.Plz help.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Telling us "error page is dispayed" isn't very helpful. We need to know hte EXACT error you are getting in order to help you figure out what is wrong. Are any exceptions being thrown? If so, show us the stack trace.

My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
subject: error outputting a pdf file
Similar Threads
setting name of requested pdf in browser's title
How to Return a File using Servlet
displaying pdf file
Servlet downloads Excel file ok but also attempts Servlet & JSP files !!
Generating PDF and displaying it ...