javascript - file download angularjs and servlets -
i have image stored in database table, primary key particular number. image below database folder of java project means of servlet. far, good.
my problem need download image user , can not it.
my steps follows:
jsp:
$scope.downloadfile = function(){ var tkact = $scope.ticketactual.tknum; var param = { nrotk: tkact }; var res = $http.post($scope.testhost +"/downloadattachment",json.stringify(param)); }); res.error(function(data, status, headers, config) { alert("failure message: " + json.stringify({data: data})); }); }
servlet:
@webservlet("/downloadattachment") public class downloadattachment extends httpservlet { // size of byte buffer send file private static final int buffer_size = 4096; private final int bytes_download=1024; public static final string file_separator = system.getproperty("file.separator"); @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession sess = request.getsession(); controller ctrl = controller.get(); servletcontext sc = getservletcontext(); //int nroticket=-1; //nroticket = 104; system.out.println(request.getparameter("nrotk")); system.out.println(request.getattribute("nrotk")); jsonobject joparam = getparametrosjo(request); long lnrotk = (long) joparam.get("nrotk"); int nroticket = lnrotk.intvalue(); vector<string> vnamesfile = ctrl.getfile(nroticket,sc.getrealpath("/downloads")); if(vnamesfile.size()==1){ string archivo = vnamesfile.get(0); file downloadfile = new file(archivo); string nombrefile = getnombrefileenvio(nroticket,downloadfile.getname()); // if want use relative path context root: string relativepath = getservletcontext().getrealpath("/downloads/"); system.out.println("relativepath = " + relativepath); // obtains servletcontext servletcontext context = getservletcontext(); string mimetype = context.getmimetype(nombrefile); if (mimetype == null) { mimetype = "application/octet-stream"; } response.setcontenttype(mimetype); response.setheader("content-disposition","attachment;filename="+nombrefile); system.out.println("obteniendo el stream..."); system.out.println("nombre del file es: "+nombrefile); inputstream = sc.getresourceasstream("/downloads/" + downloadfile.getname()); int read=0; byte[] bytes = new byte[bytes_download]; servletoutputstream out; out = response.getoutputstream(); fileinputstream fin = new fileinputstream(relativepath+downloadfile.getname()); bufferedinputstream bin = new bufferedinputstream(fin); bufferedoutputstream bout = new bufferedoutputstream(out); int ch =0; ; while((ch=bin.read())!=-1) { bout.write(ch); } bin.close(); fin.close(); bout.close(); out.close(); }else{ byte[] zip = zipfiles(getservletcontext().getrealpath("/downloads/"),vnamesfile, 95); servletoutputstream sos = response.getoutputstream(); response.setcontenttype("application/zip"); response.setheader("content-disposition", "attachment; filename=adjuntosticket95" + ".zip"); sos.write(zip); sos.flush(); } }
private jsonobject getparametrosjo(httpservletrequest request) throws ioexception{ stringbuilder buffer = new stringbuilder(); bufferedreader joparam = request.getreader(); string texto=""; string str = null; while ((str = joparam.readline()) != null) { buffer.append(str); //texto+=str; } texto = buffer.tostring(); system.out.println(texto); if(!texto.equalsignorecase("")){ jsonobject obj = jsonobject.parse(texto); return obj; }else{ return null; } } private byte[] zipfiles(string path, vector<string> vnamesfile, int nrotk) throws ioexception { bytearrayoutputstream baos = new bytearrayoutputstream(); zipoutputstream zos = new zipoutputstream(baos); byte bytes[] = new byte[2048]; (string filename : vnamesfile) { fileinputstream fis = new fileinputstream(filename); bufferedinputstream bis = new bufferedinputstream(fis); zos.putnextentry(new zipentry(getnombrefileenvio(nrotk,filename))); int bytesread; while ((bytesread = bis.read(bytes)) != -1) { zos.write(bytes, 0, bytesread); } zos.closeentry(); bis.close(); fis.close(); } zos.flush(); baos.flush(); zos.close(); baos.close(); return baos.tobytearray(); } private string getnombrefileenvio(int nrotk, string nombreenfs){ string[] anombrefile = nombreenfs.split("%"); string nombrefile = anombrefile[1]; return nombrefile; } }
how recover image database folder / downloads / , deliver user?
thank you
Comments
Post a Comment