文件下载需求:
1. 页面显示超链接
2. 点击超链接后弹出下载提示框
3. 完成图片文件下载
分析:
1.超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框,本满足需求
2.任何资源都必须弹出下载提示框
步骤:
1.定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
2.定义Servlet
1.获取文件名称
2.使用字节输入流加载文件进内存
3.指定response的响应头:content-disposition:attachment;filename=xxx
4.将数据写出到response输出流
代码实现:
download.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="img/1.jpg">图片1</a> <a href="img/1.avi">视频</a> <hr> <a href="/downloadServlet?filename=1.jpg">图片1</a> <a href="/downloadServlet?filename=1.avi">视频1</a> </body> </html>
DownloadServlet类:
package com.example.download; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.FileInputStream; import java.io.IOException; @WebServlet(name = "downloadServlet", value = "/downloadServlet") public class DownloadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取请求参数u,文件名称 String filename = request.getParameter("filename"); //2.使用字节输出流加载文件进内存 //2.1找到文件服务器路径 ServletContext servletContext = this.getServletContext(); String realPath = servletContext.getRealPath("/img/" + filename); //2.2用字节流关联 FileInputStream fis = new FileInputStream(realPath);// 输入流 //3.设置response的响应头 //3.1设置响应头类型:content-type String mimeType = servletContext.getMimeType(filename);//获取文件的mime类型 response.setHeader("content-type",mimeType); //3.2设置响应头打开方式:content-disposition response.setHeader("content-disposition","attachment;filename="+filename);// attachment:附件 //4.将输入流的数据写出到输出流中 ServletOutputStream outputStream = response.getOutputStream();//输出流 byte[] bytes = new byte[1024 * 8]; int len; while ((len = fis.read(bytes))!= -1){ outputStream.write(bytes,0,len); } } }
中文文件名问题:
解决思路:
1.获取客户端使用的浏览器版本信息
2.根据不同的版本信息,设置filename的编码方式不同
DownLoadUtils:工具类
package com.example.utils; import sun.misc.BASE64Encoder; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class DownLoadUtils { public static String getFileName(String agent, String filename) throws UnsupportedEncodingException { if (agent.contains("MSIE")) { // IE浏览器 filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+", " "); } else if (agent.contains("Firefox")) { // 火狐浏览器 BASE64Encoder base64Encoder = new BASE64Encoder(); filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // 其它浏览器 filename = URLEncoder.encode(filename, "utf-8"); } return filename; } }
DownloadServlet(使用工具类):文件下载类
package com.example.download; import com.example.utils.DownLoadUtils; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.FileInputStream; import java.io.IOException; @WebServlet(name = "downloadServlet", value = "/downloadServlet") public class DownloadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取请求参数u,文件名称 String filename = request.getParameter("filename"); //2.使用字节输出流加载文件进内存 //2.1找到文件服务器路径 ServletContext servletContext = this.getServletContext(); String realPath = servletContext.getRealPath("/img/" + filename); //2.2用字节流关联 FileInputStream fis = new FileInputStream(realPath);// 输入流 //3.设置response的响应头 //3.1设置响应头类型:content-type String mimeType = servletContext.getMimeType(filename);//获取文件的mime类型 response.setHeader("content-type",mimeType); //3.2设置响应头打开方式:content-disposition //解决中文文件名问题 //1.获取user-agent请求头 String agent = request.getHeader("user-agent"); //2.使用工具类方法编码文件名即可 filename = DownLoadUtils.getFileName(agent, filename); response.setHeader("content-disposition","attachment;filename="+filename);// attachment:附件 //4.将输入流的数据写出到输出流中 ServletOutputStream outputStream = response.getOutputStream();//输出流 byte[] bytes = new byte[1024 * 8]; int len; while ((len = fis.read(bytes))!= -1){ outputStream.write(bytes,0,len); } } }
标签:文件,String,request,案列,filename,content,import,response,下载 From: https://www.cnblogs.com/qihaokuan/p/16967216.html