1 代码实现
1 index.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=ISO-8859-1"> <title>文件下载演示</title> </head> <body> <h1>使用a标签直接指向服务器上的资源</h1> <a href="${pageContext.request.contextPath }/download/a.flv">a.flv</a><br/> <a href="${pageContext.request.contextPath }/download/a.jpg">a.jpg</a><br/> <a href="${pageContext.request.contextPath }/download/a.mp3">a.mp3</a><br/> <a href="${pageContext.request.contextPath }/download/a.mp4">a.mp4</a><br/> <a href="${pageContext.request.contextPath }/download/a.txt">a.txt</a><br/> <a href="${pageContext.request.contextPath }/download/a.zip">a.zip</a> <h1>使用服务器端编码的方式实现文件下载</h1> <a href="${pageContext.request.contextPath }/downloadServlet?filename=a.flv">a.flv</a><br/> <a href="${pageContext.request.contextPath }/downloadServlet?filename=a.jpg">a.jpg</a><br/> <a href="${pageContext.request.contextPath }/downloadServlet?filename=a.mp3">a.mp3</a><br/> <a href="${pageContext.request.contextPath }/downloadServlet?filename=a.mp4">a.mp4</a><br/> <a href="${pageContext.request.contextPath }/downloadServlet?filename=a.txt">a.txt</a><br/> <a href="${pageContext.request.contextPath }/downloadServlet?filename=a.zip">a.zip</a> </body> </html>
2 downloadServlet入门代码
package www.test.web.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1 获取要下载的文件的名称 String filename = request.getParameter("filename"); //a.jpg // 2 要下载的这个文件的类型-----客户端通过文件的 MIME 类型去区分类型 response.setContentType(this.getServletContext().getMimeType(filename)); // 3 告诉客户端该文件不是直接解析 而是以附件形式打开(下载) response.setHeader("Content-Disposition", "attachment;filename="+filename); // 4 获取文件的绝对路径 String realPath = this.getServletContext().getRealPath("download/"+filename); // 5 获得该文件的输入流 InputStream in = new FileInputStream(realPath); // 6 获得输出流---通过 response 获得的输出流 用于向客户端写内容 ServletOutputStream out = response.getOutputStream(); // 7 文件拷贝的模板代码 int len=0; byte[] buffer = new byte[1024]; while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } // 8 关闭资源 in.close(); //out.close(); //respone 获得的流可以不用手动关闭, 会自动关闭。 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 downloadServlet2详细代码
package www.test.web.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import sun.misc.BASE64Encoder; public class DownloadServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1 获取要下载的文件的名称 String filename = request.getParameter("filename"); // 这里获取到的中文是乱码 // ???.jpg // 解决获取到的中文名乱码问题 filename = new String(filename.getBytes("ISO8859-1"), "UTF-8"); // 2 获得请求头中的 User-Agent String agent = request.getHeader("User-Agent"); // 3 根据不同浏览器进行不同的编码 String filenameEncoder = ""; if (agent.contains("MSIE")) { // IE 浏览器 filenameEncoder = URLEncoder.encode(filename, "utf-8"); filenameEncoder = filenameEncoder.replace("+", " "); } else if (agent.contains("Firefox")) { // 火狐浏览器 BASE64Encoder base64Encoder = new BASE64Encoder(); filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // 其它浏览器 filenameEncoder = URLEncoder.encode(filename, "utf-8"); } // 4 要下载的这个文件的类型-----客户端通过文件的 MIME 类型去区分类型 response.setContentType(this.getServletContext().getMimeType(filename)); // 5告诉客户端该文件不是直接解析 // 而是以附件形式打开(下载)----filename="+filename 客户端默认对名字进行解码 response.setHeader("Content-Disposition", "attachment;filename="+filenameEncoder); // 6 获取文件的绝对路径 String realPath = this.getServletContext().getRealPath("download/"+filename); // 7用一个输入流和文件相关联 InputStream in = new FileInputStream(realPath); // 8 获得输出流---通过 response 获得的输出流 用于向客户端写内容 ServletOutputStream out = response.getOutputStream(); // 9 文件拷贝的模板代码 int len =0; byte[] buffer = new byte[1024]; while((len=in.read(buffer))!=-1){ out.write(buffer, 0, len); } //10 关闭资源 in.close(); //out.close();//respone 获得的流可以不用手动关闭,会自动关闭。但是自己 new 的流一定要关闭 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }