my dog learned polymorphism
The moose likes Servlets and the fly likes pdf and binary files - URGENT Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "pdf and binary files - URGENT" Watch "pdf and binary files - URGENT" New topic
Author

pdf and binary files - URGENT

Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
Hi,
I have some pdf and txt reports stored in oracle database as bfiles. I am retrieving it using a servlet. I could retrieve the text reports without any problem. When I select pdf, pdf reader opens and comes with blank pages. It is showing the actual number of pages though. I am sending the mime header to application/pdf. IF I use outputstream instead of printwriter, I am getting blank pages and it is not opening adobe reader. When I make slight changes in the code again, I am getting a dialog box asking whether to save it or open it. If I say open it, it opens up a notepad with garbage(bytes) in it. Could anybody suggest a way out from this and help me to see my actual pdf file.
Thanks
beksy98@yahoo.com
Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
I have the same problem with downloading XL file.
Any ideas are would be appreciated.
If I find the solution first I will post it here.
Alex
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
Any answer, please!
Mac Fraser
Greenhorn

Joined: Jul 13, 2001
Posts: 2
I have found when sending the pdf to the browser be very careful to ensure that you have included \n\n after the content type
Mac
edit --
But I have had success with this on most browsers
response.setHeader("Content-Type","application/pdf");
[This message has been edited by Mac Fraser (edited July 13, 2001).]
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
Thanks Fraser. I still am not getting anything. Below is the code
Please help!
static void pdfBfile(Connection myConnection,BFILE bfile,HttpServletRequest request, HttpServletResponse response)
throws Exception{
response.setHeader("Content-Type","application/pdf");
OracleCallableStatement read =
(OracleCallableStatement)
myConnection.prepareCall ("begin dbms_lob.read (?, ?, ?, ?); end;");

bfile.openFile();

InputStream instream = bfile.getBinaryStream();

int pos =0;
int len ;
byte[] buf = new byte[1024];
while ((len = instream.read(buf)) != -1)
{
for (int i=0; i<len; i++)
dataOutput.write(buf,i,len);
}
instream.close();
dataOutput.close();
bfile.closeFile();
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
I missed one line in the previous post, ...defining outputstream
here it is
OutputStream dataOutput = response.getOutputStream();
Anybody, help!
Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
I did find a solution.
Here is my Servlet. The servlet gets invoked when a user tries to access an excel file. The servlet checks permissions and ,reads the excel file and then displays it to the user.
Here is the display part
ServletOutputStream out = resp.getOutputStream();
String path = req.getParameter("Path");
FileInputStream fis = new FileInputStream(path);
<b>resp.setContentType("application/vnd.ms-excel");</b> //this is important so the browser knows to open Excel
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int l = 0;
while ( (l = bis.read(buffer) ) != -1)
out.write(buffer);
bis.close();
out.flush();
out.close();
}
Also make sure that the server has the MIME type defined for your binary files.

[This message has been edited by Aleksey Matiychenko (edited July 13, 2001).]
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
Hi Aleksey,
I don't think that solution will work for me? or will it? This files are stored in oracle as bfiles. I don't see any methods to get the correct path of the bfile. I see a getName and getDirAlias, not the full path. Any ideas?
Anybody, this is URGENT!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12271
    
    1
Sometimes with binary data it is important to set the content-length header or the browser plugin won't understand when the transmission is done.
Bill

Java Resources at www.wbrogden.com
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
This problem might be browser dependent. We had a similar problem at work a few months ago. I know one of the browsers did not load a PDF file unless it's more than a specific size. I just can't remember how they solved it.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
setting the length didn't help. I am using IE ( 4&5) and JRUN 2.3.3.
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
Finally!!!it worked!!!
Here is what I did if anybody is interested. Thanks to everybody who responded.
static void pdfBfile(Connection myConnection,BFILE bfile,HttpServletRequest request, HttpServletResponse response)
throws Exception{

response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setContentType("application/pdf");
ServletOutputStream out = response.getOutputStream();

OracleCallableStatement read =
(OracleCallableStatement)
myConnection.prepareCall ("begin dbms_lob.read (?, ?, ?, ?); end;");

bfile.openFile();
InputStream instream = bfile.getBinaryStream();

int pos =0;
int len ;
byte[] buf = new byte[1024];

while ((len = instream.read(buf)) != -1)
{
out.write(buf,0,len);
}
instream.close();
out.close();
bfile.closeFile();
}
Anuj Anand
Ranch Hand

Joined: Nov 18, 2000
Posts: 96
Can u help and tell me that if input file is PDF and file has to be moved onto server what should i do..I think my problem is kinda similar to yours..Code would be really helpful
Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
I am not sure whether I understood your question correctly, Anuj. All our text files and pdf files are on the server. We are just storing the pointers in the database as bfiles(datatype).
beksy98@yahoo.com
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: pdf and binary files - URGENT
 
Similar Threads
setting temporary text while window loads
Opening pdf from html
PDF Byte Array to video output stream
Error while opening a pdf file in IE7
Jasperreports PDF