File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes displaying pdf file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "displaying pdf file" Watch "displaying pdf file" New topic
Author

displaying pdf file

clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
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";

response.setContentType("text/pdf");


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)
bis.close();
if (bos != null)
bos.close();
}
}
}
Displays an error page.Init is called.Kindly help.
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
change content type header to application/pdf
ie use
response.setContentType("application/pdf");
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
same error page displayed when using (application/pdf)
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Forgive me if this is a stupid question but are you invoking the servlet with a GET or POST request?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
Don't get me started about those stupid light bulbs.
 
subject: displaying pdf file
 
Similar Threads
error outputting a pdf file
setting name of requested pdf in browser's title
How to Return a File using Servlet
Generating PDF and displaying it ...
Servlet downloads Excel file ok but also attempts Servlet & JSP files !!