Java, JavaScript implementation attachment download sample

Source: Internet
Author: User

In web development, it is often necessary to develop the "download" module, the following gives a simple example.

On the server side, use Java development:

01 @RequestMapping (value = "download.html", method = Requestmethod.get)
02 public void Download (String ResourceID, httpservletrequest request, httpservletresponse response) {
03 Response.setcontenttype ("Charset=utf-8");
04 File File = new file (path);
05 Response.setheader ("Content-disposition", "attachment; Filename=a ");
06 Bufferedinputstream bis = null;
07 Bufferedoutputstream BOS = NULL;
08 OutputStream fos = null;
09 InputStream FIS = null;
10 try {
11 FIS = new FileInputStream (File.getabsolutepath ());
12 bis = new Bufferedinputstream (FIS);
13 FOS = Response.getoutputstream ();
14 BOS = new Bufferedoutputstream (FOS);
15 int bytesread = 0;
16 byte[] buffer = new BYTE[5 * 1024];
17 while ((bytesread = bis.read (buffer))!=-1) {
18 Bos.write (buffer, 0, bytesread);
19 }
20 Bos.flush ();
21st }catch (e e) {
22 }finally {
23 try {
24 Bis.close ();
25 Bos.close ();
26 Fos.close ();
27 Fis.close ();
28 catch (IOException e) {
29 E.printstacktrace ();
30 }
31 }
32 }

When we request this address at the front end, the server first finds the file, sets the response header, and then outputs the stream to the browser end.

When the browser finds that the body of the response in the header is a stream file, the saved as window is automatically invoked, allowing the user to save the download.

The key here is content-disposition this header property, Content-disposition is an extension of the MIME protocol that indicates how to have the client display the file for the attachment.

It can be set to two values:

Inline//Online open

Attachment//As Attachment download

The value we set here is attachment, so it can be identified as an attachment and downloaded.

It says how to write the server side, and here's how the front end is requested.

There are three ways to front end requests:

1.Form

1 <form action= ' download.html ' method= ' post ' >
2 <input type= ' Submit '/>
3 </form>

2.iframe

1 var iframe = "<iframe style= ' display:none ' src= ' download.html ' ></iframe>"
2 Body.append (IFRAME);

When the IFRAME is append into the body, the download link is automatically requested.

3.open

1 window.open (" Download.html ");

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.