Base64FileUtils工具类

这是一个Java工具类,实现了文件到Base64字符串的转换以及Base64字符串解码回文件的功能。主要方法包括fileToBase64()用于文件转Base64,base64ToFile()用于Base64解码成File。提供了单元测试进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ruoyi.common.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

public class Base64FileUtils {
    /**
     *
     * @param path  文件全路径(加文件名)
     * @return String
     * @description 将文件转base64字符串
     * @date 2019年11月24日
     * @author rmk
     */
    public static String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            File file = new File(path);
            in = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            in.read(bytes);
            base64 = new String(Base64.encodeBase64(bytes),"UTF-8");
            System.out.println("将文件["+path+"]转base64字符串:"+base64);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }

    /**
     * @param outFilePath  输出文件路径,  base64   base64文件编码字符串,  outFileName  输出文件名
     * @return String
     * @description BASE64解码成File文件
     * @date 2019年11月24日
     * @author rmk
     */
    public static void base64ToFile(String outFilePath,String base64, String outFileName) {
        System.out.println("BASE64:["+base64+"]解码成File文件["+outFilePath+"\\"+outFileName+"]");
        File file = null;
        //创建文件目录
        String filePath=outFilePath;
        File  dir=new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.decodeBase64(base64);
            file=new File(filePath+"/"+outFileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception 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();
                }
            }
        }
    }

    /**
     * 单元测试
     */
    public static void main(String[] args) {

        //定义文件路径
        String filePath = "E:\\测试文件\\57bd4a8bcf82df651d3ce27849e4a679.mp4";
        //将文件转base64字符串
        String base64 = fileToBase64(filePath);

        System.out.println();
        //定义输出文件的路径outFilePath和输出文件名outoutFileName
        String outFilePath = "E:\\测试文件";
        String outFileName = "123.mp4";
        //将BASE64解码成File文件
        base64ToFile(outFilePath, base64, outFileName);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值