Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。
一.ListFile.java
1.递归遍历指定目录下的所有文件和目录,并将文件名存储到Map集合中
package com.control;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Ray on 2018/3/16 0016.
**/
public class ListFile extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取指定目录
String FilePath = "f:/test";
//存储要下载的文件名
Map fileNameMap = new HashMap();
//递归遍历filepath目录下的所有文件和目录,将文件名存储到map集合中
listfile(new File(FilePath),fileNameMap);//File既可以代表一个文件也可以代表一个目录
//将Map集合发送到listfile.jsp页面显示
request.setAttribute("fileNameMap", fileNameMap);
request.getRequestDispatcher("listfile.jsp").forward(request,response);
}
/**
* @Author: Ray
* @Date: 2018/3/17 0017
* @Description: 递归遍历指定目录下的所有文件和目录,并将文件名存储到Map集合中
* @Return:
*/
public void listfile(File file,Map map){
//如果file代表的不是一个文件,而是一个目录
if(!file.isFile()){
//列出该目录下的所有文件和目录
File[] files = file.listFiles();
//遍历files[]数组
for(File f:files){
//递归
listfile(f,map);
}
}else{
String fileOrgName = file.getName();
int index = fileOrgName.lastIndexOf(".");
String fileName = fileOrgName.substring(0,index);
System.out.println(file.getName());
map.put(file.getName(),fileName);
}
}
}
二.listfile.jsp
1.遍历Map集合
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
文件列表
${fileItem.value}下载
三.Download.java
1.设置响应头,控制浏览器下载该文件
2.JSP文件下载中文和空格显示Content-Disposition设置
package com.control;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
* Created by Ray on 2018/3/16 0016.
**/
public class Download extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到要下载的文件名
String fileName = request.getParameter("filename");
//要下载的文件的目录
String fileSavePath = "f:/test";
//要下载的文件
File file = new File(fileSavePath + File.separator + fileName);
//如果文件不存在
if(!file.exists()){
request.setAttribute("info","您要下载的资源已被删除!");
request.getRequestDispatcher("messagedownload.jsp").forward(request,response);
return;
}
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+", "%20"));
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(fileSavePath + File.separator + fileName);
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while(0<=(len=in.read(buffer))){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入输出流
out.close();
in.close();
}
}
四.messagedownload.jsp
1.输出提示信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
下载操作
下载操作
<%
//获取提示信息
String info = String.valueOf(request.getAttribute("info"));
//如果提示信息不为空,则输出提示信息
if (info != "null") {
out.print(info);
}
%>
五.web.xml
BookList
com.control.BookList
BookList
/BookList
BookAdd
com.control.BookAdd
BookAdd
/BookAdd
BookUpdate
com.control.BookUpdate
BookUpdate
/BookUpdate
BookDoUpdate
com.control.BookDoUpdate
BookDoUpdate
/BookDoUpdate
BookDelete
com.control.BookDelete
BookDelete
/BookDelete
BookDoDelete
com.control.BookDoDelete
BookDoDelete
/BookDoDelete
BookBatchAdd
com.control.BookBatchAdd
BookBatchAdd
/BookBatchAdd
BookBatchDelete
com.control.BookBatchDelete
BookBatchDelete
/BookBatchDelete
BookFind
com.control.BookFind
BookFind
/BookFind
Login
com.control.Login
Login
/Login
DrawImage
com.control.DrawImage
DrawImage
/DrawImage
Register
com.control.Register
Register
/Register
LoginUser
com.control.LoginUser
LoginUser
/LoginUser
ExitLogin
com.control.ExitLogin
ExitLogin
/ExitLogin
Upload
com.control.Upload
Upload
/Upload
ListFile
com.control.ListFile
ListFile
/ListFile
Download
com.control.Download
Download
/Download
CharSetFilter
com.Filter.CharSetFilter
charset
UTF-8
CharSetFilter
/*
404
/error/error404.jsp
500
/error/error500.jsp
ErrorFilter
com.Filter.ErrorFilter
ErrorFilter
/error.jsp
ERROR
六.页面效果
ok!