Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

PDF File Streaming in a JSP

 
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
 
Sheriff
Posts: 10445
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
 
Author and all-around good cowpoke
Posts: 13078
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
 
Marshal
Posts: 67415
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic