Java File download, page display Save as effect

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.