• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error outputting a pdf file

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where am i going wrong.Plz help.
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic