package com.ruoyi.common.utils;

import java.io.*;

public class FileUtil {

    public static void main(String[] args) {
        String s2 = readAsBase64FromFile("C:\\Users\\EDY\\Desktop\\file\\image.png");
        String s = "data:image/png;base64," + s2;

        System.out.println("原图片：" + s);
        String substring = s.substring(s.indexOf(",") + 1);
        String s1 = Base64Util.resizeImageLimitSize(substring, 200);
        String s3 = "data:image/png;base64," + s1;
        System.out.println("压缩后图片：" +s3);
    }

    /**
     * 读入文件转换为Base64编码并返回
     *
     * @param filePath 文件路径
     * @return base64 的文件
     * @throws Exception
     */
    public static String readAsBase64FromFile(String filePath) {
        InputStream in = null;
        byte[] data = null;

        try {
            in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return Base64Util.encode(data);
    }


    /**
     * 将Base64编码的字符串转为文件输出
     *
     * @param base64String 字符串
     * @param path         输出文件保存路径
     * @param fileName     输出文件名称
     * @return 是否转换成功
     */
    public static boolean writeFileFromBase64(String base64String, String path, String fileName) {
        if (base64String == null) {
            return false;
        }
        try {
            byte[] b = Base64Util.decode(base64String);
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            OutputStream out = new FileOutputStream(path + File.separator + fileName);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 文件byte[]类型转File
     *
     * @param bytes    bytes
     * @param outPath  输出目录
     * @param fileName 文件名
     * @return
     */
    public static File bytesToFile(byte[] bytes, String outPath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(outPath);
            if (!dir.exists() && dir.isDirectory()) { //判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(outPath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }
}
