系统: Mac Os
开发工具: IDEA
用到的Jar包:
download.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>文件下载</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="/DownloadServlet?filename=1.txt">文件下载</a>
</body>
</html>
message.jsp
随便写,问题不大
DownloadServlet
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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DownloadServlet
*/
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DownloadServlet() {
super();
// TODO Auto-generated constructor stub
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到要下载的文件名 通过 request对象的方法
String fileName = request.getParameter("filename");
//处理文件名的中文乱码问题
fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
//下载的文件都是保存在/WEB-INF/upload目录当中
String fileSaveRootPath=this.getServletContext().getRealPath("/WEB-INF/upload");
System.out.println(fileSaveRootPath);
//得到要下载的文件
File file = new File(fileSaveRootPath + "/" + fileName);
System.out.println(fileSaveRootPath + "/" + fileName);
//如果文件不存在
if(!file.exists()){
request.setAttribute("msg", "您要下载的资源已被删除!!");
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(file);
//创建输出流
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 void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>download.jsp</welcome-file>
</welcome-file-list>
<!-- 声明Servlet -->
<servlet>
<!-- Servlet名称 -->
<servlet-name>DownloadServlet</servlet-name>
<!-- Servlet完整类名 -->
<servlet-class>DownloadServlet</servlet-class>
</servlet>
<!-- Servlet映射 -->
<servlet-mapping>
<!-- Servlet名称 -->
<servlet-name>DownloadServlet</servlet-name>
<!-- URL映射 -->
<url-pattern>/DownloadServlet</url-pattern>
</servlet-mapping>
</web-app>
注意事项:
1.下载文件放在WEB_INF下的upload文件夹里
2.url路径不要写错
3.注意File file里的"/"与"\\"
推荐一个 Jar 包下载地址:https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload
搜索后下载对应Jar包即可。