Experience 2--Download Resource Introduction, case analysis details

Source: Internet
Author: User

1. Two ways to implement file downloads in Web applications

Hyperlinks point directly to the download resource, the program implementation download needs to set two response headers:

Set the Content-type value to: Application/x-msdownload. The WEB server needs to tell the browser that the type of content it outputs is not a normal text file or an HTML file, but rather a download file to be saved locally.

The WEB server wants the browser to not work directly with the corresponding entity content, but rather the user chooses to save the corresponding entity content to a file, which requires setting the Content-disposition header. The header specifies how the receiving program handles the content of the data, and only attachment is the standard way in HTTP applications, attachment indicates that user intervention is required . You can also specify the filename parameter after attachment , which is the name of the file that the server recommends the browser to save the entity contents to a file. Be sure to specify content-type before setting content-dispostion.

Because the files to be downloaded can be of various types of files, the corresponding content should be treated as binary when the file is routed to the client, so the method should be called to return the Serveltoutputstream object to write the text to the client Piece of content.

2. Download the case

All files in the upload directory are displayed to the user and the user is allowed to complete the download.

(Read all the files under a folder, save them to the list in the collection, and save to the request scope) listfileservelt-(show all the file lists) listfiles.jsp-----Downloaservlet.java

3. To say that a little bit of the theory is not dizzy, then a bit of practical case analysis:

  Download the page of the Jump listfile.jsp <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" "%> <%@ taglib uri=" htt P://java.sun.com/jsp/jstl/core "prefix=" C "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  


 

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.