java - Download a zip file using Restful services -
im trying create , download zip file in java using restful service. not working me. please find code below:
@get @path("/exportzip") @produces("application/zip") public response download(@queryparam("dim") final string dimid, @queryparam("client") final string clientid, @queryparam("type") final string type, @context uriinfo ui) { system.out.println("start"); responsebuilder response = null ; string filetype = ""; if(type.equalsignorecase("u")){ filetype = "unmapped"; }else if(type.equalsignorecase("m")){ filetype = "mapped"; } try { byte[] workbook = null; workbook = engineservice.export(dim, client, type); inputstream = new bytearrayinputstream(workbook); fileoutputstream out = new fileoutputstream(filetype + "tmp.zip"); int buffersize = 1024; byte[] buf = new byte[buffersize]; int n = is.read(buf); while (n >= 0) { out.write(buf, 0, n); n = is.read(buf); } response = response.ok((object) out); response.header("content-disposition", "attachment; filename=\"" + filetype + " - " + new date().tostring() + ".zip\""); out.flush(); out.close(); catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } system.out.println("end"); return response.build(); }
this giving me following error: javax.ws.rs.webapplicationexception: com.sun.jersey.api.messageexception: message body writer java class java.io.fileoutputstream, , java type class java.io.fileoutputstream, , mime media type application/zip not found
you haven't added mime type of response . browser confused while processing response. needs response content type. set response content type response, add following code ,
response.setcontenttype("application/zip");
after
response = response.ok((object) out);
thanks.
Comments
Post a Comment