@RequestMapping Public voidDownload (httpservletrequest request, httpservletresponse response)throwsException {response.setcontenttype ("Text/html;charset=utf-8"); Bufferedinputstream in=NULL; Bufferedoutputstream out=NULL; Request.setcharacterencoding ("UTF-8"); String RootPath=request.getsession (). Getservletcontext (). Getrealpath ("/"); String FileName= Request.getparameter ("F"); FileName=Commonproperty.getvalue (fileName); Try{File F=NewFile (RootPath + "template/" +fileName); Response.setcontenttype ("Application/x-excel"); Response.setcharacterencoding ("UTF-8"); Response.setheader ("Content-disposition", "attachment; Filename= "+fileName); Response.setheader ("Content-length", String.valueof (F.length ())); Inch=NewBufferedinputstream (NewFileInputStream (f)); out=NewBufferedoutputstream (Response.getoutputstream ()); byte[] data =New byte[1024]; intLen = 0; while( -1! = (len=in.read (data, 0, Data.length))) {out.write (data,0, Len); } } Catch(Exception e) {e.printstacktrace (); } finally { if(In! =NULL) {in.close (); } if(Out! =NULL) {out.close (); } } }
Springmvc Downloading Excel files