• java servlet文件下载


    按照以下步骤编写

    1. html编写: <a href="/day15/downloadServlet?filename=1.jpg">图片1</a>

    2. 编写DownLoadUtils工具处理多浏览器中文乱码:

    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;
      }
    }

    2. 编写Servlet类的逻辑代码:

    import cn.itcast.web.utils.DownLoadUtils;

    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.FileInputStream;
    import java.io.IOException;

    @WebServlet("/downloadServlet")
    public class DownloadServlet extends HttpServlet {


      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数,文件名称
        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);
        //4.将输入流的数据写出到输出流中
        ServletOutputStream sos = response.getOutputStream();
        byte[] buff = new byte[1024 * 8];
        int len = 0;
        while((len = fis.read(buff)) != -1){
          sos.write(buff,0,len);
        }

        fis.close();


      }

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
      }
    }

  • 相关阅读:
    redis info命令中各个参数的含义
    mogndb 慢查询
    一分钟看懂Docker的网络模式和跨主机通信
    解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法
    dva学习---effects异步中通过select获取当前的state
    golang json数组拼接
    golang mongodb查找find demo
    React从入门到精通系列之(14)refs和DOM元素
    选择——ERP信息系统选型
    企业库存资产的帐实管理思考
  • 原文地址:https://www.cnblogs.com/M87-A/p/14730617.html
Copyright © 2020-2023  润新知