• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

PDF File Streaming in a JSP  RSS feed

 
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: 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
 
Author and all-around good cowpoke
Rancher
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
 
Author and ninkuma
Marshal
Posts: 66787
168
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!