package com.sifang.modules.sys.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import com.sifang.modules.sys.common.utils.FileUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author DQChen
 * @date 2024-01-22 19:49
 */
public class EntryUtils {

    public static void downloadLocal(String fileName, String filePath, HttpServletResponse response) {
        // 创建OSSClient实例。
        BufferedInputStream input;
        OutputStream outputStream;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition","attachment; filename="+ fileName);
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            input = new BufferedInputStream(new FileInputStream(new File(filePath)));
            byte[] buffBytes = new byte[1024];
            outputStream = response.getOutputStream();
            int read;
            while ((read = input.read(buffBytes)) != -1) {
                outputStream.write(buffBytes, 0, read);
            }
            outputStream.flush();
        } catch (OSSException | ClientException | IOException oe) {
            oe.printStackTrace();
        }
    }

}
