base64字符串与正常文本文件之间的转换

文章介绍了如何利用Java的Hutool库将文本文件转换为Base64字符串,便于在接口中传输。同时,也展示了如何使用ApacheCommonsCodec库将Base64字符串还原为文本文件,特别提到了对于大量数据的处理,ApacheCommonsCodec可能更为适合。
摘要由CSDN通过智能技术生成

很多时候为了传输文本文件方便,会提供给获取方接口,让获取方调用接口来获得我们事先写好的文件,在这种情况下,其实可以把文件转换成base64字符串,用json中的其中一个字段存放这个文件,也就是base64字符串;

   以下是文件转换成base64字符串,filePah是我们的文本文件,fileContent就是我们所转换出来的base64字符串,这里的FileUtil和base64都是使用的hutool的工具类

        // 判断文件是否存在,如果不存在则报错
        File file = new File(filePath);
        if (!file.exists()) {
            // 返回错误信息
            LogUtil.error("文件不存在!");
        }
        // 把文件转成base64编码值
        byte[] fileBytes = FileUtil.readBytes(file);

        String fileContent = Base64.encode(fileBytes);

接下来的把base64字符串转换成文本文件,值得一提的是,这里用的base64是阿帕奇的包,因为hutool的base64包进行base64转换成文本文件的话对于数量大点的文件可能会解析出错。

代码里的待转换的base64文件.txt就是只放了base64字符串的一个txt文件,getFileContent()方法是把这个txt文件转换成base64字符串。如果你一开始就有base64字符串的话可以跳过这一步。

package com.hnykx.lottery.controller.backend;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

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

public class TestSettle {

    public static void main(String[] args) {
        // 读取base64字符串
        String base64String = getFileContent("D:\\待转换的base64文件.txt");
        // 解码
        byte[] bytes = decodeBase64(base64String);
        // 将字节数组写出到文件
        String filePath = "D:\\生成的文件.txt";
        writeToFile(bytes, filePath);
    }

    /**
     * 读取文件内容
     * @param filePath 文件路径
     * @return 文件内容字符串
     */
    public static String getFileContent(String filePath) {
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return builder.toString();
    }

    /**
     * 将Base64字符串解码为字节数组
     * @param base64String 待解码的Base64字符串
     * @return 解码后的字节数组
     */
    public static byte[] decodeBase64(String base64String) {
        return Base64.decodeBase64(base64String);
        // Base64.getDecoder().decode(base64String);
    }

    /**
     * 将字节数组写出到文件
     * @param bytes 字节数组
     * @param filePath 文件路径
     */
    public static void writeToFile(byte[] bytes, String filePath) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bos.write(bytes);
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

希望本文能帮助到你。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值