1.Html文件,使用超链接,指向处理下载请求的servlet类,并附带上文件路径和文件名
(就像自动用GET那样,在URL上使用?,&来使用和连接参数)
2.servlet要处理:
0.获取request获取URL的参数,文件路径filepath和文件名filename
1.使用ServletContext获取文件名filename的MIME类型,并设置为响应头的ContentType
2.使用ServletContext获取文件路径filepath的真实路径realpath,并为之创建出File对象
3.设置响应头的content-disposition为 attachment;filename=文件名,相当于弹窗附件下载,命为文件名
4.使用BufferedInputStream,将文件读入内存,输入到byte[],并同时使用response字节输出流输出
Download.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 使用超链接,指向处理下载请求的servlet类,并附带上文件路径和文件名 -->
<a href = "http://localhost:8080/WebServlet/ResponseDemo3?filepath=./WEB-INF/bb.jpg&filename=bb.jpg" >图片</a>
<a href = "http://localhost:8080/WebServlet/ResponseDem