• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PDF download failing.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic