File upload and download--File Download __java-web Foundation

Source: Internet
Author: User

After we have finished uploading the files, we are going to download the files.
How to implement file downloads in Web applications: Hyperlinks point directly to download resources.
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 process the corresponding entity content directly, but instead 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. lists the file resources that provide downloads

We want to provide the Web application system file resources to the user to download, first of all we have a page listing the uploaded files in the directory of all the files, when the user clicks on the file to download hyperlinks when the download operation, write a listfileservlet, Used to list all downloaded files in the Web application system.
The code for Listfileservlet is as follows:

List all files in Web site public class Listfileservlet extends HttpServlet {protected void doget (HttpServletRequest request, HttpS Ervletresponse response) throws Servletexception, IOException {String path = This.getservletcontext (
        ). Getrealpath ("/web-inf/upload");
        Map map = new HashMap ();

        ListFile (new File (path), map);
        Request.setattribute ("map", map);
    Request.getrequestdispatcher ("/listfile.jsp"). Forward (request, response);
            }//How to save recursive resources public void ListFile (file file, map map) {if (!file.isfile ()) {//Incoming directory
            File[] Children = file.listfiles ();
            for (File F:children) {listfile (f, map); } else {//is the file name of the file/* that is stored in the server, and this should be the case: 4de727e8-2579-477f-a4ae-658355f107fe_ Upload file _1.txt * At this time, we want to get to the file's original file name: Upload file _1.txt/String fileName = File.getname (). substr ING (File.getname (). IndexOf ("_") +1); Get the original filename of the file Map.put (file.getname (), filename); <a href= "/day18/listfileservlet?filename= file in Server name" > file original filename </a>}} protected void Dopos T (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {DoG
    ET (request, response); }

}

Here is a brief look at the ListFile method in Listfileservlet, ListFile method is used to list all the files in the upload directory, ListFile method is used in the internal recursive , in actual development, We will definitely create a table in the database, it will store the uploaded file name and the file's specific directory, we can know the file directory through the query table, is not required to use recursive operation, this example is because the database is not used to store uploaded file names and files of the specific storage location, The location of the uploaded file is also used to scatter the storage of the hash algorithm, so, recursion is needed, and in recursion, the acquired filename is stored in a map set that is passed from the outside to the ListFile method, so that all files are stored in the same map collection.
Next we're going to write a listfile.jsp page that shows the list of downloaded files.

<% @page import= "Java.util.HashMap"%> <% @page import= "java.util.Map"%> <%@ page language= "Java" Contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix=" C "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 

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.