aspose file tools*
The moose likes Servlets and the fly likes PDF download failing. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "PDF download failing." Watch "PDF download failing." New topic
Author

PDF download failing.

Mi Jo
Greenhorn

Joined: Jan 24, 2011
Posts: 7
Team,

I am not able to download PDF file consistantly from IE8 while in mozilla it is working fine. We are setting Content-Length header and it is coming same for success ful and corrupted download.

When we use 'open' option from IE it fails 1 out of 5 time (failing one is also having proper content length). 'save' option is always working consistantly

Given below is code snippt we tried.

File f = new File("/opt/temp/test.pdf");

InputStream inputStream = null;


try
{

resp.setHeader("Pragma", "public");
resp.setHeader("Content-Transfer-Encoding", "binary");
resp.setHeader("Cache-Control" , "must-revalidate, post-check=0, pre-check=0");
resp.setHeader("Content-Description", "File Transfer");
resp.setHeader("Expires", "0");
resp.setHeader("Content-Length",String.valueOf((int)f.length()));
resp.setContentLength((int)f.length());


os=resp.getOutputStream();

inputStream = new FileInputStream(f);

byte[] data = new byte[(int)f.length()];

inputStream.read(data, 0, (int)f.length());

// Write the response to Servlet output stream
os.write(data);
}

catch (Exception ex)
{
e.printStackTrace();
}
finally
{

try {
if (os != null) {

os.flush();
os.close();
}
if(inputStream != null)
{
inputStream.close();
}
if (f != null)
{
//
boolean deleted = f.delete();
if(!deleted)
{

f.delete();
}


}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1059
    
  10

Your code for reading the file content is flawed since


does not guarantee to read the whole of the file in one go. It returns the number of bytes actually read. To get round this one can wrap the FileInputStream in a DataInputStream and use the readFully() method.

There is no need to read the whole file into memory since one can read the file in chunks and write each chunk straight to the response.
Mi Jo
Greenhorn

Joined: Jan 24, 2011
Posts: 7
Thanks for your response I tried readyFully(byte[]) and readFully(byte[],startoffset,len) from data input stream but unfortunately error is still not resolved I am getting corrupted PDF in IE8.

I am closing data input stream so I believe it should close underlying file stream as well. So I think code is proper.

Given below is code snippet.


Mi Jo
Greenhorn

Joined: Jan 24, 2011
Posts: 7
Regarding chunking I donot want to do it because with chunking enabled we are getting BLANK page that is also not acceptable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PDF download failing.