Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error outputting a pdf file

 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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";

response.setContentType("application/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();
}
}
}
 
Sunil Karumuri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx,will do the needful and get back to u.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will I have to use upgraded version of jsdk or will it work with jsdk2.0
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where am i going wrong.Plz help.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Brian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic