Servlet实现文件下载的功能

download.html

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件下载</title>
    <base href="<%=request.getContextPath()+"/"%>>">
</head>
<body>
<h1>文件下载</h1>
<a href="fileDownLoadServlet?name=2.png">点击下载图片</a><br/><br/>
<a href="fileDownLoadServlet?name=笔记.pdf">点击下载笔记</a><br/><br/>
</body>
</html>

FileDownLoadServlet

package com.sparrow.servlet;

import org.apache.commons.io.IOUtils;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @Author: 诉衷情の麻雀
 * @Description: TODO
 * @DateTime: 2023/7/15 12:41
 **/
public class FileDownLoadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("FileDownLoadServlet被调用了");
        //准备下载的文件
        req.setCharacterEncoding("utf-8");
        String downLoadName= req.getParameter("name");
        //3.给HTTP响应,设置响应头Content-Type,就是文件的MiME
        ServletContext servletContext = req.getServletContext();
        String downLoadPath = "/download/";
        String downLoadFileFullPath = downLoadPath + downLoadName;
        String mimeType = servletContext.getMimeType(downLoadFileFullPath);
        resp.setContentType(mimeType);
        //4.给HTTP响应设置响应头Content-Disposition 是指定下载的数据的展示形式
        //如果是attachment则使用文件下载方式
        //如果是Firefox 则中文编码需要 base64
        if (req.getHeader("User-Agent").contains("Firefox")) {
            // 火狐 Base64编码
            resp.setHeader("Content-Disposition", "attachment; filename==?UTF-8?B?" +
                    new BASE64Encoder().encode(downLoadName.getBytes("UTF-8")) + "?=");
        } else {
            // 其他(主流ie/chrome)使用URL编码操作
            resp.setHeader("Content-Disposition", "attachment; filename=" +
                    URLEncoder.encode(downLoadName, "UTF-8"));
        }
        //5.读取下载的文件数据,返回给客户端/浏览器
        // 1)创建一个和要下载的文件关联的输入流
        InputStream resourceAsStream = servletContext.getResourceAsStream(downLoadFileFullPath);
        //2) 得到返回数据的输出流【因为返回的文件大多是二进制字节】
        ServletOutputStream outputStream = resp.getOutputStream();
        //3)使用工具类,将输入流关联的文件对拷到输出流,并返回给客户端/浏览器
        IOUtils.copy(resourceAsStream, outputStream);
    }



}

在web下建一个目录将要下载的文件放入该项目下

在这里插入图片描述
## 项目演示
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诉衷情の麻雀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值