views:

76

answers:

2

Hi! I'm running a web app that provides a servlet. this servlet opens a pdf file from a Network File System and finally streams it to the requesting browser.

All the pdf files are linearized by adobe lifecycle pdf generator and ready for fast web view.

unfortunately, the fast web view does not work. I guess it's a problem of how to open and stream the file in java code and the setting of response header info. if i deploy a test pdf within my webapp onto the jboss AS and open it directly from the browser by url, the incrementel loading works.

can anyone help me?

Here's the code of my servlet:

response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
    "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-Disposition",
    "inline;filename=" + documentReference);
response.setHeader("Accept-Ranges", "bytes");

File nfsPDF = new File(NFS_DIRECTORY_PATH + documentReference);

FileInputStream fis = new FileInputStream(nfsPDF);
BufferedInputStream bis = new BufferedInputStream(fis);
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[(int) nfsPDF.length()];
while (true) {
   int bytesRead = bis.read(buffer, 0, buffer.length);
   if (bytesRead < 0) {
      break;
   }
   sos.write(buffer, 0, bytesRead);
}
sos.flush();
//... closing...
A: 

I'm not familiar with "PDF fast web view" feature, but in you're code your're first reading the file completely into buffer and then you write it out. The client won't receive anything before the call to sos.flush(). In fact your while loop is obsolete because there will always be just one run.

Maybe you should try to read/write the stuff blockwise.

byte[] buffer = new byte[1024];
while (true) {
   int bytesRead = bis.read(buffer, 0, buffer.length);
   if (bytesRead < 0) {
      break;
   }
   sos.write(buffer, 0, bytesRead);
   sos.flush();
}

sos.flush();
huo73
A: 

Let's see. You want to send a file in parts, right? Then you should check Range header (HTTP Header) and send only bytes in this range. I'm correct?

Plínio Pantaleão
Yes, you are. I searched the web for an online linearized pdf file, called it and read the http headers of requests and responses. I found out that the adobe acrobat reader plugin of a browser starts sending requests on it's own to the server using the header Range with a list of wanted byte ranges of the pdf file. i wrote my servlet to be able to pass such requests and was successful. but unfortunatly, i was only able to just get the first page shown. after some requests it stops requesting data. I'll let you know when i'm through this. Thx!
Markus
Can you post your code?
Plínio Pantaleão