Loading

Servlet文件下载

案例:

* 文件下载需求:
	1. 页面显示超链接
	2. 点击超链接后弹出下载提示框
	3. 完成图片文件下载


* 分析:
	1. 超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框。不满足需求
	2. 任何资源都必须弹出下载提示框
	3. 使用响应头设置资源的打开方式:
		* content-disposition:attachment;filename=xxx


* 步骤:
	1. 定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
	2. 定义Servlet
		1. 获取文件名称
		2. 使用字节输入流加载文件进内存
		3. 指定response的响应头: content-disposition:attachment;filename=xxx
		4. 将数据写出到response输出流


* 问题:
	* 中文文件问题
		* 解决思路:
			1. 获取客户端使用的浏览器版本信息
			2. 根据不同的版本信息,设置filename的编码方式不同

download.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a href="/request/test?filename=2.jpg"  >图片1</a>
</body>
</html>

Download.java

package cn.itcast.web.downLoad;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/download")
public class DownLoad extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //1.获取请求参数,文件名称
        String filename = req.getParameter("filename");
        //获取ServletContext对象
        ServletContext context = this.getServletContext();
        //2.使用字节输入流加载文件进内存
        //2.找到文件服务器的路径
        String realPath = context.getRealPath("/img/" + filename);
        //2.2用字节流关联
        FileInputStream fileInputStream = new FileInputStream(realPath);
        //3.设置response的响应头
        //3.1设置响应头的类型:content-type
        String mimeType = context.getMimeType(filename);//获取文件MIME类型
        resp.setHeader("content-type","mimeType");
        //3.2设置响应头打开方式:content-disposition
        resp.setHeader("Content-Disposition","attachment;filename="+filename);

        //解决中文文件名问题
        //1.获取user-agent请求头
        String agent = req.getHeader("user-agent");
        //2.使用工具类方法编码文件名
        filename = DownloadUtils.getFileName(agent,filename);


        //4.将输入流的数据写出到输出流
        ServletOutputStream outputStream = resp.getOutputStream();
        byte[] buff = new byte[1024*8];
        int len = 0;
        while((len = fileInputStream.read(buff)) != -1){
            outputStream.write(buff,0,len);
        }
        outputStream.close();

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

posted @ 2020-12-15 22:48  IamHzc  阅读(280)  评论(0编辑  收藏  举报