java - Why my byte[] gets UTF-8 encoded by HttpServletResponse? -
i have unusual problem while downloading file spring controller, first of here's code:
@requestmapping(value = "/id/{fileid}", method = requestmethod.get) public void getfile(@pathvariable("fileid") string fileid, httpservletresponse response) { // check if file exists if (files.containskey(fileid)) { response.reset(); // storedfile contains name, extension , content (byte[]) of file storedfile file = files.remove(fileid); response.setcontenttype("application/pdf"); response.setheader("content-disposition", "attachment; filename=" + file.getfullname()); byte[] payload = file.getcontent(); response.setcontentlength(payload.length); // writing file on disk file f = new file("/" + fileid + ".pdf"); seek { filecopyutils.copy(payload, f); } grab (ioexception e) { e.printstacktrace(); } // send same payload client download seek { response.getoutputstream().write(payload); } grab (ioexception ex) { throw new runtimeexception("ioerror writing file output stream"); } } }
so, retrieve stored file, save content on disk , send same content client download, here's unusual thing: 2 files different!
the saved file ok (it's jasper study pdf).
the downloaded file displays nothing, , looking within binary editor found "ef bf bd" byte sequence displays utf-8 unrecognised character.
i don't understand when content of byte[] interpreted utf-8 , why byte sequence inserted; should set encoding somewhere if i'm writing raw bytes within output stream?
i tried set response charactr encoding different encoding no alter in result, hadn't expected since i'm not transferring text...
any idea?
the code works! colleague downloaded branch , run application... downloaded file ok. tried pass code colleague , deploy on test server, getting positive results. issue must in workspace or machine setting, sure not in code.
java spring servlets encoding jasper-reports
No comments:
Post a Comment