hello

Base64工具类

提供base64文本互转,文件和base64互转

点击查看代码
package com.fch.demo.utiils;

import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Base64;
import java.util.Date;

/**
 * @author LHW
 */
public class Base64Util {
    public static void main(String[] args) throws Exception {
        byte[] decodeStr = Base64Util.decryptBase64("加密串");
        String jsonString = new String(decodeStr,"UTF-8");
        System.out.println(jsonString);
    }

    /**
     * BASE64解密
     */
    public static byte[] decryptBase64(String key) throws Exception {
        return (new BASE64Decoder()).decodeBuffer(key);
    }

    /**
     * BASE64加密
     */
    public static String encryptBase64(byte[] key) throws Exception {
        return (new BASE64Encoder()).encodeBuffer(key);
    }

    /**
     * base64字符串转文件
     */
    public static File base64ToFile(String base64) {
        File file = null;
        String fileName = DateUtil.format(new Date(), "yyyyMMddhhmmss") + System.currentTimeMillis()+"_tmp.txt";
        FileOutputStream out = null;
        try {
            // 解码,然后将字节转换为文件
            file = new File(fileName);
            if (!file.exists()) {
                //noinspection ResultOfMethodCallIgnored
                file.createNewFile();
            }
            // 将字符串转换为byte数组
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes = decoder.decodeBuffer(base64);
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(file);
            int byteread;
            while ((byteread = in.read(buffer)) != -1) {
                // 文件写操作
                out.write(buffer, 0, byteread);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }


    /**
     * 文件转base64字符串
     */
    public static String fileToBase64(File file) {
        String base64 = null;
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();

            base64 = Base64.getEncoder().encodeToString(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64;
    }

    /**
     * base64 转 文件
     *
     * @param ofdFilePath
     * @param fileName
     */
    public static void stringToOfd(String base64, String ofdFilePath, String fileName) {

        byte[] decodeBase64 = new byte[0];
        try {
            decodeBase64 = new BASE64Decoder().decodeBuffer(base64);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (decodeBase64 != null) {
            File directory = new File(ofdFilePath);
            if (!directory.exists() || !directory.isDirectory()) {
                directory.mkdir();
            }
            //String filepath =ofdFilePath + fileName;
            File file = new File(ofdFilePath + fileName);
            if (file.exists()) {
                file.delete();
            }

            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(decodeBase64, 0, decodeBase64.length);
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

posted @ 2022-02-14 16:02  八股文研究生  阅读(478)  评论(0)    收藏  举报
my name is zhangsan