Java - Make byte array as a download -


i have zip file byte array (byte[]), write file system using,

        fileoutputstream fos = new fileoutputstream("c:\\test1.zip");         fos.write(decodedbytes);  // decodedbytes zip file byte array          fos.close();             

instead of writing file , reading make download, make byte array download directly, tried this,

    response.setcontenttype("application/zip");     response.setheader("content-disposition", "attachment; filename=\"file.zip\"");     servletoutputstream outstream = response.getoutputstream();     outstream.write(decodedbytes);  // decodedbytes zip file byte array  

this not working, i'm getting empty file. how can make byte array download?

update: added clause , closed servletoutputstream , worked.

    }catch (exception e) {         log.error(this, e);     } {         try{             if (outstream != null) {                 outstream.close();                           }         } catch (ioexception e) {             log.error(this, "download: error during closing resources");         }     } 

pankaj solution works.

try following:

servletoutputstream outstream = response.getoutputstream(); response.setcontenttype("application/zip"); response.setheader("content-disposition", "attachment; filename="data.zip""); outstream.write(decodedbytes); outstream.flush(); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -