package com.yuchen.saas.park.service.pull.untils;


import java.io.*;
import java.util.Base64;

/**
 * @author 吕海泉
 * @create 2020/11/25 13:45
 */
public class Base64Img {
    // 图片转化成base64字符串
    public static String GetImageStr(File imgFile) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        String base64 = null;
        // 读取图片字节数组
        try{
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            base64 = Base64.getEncoder().encodeToString(data);
        } catch (IOException  e) {
            e.printStackTrace();
        } finally {
            if(in != null) {
                try{
                    in.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }
    // base64字符串转化成图片
    public static File base64ToFile(String base64, String fileName) throws Exception {
        if(base64.contains("data:image")) {
            base64 = base64.substring(base64.indexOf(",")+1);
        }
        base64 = base64.toString().replace("\r\n", "");
        File file = null;
        // 创建文件目录
        String filePath="tem-frp";
        File dir = new File(filePath);
        if(!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try{
            byte[] bytes = Base64.getDecoder().decode(base64);
            file = new File(filePath+"\\"+fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos != null) {
                try {
                    bos.close();

                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  file;
    }

//    public static MultipartFile fileToMultipartFile(File file) {
//
//        if( file == null) {
//            return null;
//        }
//        File pdfFile = new File(String.valueOf(file));
//        InputStream fileInputStream = null;
//        MultipartFile multipartFile = null;
//        try{
//            fileInputStream = new FileInputStream(pdfFile);
//             multipartFile = new MockMultipartFile(pdfFile.getName(),fileInputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if(fileInputStream != null) {
//                try {
//                    fileInputStream.close();
//                }catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//
//        }
//
//        return multipartFile;
//    }

}
