java - File download from the browser via DB has extra bytes added causing corruption messages from microsoft applications -
<% document downloadfile = null; string mimetype = null; try{ downloadfile = new documentsdao().loadbyid(long.parselong(request.getparameter("id"))); // gets mime type of file mimetype = downloadfile.getfiletype(); if (mimetype == null) { // set binary type if mime mapping not found mimetype = "application/octet-stream"; } system.out.println("mime type: " + mimetype); }catch (exception e){ return; } // modifies response response.reset(); response.resetbuffer(); response.setcontenttype(mimetype); response.setcontentlength((int) downloadfile.getdocumentdata().length); // forces download string headerkey = "content-disposition"; string headervalue = string.format("attachment; filename=\"%s\"", downloadfile.getfilename()); response.setheader(headerkey, headervalue); // obtains response's output stream outputstream outstream = response.getoutputstream(); byte[] buffer = new byte[8192]; int bytesread = -1; system.out.println("### length db = "+downloadfile.getdocumentdata().length); bytearrayinputstream instream = new bytearrayinputstream(downloadfile.getdocumentdata()); while ((bytesread = instream.read(buffer)) != -1) { outstream.write(buffer, 0, bytesread); } instream.close(); outstream.close(); response.flushbuffer(); return; %>
the above code in jsp produces file download has additional sequence of 0d0a x 4 @ end causes microsoft applications word , excel complain , have repair file has been downloaded.
i thought might upload of file not, , retrieving database fine. input stream fine problem occurs after output stream closed.
errors 'word found unreadable content' 'excel found unreadable content'
has seen this?
cheers
charlie
the conversion of jsp servlet introduced 0d0a x 4 characters. used fiddle find apache tomcat web server altering content length , sending bytes. looked @ working apps @ work , used servlets job converted above code servlet , worked perfectly.
so don't use jsps purpose.
Comments
Post a Comment