After testing Firefox, QQ, IE browser is available Chrome browser not (directly downloaded)
1. System Framework Springmvc+jsp
2. Background servlet Code
@RequestMapping ("Download") Public voidDownload (httpservletrequest request,httpservletresponse response) {Bufferedinputstream dis=NULL; Bufferedoutputstream Fos=NULL; String urlstring= Request.getparameter ("URLString"); String FileName= Urlstring.substring (Urlstring.lastindexof ('/') + 1); Try{URL URL=NewURL (urlstring); //Response.setcontenttype ("application/x-msdownload;");Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition", "attachment; Filename= "+NewString (Filename.getbytes ("Utf-8"), "Iso8859-1")); Response.setheader ("Content-length", String.valueof (Url.openconnection (). Getcontentlength ())); Dis=NewBufferedinputstream (Url.openstream ()); FOS=NewBufferedoutputstream (Response.getoutputstream ()); byte[] Buff =New byte[2048]; intBytesread; while( -1! = (bytesread = dis.read (buff, 0, Buff.length))) {fos.write (buff,0, Bytesread); } } Catch(Exception e) {e.printstacktrace (); } finally { if(Dis! =NULL) Try{dis.close (); }Catch(Exception e) {e.printstacktrace (); } if(Fos! =NULL) Try{fos.close (); }Catch(Exception e) {e.printstacktrace (); } } }
3. Page JSP code
<inputtype= "button"onclick= "Downloadimage ()"value= "Download"><Script> functionDownloadimage () {varurlstring= "Http://pic32.nipic.com/20130829/12906030_124355855000_2.png"; //jump to back-end controllerLocation.href="${ctx}/guest/download.do?urlstring="+urlstring; }</Script>
4. Download effect 1 (Firefox)
Download Effect 2 (QQ)
Java File download, page display Save as effect