In My Struts base Java web application , I have implemented download functionality with pause/resume. let me explain how my download works , actually file (e.g. image file, doc) will be converted to pdf in the server then it will start downloading , then without pause, it has successfully downloaded file and its not corrupted but when I pause and resume it downloads file successfully but it will be corrupted pdf. I can't figure what I am missing . My Code is as below
{ in = new FileInputStream(f);
int DOWNLOAD_BUFFER_SIZE = Integer.parseInt(CacheManagement.getInstance().getSystemPropertyByAlias().get("DOWNLOAD_BUFFER_SIZE").getPropertyValue());
response.setHeader("Accept-Ranges", "bytes");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setBufferSize(DOWNLOAD_BUFFER_SIZE);
// SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
// Date lastModifiedDate = formatter.parse(String.valueOf(f.lastModified()));
Date lastModifiedDate = new Date(f.lastModified());
response.setHeader("ETag", f.length() + "_" + lastModifiedDate.getTime());
response.setDateHeader("Last-Modified", lastModifiedDate.getTime());
String range = request.getHeader("Range");
if (range != null && !range.isEmpty())
{
ArrayList<Range> ranges = getRangeList(range, f.length());
Range r = ranges.get(0);
response.setHeader("Content-Range", "bytes " + r.getStart() + "-" + r.getEnd() + "/" + r.getTotal());
response.setHeader("Content-Length", String.valueOf(r.getLength()));
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
}
else
{
response.setHeader("Content-Length", String.valueOf(f.length()));
response.setHeader("Content-Range", "bytes 0-" + (f.length() - 1) + "/" + f.length());
}
o = response.getOutputStream();
buf = new BufferedInputStream(in);
byte[] dataBytes = new byte[DOWNLOAD_BUFFER_SIZE];
int readed = 0;
while ((readed = buf.read(dataBytes)) != -1)
{
try
{
o.write(dataBytes, 0, readed);
}
catch (Exception e)
{
// System.out.println("-- Client Aborted Download --");
logger.error("-- Client Aborted Download --");
break;
}
}