This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying pdf file

 
clyde melly
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change content type header to application/pdf
ie use
response.setContentType("application/pdf");
 
clyde melly
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same error page displayed when using (application/pdf)
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me if this is a stupid question but are you invoking the servlet with a GET or POST request?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic