participate


JavaServer Pages (JSP) and JSTL - cannot open pdf from a link in web page
<<   Back to Forum  |   Give us Feedback
This topic has 8 replies on 1 page.
pepgrifell
Posts:63
Registered: 2/10/01
cannot open pdf from a link in web page   
Aug 14, 2003 2:38 AM

 
Hi,
I have a link in a web page that gets a Blob(pdf file) from Oracle database and returns the pd file.

The code in my jsp is :
// I got this from an Action (I?m using Struts)
java.sql.Blob file=(java.sql.Blob)request.getAttribute("PDFfile");
String filename=(String)request.getAttribute("filename");
try{
  int iLength = (int)(file.length());
  response.setHeader("Cache-Control","no-cache");
  response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");
  response.setContentType("application/pdf");
  response.setContentLength(iLength);
  ServletOutputStream os = response.getOutputStream();
  InputStream in = null;
  in = file.getBinaryStream();
  byte buff[] = new byte[1024];
  while (true) {
     int i = in.read(buff);
     if (i<0) break;
       os.write(buff,0,i);
  }
  os.flush();
  os.close();
} catch(Exception ex){
  out.println("Error while reading file : " + ex.getMessage());
}


When I click the link, a popup windows asks if I want to save the file or open it. If I save it, there is no problem, I can open the file and see the content but if I try to open the content, I get an error in Acrobat Reader ("error opening the document.File not found"). Does I need to have a "physical file" to be able to open it ?

Another question : What I have to do to open the pdf file in the browser and not in the acrobat reader ? I have IE 6.0 and acrobat reader 6.0. Doeas I need to put some pluggin in IE ?

Thanks !
 
pepgrifell
Posts:63
Registered: 2/10/01
Re: cannot open pdf from a link in web page   
Aug 14, 2003 3:37 AM (reply 1 of 8)  (In reply to original post )

 


// I got this from an Action (I?m using Struts)
java.sql.Blob file=(java.sql.Blob)request.getAttribute("PDFfile");
String filename=(String)request.getAttribute("filename");
try{ 
  int iLength = (int)(file.length());
  response.setHeader("Content-type", "application/pdf");    
  response.setHeader("Content-Disposition", "inline; filename=\""+filename+"\"");
  response.setHeader("Expires","0"); 
  response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
  response.setHeader("Pragma","public");
  response.setContentLength(iLength);
  ServletOutputStream os = response.getOutputStream();
  InputStream in = null;
  in = file.getBinaryStream();
  byte buff[] = new byte[1024];
  while (true) {
      int i = in.read(buff);
      if (i<0) break;       
      os.write(buff,0,i);  
  }
  os.flush();
  os.close();
} catch(Exception ex){ 
   out.println("Error while reading file : " + ex.getMessage());
}


and now it?s running !!! I?m not using response.setContentType(...) and I do this in response.setHeader("Content-type", "application/pdf"). And I use response.setHeader("Content-Disposition", "inline; filename=\""filename"\"") instead of response.setHeader("Content-Disposition", "attachment; filename=\""filename"\"");
 
arthor
Posts:5
Registered: 7/23/00
Re: cannot open pdf from a link in web page   
Aug 25, 2005 7:13 AM (reply 2 of 8)  (In reply to #1 )

 
Tons of thanks!

It really works!

**** DO NOT use response.setContentType(....), use response.setHeader("Content-type", "application/pdf")
 
masoodmjan
Posts:204
Registered: 9/7/01
Re: cannot open pdf from a link in web page   
Aug 25, 2005 7:20 AM (reply 3 of 8)  (In reply to #2 )

 
I think its nothing to do with response.setContentType
I suspect its the
response.setHeader("Cache-Control","no-cache");
which caused it to fail. This no-cache would delete the file once it is downloaded in temp dir and thats why the Acrobat reader throws no file error.
 
sittharthy3@yahoo.co.in
Posts:1
Registered: 10/13/05
Re: cannot open pdf from a link in web page   
Oct 13, 2005 9:06 PM (reply 4 of 8)  (In reply to original post )

 
i cant open the .pdf file in my adope reader
 
Takaitra
Posts:2
Registered: 8/23/06
Re: cannot open pdf from a link in web page   
Aug 23, 2006 7:36 AM (reply 5 of 8)  (In reply to #3 )

 
I think its nothing to do with
response.setContentType
I suspect its the
response.setHeader("Cache-Control","no-cache");
which caused it to fail. This no-cache would delete
the file once it is downloaded in temp dir and thats
why the Acrobat reader throws no file error.

Thanks much Masoodmjan. I was having the same "file not found" error when opening PDFs. Removing response.setHeader("Cache-Control","no-cache") fixed the problem!
 
Srinivas@sandesh
Posts:2
Registered: 3/28/07
Re: cannot open pdf from a link in web page   
Jun 29, 2007 2:13 AM (reply 6 of 8)  (In reply to #2 )

 
urs code is working cutely
 
zeal
Posts:2
Registered: 2/5/08
Re: cannot open pdf from a link in web page   
Feb 19, 2008 4:42 AM (reply 7 of 8)  (In reply to #1 )

 
Hey Thanks a lot for the wonderful tip.. You really saved me...
Thanks again...:-)
 
Ruvin
Posts:1
Registered: 3/5/08
Re: cannot open pdf from a link in web page   
Mar 5, 2008 4:07 AM (reply 8 of 8)  (In reply to #5 )

 
I think its nothing to do with
response.setContentType
I suspect its the
response.setHeader("Cache-Control","no-cache");
which caused it to fail. This no-cache would delete
the file once it is downloaded in temp dir and thats
why the Acrobat reader throws no file error.




Thank you so much for this really useful information. It helped me a lot in debugging my application. Thanks a lot. Hope such important suggestions will continue to prove helpful for me in future as well.
 
This topic has 8 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums