This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PDF download failing.

 
Mi Jo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding chunking I donot want to do it because with chunking enabled we are getting BLANK page that is also not acceptable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic