zoukankan      html  css  js  c++  java
  • HTTP 下载文件工具类

    ResponseUtils.java

    package javax.utils;
    
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.URLEncoder;
    
    import javax.servlet.http.HttpServletResponse;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /**
     * HTTP 输出响应内容工具类
     * 
     * @author Logan
     * @createDate 2019-02-13
     * @version 1.0.0
     *
     */
    public class ResponseUtils {
    
        /**
         * 发送HTTP响应信息
         * 
         * @param response HTTP响应对象
         * @param message 信息内容
         * @throws IOException 抛出异常,由调用者捕获处理
         */
        public static void write(HttpServletResponse response, String message) throws IOException {
            response.setContentType("text/html;charset=UTF-8");
    
            try (
                    PrintWriter writer = response.getWriter();
            ) {
                writer.write(message);
                writer.flush();
            }
        }
    
        /**
         * 发送HTTP响应信息,JSON格式
         * 
         * @param response HTTP响应对象
         * @param message 输出对象
         * @throws IOException 抛出异常,由调用者捕获处理
         */
        public static void write(HttpServletResponse response, Object message) throws IOException {
            response.setContentType("application/json;charset=UTF-8");
            ObjectMapper mapper = new ObjectMapper();
    
            try (
                    PrintWriter writer = response.getWriter();
            ) {
                writer.write(mapper.writeValueAsString(message));
                writer.flush();
            }
        }
    
        /**
         * 下载文件
         * 
         * @param response HTTP响应对象
         * @param file 需要下载的文件
         * @throws IOException 抛出异常,由调用者捕获处理
         */
        public static void download(HttpServletResponse response, File file) throws IOException {
            String fileName = file.getName();
            try (
                    FileInputStream in = new FileInputStream(file);
            ) {
    
                download(response, in, fileName);
            }
        }
    
        /**
         * 下载文件
         * 
         * @param response HTTP响应对象
         * @param data 需要下载的文件二进制内容
         * @param fileName 下载文件名
         * @throws IOException 抛出异常,由调用者捕获处理
         */
        public static void download(HttpServletResponse response, byte[] data, String fileName) throws IOException {
            try (
                    ByteArrayInputStream in = new ByteArrayInputStream(data);
            ) {
    
                download(response, in, fileName);
            }
        }
    
        /**
         * 下载文件
         * 
         * @param response HTTP响应对象
         * @param in 输出流
         * @param fileName 下载文件名
         * @throws IOException 抛出异常,由调用者捕获处理
         */
        public static void download(HttpServletResponse response, InputStream in, String fileName) throws IOException {
            try (
                    OutputStream out = response.getOutputStream();
            ) {
    
                // 对文件名进行URL转义,防止中文乱码
                fileName = URLEncoder.encode(fileName, "UTF-8");
    
                // 空格用URLEncoder.encode转义后会变成"+",所以要替换成"%20",浏览器会解码回空格
                fileName = fileName.replace("+", "%20");
    
                // "+"用URLEncoder.encode转义后会变成"%2B",所以要替换成"+",浏览器不对"+"进行解码
                fileName = fileName.replace("%2B", "+");
                response.setContentType("application/x-msdownload;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    
                byte[] bytes = new byte[4096];
                int len = -1;
                while ((len = in.read(bytes)) != -1) {
                    out.write(bytes, 0, len);
                }
                out.flush();
            }
        }
    
    }

     

    HTTP 下载文件工具类

    .

  • 相关阅读:
    关于编码问题
    期中架构之前所有的命令-总结
    Bootstrap表格添加搜索栏
    Bootstrap表格分页(二)
    Bootstrap表格分页(一)
    Entity Framework 分页处理
    Protocol Buffers v3.0.0编译安装小记
    golang学习笔记
    Java 对象生命周期
    Java 操作符
  • 原文地址:https://www.cnblogs.com/jonban/p/10372033.html
Copyright © 2011-2022 走看看