测试用的 download.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>使用a标签直接指向服务器上的资源</h1>
<a href="/download/a.flv">a.flv</a><br>
<a href="/download/a.jpg">a.jpg</a><br>
<a href="/download/a.mp3">a.mp3</a><br>
<a href="/download/a.mp4">a.mp4</a><br>
<a href="/download/a.txt">a.txt</a><br>
<a href="/download/a.zip">a.zip</a><br>
<h1>使用服务器端编码的方式实现文件下载</h1>
<a href="/web1/downloadServlet?filename=a.flv">a.flv</a><br>
<a href="/web1/downloadServlet?filename=a.jpg">a.jpg</a><br>
<a href="/web1/downloadServlet?filename=a.mp3">a.mp3</a><br>
<a href="/web1/downloadServlet?filename=a.mp4">a.mp4</a><br>
<a href="/web1/downloadServlet?filename=a.txt">a.txt</a><br>
<a href="/web1/downloadServlet?filename=a.zip">a.zip</a><br>
<a href="/web1/downLoadServlet2?filename=美女.jpg">美女.jpg</a><br>
</body>
</html>
测试用的servlet代码
DownloadServlet.java
package com.xiangshuai.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.tagext.TryCatchFinally;
import org.apache.coyote.http11.filters.BufferedInputFilter;
/**
* Servlet implementation classDownloadServlet
*/
publicclassDownloadServlet extends HttpServlet {
privatestaticfinallongserialVersionUID= 1L;
/**
* @see HttpServlet#HttpServlet()
*/
publicDownloadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response)
*/
protectedvoid doGet(HttpServletRequestrequest,HttpServletResponse response)throwsServletException, IOException {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
String con="F:/download";
String filename = request.getParameter("filename");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("UTF-8");
/*告示浏览器文件的打开方式是下载 ---- Content-Disposition 的作用,
当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
如这里如果 filename,不管是a.mp4,a.flv,a.jpg,a.txt,a.zip都会以正确的方式进行下载*/
response.setHeader("Content-Disposition","attachment;filename="+filename);
//respnse流写出,tomcat引擎会将respnse缓冲区的内容于引擎自己添加的信息组成一个http信息响应--包括响应行,响应头,响应体等
ServletOutputStream os = response.getOutputStream();
//io 流将文件从硬盘读入内存中
try {
bis=newBufferedInputStream(new FileInputStream(new File(con+"/"+filename)));
bos=newBufferedOutputStream(os);
byte[] b=newbyte[1024];intlen=-1;
while((len=bis.read(b))!=-1){
bos.write(b, 0, len);
bos.flush();
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request,HttpServletResponse response)
*/
protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}