HTTP 下載文件工具類

 

ResponseUtils.javahtml

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 下載文件工具類java

.json

相關文章
相關標籤/搜索