将图片和Base64互相转换

package com.andis.app.util;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * 图片转换Base64工具类
 *
 * @author Fetter
 * @version v1.0
 * @date 2021/10/17 18:53
 */
public class BaseImgUtil {
    //       String imgIo= Base64UtilTest.GetImageBase64Str();
//       Base64UtilTest.CreateImage(imgIo);
    String filePath = "D:\\document\\testimg\\操作日志s.PNG";
    String fileName = filePath.substring(filePath.lastIndexOf(".") + 1);


    public static boolean checkSuffix(String fileSuffix) {
        String suffix = "jpg,jpeg,png,PNG,JPG,JPEG";
        if (!suffix.contains(fileSuffix)) {
            return false;
        }
        return true;
    }

    // 图片转化成base64字符串---将图片文件转化为字节数组字符串,并对其进行Base64编码处理
    public static String GetImageBase64Str(File imgFile) {//
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    // base64字符串转化成图片---对字节数组字符串进行Base64解码并生成图片
    public static boolean CreateImage(String imgStr) {
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            String imgFilePath = "D:\\document\\testimg\\tests.PNG";// 新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }
    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

你可能感兴趣的:(Java,java)