Band of Brothers JavaWeb tutorial file downloading technology

● List files downloaded resources

We want the Web application resource file systems available to users to download, first of all we need to have a page listing all the files in the directory to upload files, download files when the user clicks on a hyperlink to download the operation, write a ListFileServlet, with to list all files downloaded Web application system.

ListFileServlet code is as follows:

package com.xdl.servlet;

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ListFileServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void service(HttpServletRequest request,

             HttpServletResponse response) throws ServletException, IOException {

        // Get directory upload files

        String uploadFilePath = this.getServletContext().

                getRealPath("/WEB-INF/ upload");

        // store to download the file name

        Map<String, String> fileNameMap = new HashMap<String, String>();

        // 递归遍历filepath目录下的所有文件和目录,将文件的文件名存储到map集合中

        listfile(new File(uploadFilePath), fileNameMap);

        // File既可以代表一个文件也可以代表一个目录

        // 将Map集合发送到listfile.jsp页面进行显示

        request.setAttribute("fileNameMap", fileNameMap);

        request.getRequestDispatcher("/listfile.jsp").

                forward(request, response);

    }

    public void listfile(File file, Map<String, String> map) {

        // 如果file代表的不是一个文件,而是一个目录

        if (!file.isFile()) {

             // 列出该目录下的所有文件和目录

             File files[] = file.listFiles();

             // 遍历files[]数组

             for (File f : files) {

                  // 递归

                  listfile(f, map);

             }

        } else {

             /**

              * 处理文件名,上传后的文件是以uuid_文件名的形式去重新命名的

              */

             String realName = file.getName().substring(

                                file.getName().indexOf ("_") + 1);

             // file.getName()得到的是文件的原始名称,这个名称是唯一的,

             // 因此可以作为key,realName是处理过后的名称,有可能会重复

             map.put(file.getName(), realName);

        }

    }

}

这里介绍一下ListFileServlet中listfile方法,listfile方法是用来列出目录下的所有文件的,listfile方法内部用到了递归,在实际开发当中,我们肯定会在数据库创建一张表,里面会存储上传的文件名以及文件的具体存放目录,我们通过查询表就可以知道文件的具体存放目录,是不需要用到递归操作的,这个例子是因为没有使用数据库存储上传的文件名和文件的具体存放位置,而上传文件的存放位置又使用了散列算法打散存放,所以需要用到递归,在递归时,将获取到的文件名存放到从外面传递到listfile方法里面的Map集合当中,这样就可以保证所有的文件都存放在同一个Map集合当中。

在web.xml文件中配置ListFileServlet。

<servlet>

    <servlet-name>ListFileServlet</servlet-name>

    <servlet-class>com.xdl.servlet.ListFileServlet</servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>ListFileServlet</servlet-name>

    <url-pattern>/list</url-pattern>

</servlet-mapping>

●  展示下载文件的listfile.jsp页面,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>兄弟连IT教育</title>

</head>

<body>

    <c:forEach var="me" items="${fileNameMap}">

        <c:url value="/download" var="downurl">

             <c:param name="filename" value="${me.key}"></c:param>

        </c:url>

         ${me.value}<a href="${downurl}">下载</a>

        <br />

    </c:forEach>

   </body>

</html>

访问ListFileServlet,就可以在listfile.jsp页面中显示提供给用户下载的文件资源,如图25所示。

1910b6cd4a8e41179b863c92627a570b.png

图25  listfile.jsp页面


●  实现文件下载

编写一个用于处理文件下载的Servlet,DownloadServlet的代码如下:

package com.xdl.servlet;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.net.URLEncoder;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void service(HttpServletRequest request,

         HttpServletResponse response) throws ServletException, IOException {

        // 得到要下载的文件名

        String fileName = request.getParameter("filename");

        // 上传的文件都是保存在/WEB-INF/upload目录下的子目录当中

        String fileSaveRootPath =

             this.getServletContext().getRealPath ("/WEB-INF/upload");

        // 通过文件名找出文件的所在目录

        String path = findFileSavePathByFileName(fileName, fileSaveRootPath);

        // 得到要下载的文件

        File file = new File(path + "\\" + fileName);

        // 如果文件不存在

        if (!file.exists()) {

             request.setAttribute("message", "您要下载的资源已被删除!!");

             request.getRequestDispatcher("/message.jsp").

                       forward(request, response);

             return;

        }

        // 处理文件名

        String realname = fileName.substring(fileName.indexOf("_") + 1);

        // 设置响应头,控制浏览器下载该文件

        response.setHeader("content-disposition",

                         "attachment;filename=" + URLEncoder.

                         encode(realname, "UTF-8"));

        // 读取要下载的文件,保存到文件输入流

        FileInputStream in = new FileInputStream(path + "\\" + fileName);

        // 创建输出流

        OutputStream out = response.getOutputStream();

        // 创建缓冲区

        byte buffer[] = new byte[1024];

        int len = 0;

        // 循环将输入流中的内容读取到缓冲区当中

        while ((len = in.read(buffer)) > 0) {

             // 输出缓冲区的内容到浏览器,实现文件下载

             out.write(buffer, 0, len);

        }

        // 关闭文件输入流

        in.close();

        // 关闭输出流

        out.close();

    }

    public String findFileSavePathByFileName

           (String filename, String saveRootPath) {

        int hashcode = filename.hashCode();

        int dir1 = hashcode & 0xf;

        int dir2 = (hashcode & 0xf0) >> 4;

        String dir = saveRootPath + "\\" + dir1 + "\\" + dir2;

        File file = new File(dir);

        if (!file.exists()) {

             // 创建目录

             file.mkdirs();

        }

        return dir;

    }

}

●  在web.xml文件中配置DownloadServlet。

<servlet>

    <servlet-name>DownloadServlet</servlet-name>

    <servlet-class>com.xdl.servlet.DownloadServlet</servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>DownloadServlet</servlet-name>

    <url-pattern>/download</url-pattern>

</servlet-mapping>

再次访问ListFileServlet,就可以在listfile.jsp页面中点击下载开始下载资源,如图26所示。

7f125a2a3af54fc0a66987a32f9c1db0.png

图26  下载文件


bfaf8fc02f654ecabe242f4ac18727f8.png

图27  下载文件成功


从运行结果可以看到,我们的文件下载功能已经可以正常下载文件了。


Guess you like

Origin blog.51cto.com/14311187/2403767