欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Servlet下载文件

程序员文章站 2022-07-10 18:23:15
package com.itcast.web.controller;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java....
package com.itcast.web.controller;

import com.itcast.utils.DownLoadUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DownLoadServlet extends HttpServlet {

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

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取文件名
        String fileName = req.getParameter("fileName");

        //获取文件真实路径
        ServletContext servletContext = req.getServletContext();
        String realPath = servletContext.getRealPath("/download/" + fileName);

        //获取文件类型
        String mimeType = servletContext.getMimeType(fileName);//根据文件名后缀去tomcat的web.xml中获取文件类型image/jpeg
        resp.setHeader("Content-type", mimeType);

        // 设置 下载附加 attachment 表示附件 , filename就是下载后的文件名称
        long l = System.currentTimeMillis();
        //解决中文文件名乱码问题
        String encodeFileName = DownLoadUtils.encodeFileName(req, fileName);
        resp.setHeader("Content-Disposition", "attachment;filename=" + encodeFileName + "_" + l);

        //文件读写
        FileInputStream bis = new FileInputStream(realPath);
        BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());

        //循环读写
        byte[] b = new byte[1024];
        int len;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }

        bos.close();
        bis.close();

    }
}
package utils;

import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class DownLoadUtils {

    public static String encodeFileName(HttpServletRequest request, String fileName) {
        String name = "";

        String agent = request.getHeader("User-Agent");
        System.out.println(agent);
        try {
            if (agent.contains("Firefox")) {
                BASE64Encoder base64Encoder = new BASE64Encoder();
                name = "=?UTF-8?B?" + new String(base64Encoder.encode(fileName.getBytes("UTF-8"))) + "?=";
            } else {
                name = URLEncoder.encode(fileName, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //System.out.println(name);
        return name;
    }
}

本文地址:https://blog.csdn.net/weixin_42210904/article/details/109588866

相关标签: Servlet