Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PDF File Streaming in a JSP

 
ravi D shankar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I have a websphere server app context on a Solaris machine.
From a jsp I am calling a method in my code which converts a file into PDF and returns a BufferedInputStream ... using the BIS I am writing the file onto OutputStream in the jsp ... but I am not able to open the Pdf File ...its giving the error something like "document is damaged ".

The PDF file created is not a part of the context ...it is located in some other /tmp folder on the server separate from context folders... Please tell me a way to stream that file on the clients browser using a "jsp".

Sorry , if this is not supposed to be asked in this topic .

Thanks
Ravi
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code.
 
ravi D shankar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below is the jsp code I am using.
The pdffile is getting created perfectly in the server folder "/tmp".
But while opening it in the browser I am getting the error "The file is damaged and couldnt be repaired".

<%
try
{
BufferedInputStream bis = PDFFile.getPDFFile();
OutputStream out1 = (OutputStream)response.getOutputStream ();
response.setContentType( "application/pdf" );
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(out1);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch(final MalformedURLException e) {
out.println ( "MalformedURLException." );
throw e;
} catch(final IOException e) {
out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}

}
}
catch(Exception e)
{
out.println("Exception occured "+e);
throw e;
}

%>
 
ravi D shankar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
Getting this error while executing that jsp

"Server caught unhandled exception from servlet [JSP 1.1 Processor]: OutputStream already obtained"
com.ibm.servlet.engine.webapp.UncaughtServletException: Server caught unhandled exception from servlet [JSP 1.1 Processor]: OutputStream already obtained
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your PDF needs to be served as a binary data stream, JSP assumes it is sending HTML characters. Use a servlet NOT jsp - it really is not that hard.
Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listen to William for he is wise. This article might help you understand why serving up a binary resource with a JSP is folly.
 
ravi D shankar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much .. did exactly as you told and its working fine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic