Java Web server file download (with download popup)

Source: Internet
Author: User

Yesterday did a file from the service download function, how do not pop up the page, download box. After the query learned. There are two ways of

1.<a href= ' download path '/>

2.window.location.href = BasePath + "Downloadtemplate.do?template=huiyuan";

There are two ways to have this pop-up window

Hope can help, meet with me the same problem friend ~

A downloadable tool class that can be copied directly to the available

Public void DownloadFile (String filepath,string filename,httpservletresponse response) throws exception{
//uri URI = This.getclass (). getClassLoader (). GetResource (FilePath). Touri ();
File File = new file (FilePath);
//Empty response
Response.reset ();
//Set the header of response
Response.AddHeader ("Content-disposition", "Attachment;filename="
+ new String (Filename.getbytes ("GBK"), "iso-8859-1")); Files downloaded after transcoding do not appear garbled in Chinese
Response.AddHeader ("Content-length", "" "+ File.length ());
//Download the file in stream form
        
InputStream fis = new Bufferedinputstream (new FileInputStream (FilePath));
byte[] buffer = new byte[fis.available ()];
fis.read (buffer);
fis.close ();
outputstream toclient = new Bufferedoutputstream (Response.getoutputstream ());
toclient.write (buffer);
Toclient.flush ();
toclient.close ();
       }

Java Web server file download (with download popup)

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.