rest - How download file using java spark? -
i want write simple rest api file download.
i cant find docs understood need set mimetype='application/zip'
response, not clear how return stream.
update: resolved here example code:
public static void main(string[] args) { //setport(8080); get("/hello", (request, responce) -> getfile(request,responce)); } private static object getfile(request request, response responce) { file file = new file("myfile"); responce.raw().setcontenttype("application/octet-stream"); responce.raw().setheader("content-disposition","attachment; filename="+file.getname()+".zip"); try { try(zipoutputstream zipoutputstream = new zipoutputstream(new bufferedoutputstream(responce.raw().getoutputstream())); bufferedinputstream bufferedinputstream = new bufferedinputstream(new fileinputstream(file))) { zipentry zipentry = new zipentry(file.getname()); zipoutputstream.putnextentry(zipentry); byte[] buffer = new byte[1024]; int len; while ((len = bufferedinputstream.read(buffer)) > 0) { zipoutputstream.write(buffer,0,len); } } } catch (exception e) { halt(405,"server error"); } return null;
what need similar this thread. need close outputstream , return raw httpservletresponse:
try { ... zipoutputstream.flush(); zipoutputstream.close(); } catch (exception e) { halt(405,"server error"); } return responce.raw();
Comments
Post a Comment