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[] = newbyte[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 ?
// 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[] = newbyte[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"\"");
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.
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!
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.