File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes PDF File Streaming in a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "PDF File Streaming in a JSP" Watch "PDF File Streaming in a JSP" New topic
Author

PDF File Streaming in a JSP

ravi D shankar
Ranch Hand

Joined: May 27, 2005
Posts: 41
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

Joined: Jul 20, 2005
Posts: 9329
    
110

Post your code.


[My Blog] [JavaRanch Journal]
ravi D shankar
Ranch Hand

Joined: May 27, 2005
Posts: 41
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

Joined: May 27, 2005
Posts: 41
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

Joined: Mar 22, 2000
Posts: 12682
    
    5
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


Java Resources at www.wbrogden.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Listen to William for he is wise. This article might help you understand why serving up a binary resource with a JSP is folly.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ravi D shankar
Ranch Hand

Joined: May 27, 2005
Posts: 41
Thanks very much .. did exactly as you told and its working fine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PDF File Streaming in a JSP
 
Similar Threads
Showing PDF in a new window
serving images - mappings question
setting No Cache for JSP
Javascript alert from PDF in JSP file
Cache file(pdf) in user's computer using Servlet-Cookie, or Session