java - Writing multiple images to ServletOuputStream? -


i've requirement multiple images web service(ex:list) , have write images servletoutputstream.

when click on 'view' link invokes servlet , servlet invokes webservice , receives multiple images list.

now i'm trying write images servletoutputstream not working..

trying send images zip

response.setcontenttype("application/zip");   outputstream os = null; bufferedoutputstream bos = null; zipoutputstream zos = null;      try{     os = resp.getoutputstream();     bos = new bufferedoutputstream(os);      zos = new zipoutputstream(bos);     zos.setlevel(zipoutputstream.stored);      sendmultiplefiles(zos, annotcontent,"display"); }catch (ioexception e) {     resp.setstatus(httpservletresponse.sc_internal_server_error); } {     if (zos != null) {         zos.finish();         zos.flush();     }     bos.close();     os.close(); }  private void sendmultiplefiles(zipoutputstream zos, collection<byte[]> filestosend, string name) throws ioexception {     mylogger.info("sendmultiplefiles invoked..");     for(byte[] f: filestosend) {          inputstream instream = null;         zipentry ze = null;          try {             instream = new bytearrayinputstream(f);              ze = new zipentry(name + "-archived");             ze.setcomment("dummy file");              zos.putnextentry(ze);             int readbyte = 0;             while((readbyte = instream.read()) != -1)             {                 zos.write(readbyte);             }         } catch (ioexception e) {             system.out.println("cannot find " );         } {             if (ze != null) {                 zos.closeentry();             }             instream.close();          }     } 

above code not working..any suggestions appreciated..

your solution depend on content-type serving - if serving html response issue can resolved either storing images on disk (outside context root , writing image display endpoint) or if images small - sending bytes out base64 encoding in image tag directly.

if response type not html - here options - return zip file.

less standard way of using servlet multipart mime extension.

edit: per comment mime type jpeg - 1 option combine jpeg 1 larger 1 (cannot send multiple out). combining bytes of binary file - jpeg has it's own format compression along header, footer , exif info - may need specialized apis combine them one. here 1 came across. if combining jpg not option - trying achieve cannot done in 1 write.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -