JavaWeb项目,WebRoot 》 pages 》 download.jsp
--------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.LinkedList"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--需求:在你的磁盘,找一个文件夹,把该文件夹下的所有的文件及其子文件,
循环遍历到JSP页面上,提供下载树形结构的知识。队列(先进先出)--%>
<%
File srcDir = new File("F:" + File.separator + "测试目录");
LinkedList<File> list = new LinkedList<File>();
list.add(srcDir);
while (!list.isEmpty()) {
File dir = list.poll();
File[] fs = dir.listFiles();
if (fs != null) {
for (File f : fs) {
if (f.isDirectory()) {
list.add(f);
} else {
%>
<a href="${pageContext.request.contextPath}/download?filepath=<%=f.getAbsolutePath()%>"><%=f.getName() %></a><br/>
<%
}
}
}
}
%>
</body>
</html>
----------------------------------------------------------------------
download Servlet:
package com.ygang.servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import sun.misc.BASE64Encoder;
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = -4546919017317626185L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 先接收下载文件的路径
// 解决get方式乱码的问题
String filepath = request.getParameter("filepath");
filepath = new String(filepath.getBytes("ISO-8859-1"),"UTF-8");
// filepath 内容:d:\\root\\aa\\xxx.mp3
// 需要文件的名称
int index = filepath.lastIndexOf("\\");
// 已经获取到了文件名称
String filename = filepath.substring(index+1);
// 先获取请求头(user-agent)
String agent = request.getHeader("user-agent");
// Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
// 采用的IE 是MSIE
// System.out.println(agent);
// 处理的是火狐和IE 谷歌
if(agent.contains("Firefox")){
// 使用的火狐
// 采用的是BASE64编码
filename = "=?UTF-8?B?"+ new String(new BASE64Encoder().encode(filename.getBytes("UTF-8"))) + "?=";
}else{
// 使用IE和谷歌 采用的是URL编码
// 需要把filename编成URL编码
filename = URLEncoder.encode(filename, "UTF-8");
// URL编码默认把空格编成+号
filename = filename.replace("+", " ");
}
// 设置两个头一个流
response.setContentType(getServletContext().getMimeType(filename));
response.setHeader("Content-Disposition", "attachment;filename="+filename);
// 获取输入流
InputStream in = new FileInputStream(filepath);
OutputStream os = response.getOutputStream();
int len = 0;
byte [] b = new byte[1024];
while((len = in.read(b)) != -1){
os.write(b, 0, len);
}
in.close();
os.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}