Base64文件互转(图片,视频,文本,字符串)

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Description Base64工具处理类
 * @Author WangKun
 * @Date 2023/10/9 15:37
 * @Version
 */
@Slf4j
public class Base64Utils {

    final static String[] VIDEO_ARRAY = {".avi", ".wmv", ".mpg", ".mpeg", ".mov", ".rm", ".ram", ".swf", ".flv", ".mp4", ".3gp"};
    final static String[] PICTURE_ARRAY = {".png", ".gif", ".jpg", ".jpeg"};

    /**
     * @param str
     * @Description 字符串转Base64
     * @Throws
     * @Return java.lang.String
     * @Date 2023-10-09 17:35:02
     * @Author WangKun
     */
    public static String strToBase64(String str) {
        return Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * @param base64Str
     * @Description Base64转字符串
     * @Throws
     * @Return java.lang.String
     * @Date 2023-10-09 17:37:43
     * @Author WangKun
     */
    public static String base64ToStr(String base64Str) {
        return new String(Base64.getDecoder().decode(base64Str));
    }

    /**
     * @param filePath
     * @Description 文件转Base64编码(带文件后缀 ” 1.txt “)
     * @Throws
     * @Return java.lang.String
     * @Date 2023-10-09 16:16:07
     * @Author WangKun
     */
    public static String fileToBase64(String filePath) {
        try {
            InputStream inputStream = FileUtils.openInputStream(new File(filePath));
            return new String(Base64.getEncoder().encode(IOUtils.toByteArray(inputStream)), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("文件转Base64{} 异常", e.getMessage(), e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param base64Str
     * @param filePath
     * @Description Base64编码转文件(带文件后缀 ” 1.txt “)
     * @Throws
     * @Return void
     * @Date 2023-10-09 16:43:00
     * @Author WangKun
     */
    public static void base64ToFile(String base64Str, String filePath) {
        try {
            InputStream is = new ByteArrayInputStream(Base64.getDecoder().decode(base64Str.getBytes(StandardCharsets.UTF_8)));
            FileOutputStream fos = new FileOutputStream(filePath);
            byte[] bytes = new byte[is.available()];
            int x = -1;
            while ((x = is.read(bytes)) != -1) {
                fos.write(bytes);
            }
            is.close();
            fos.close();
        } catch (IOException e) {
            log.error("Base64转文件{} 异常", e.getMessage(), e);
            e.printStackTrace();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值