base64 InputStream互转

1、InputStream转base64

public static String getBase64FromInputStream(InputStream in) {
    // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
    byte[] data = null;
    // 读取图片字节数组
    try {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = in.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        data = swapStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
              System.out.println(ExceptionUtils.getStackTrace(e));
            }
        }
    }
    return new String(Base64.getEncoder().encode(data));
}

2、base64转InputStream

public static InputStream baseToInputStream(final byte[] base64byte){
    ByteArrayInputStream stream1 = null;
    try {
        byte[] bytes1 = Base64.getEncoder().encode(base64byte);
        stream = new ByteArrayInputStream(bytes1);
    } catch (Exception e) {
        System.out.println(ExceptionUtils.getStackTrace(e));
    }
    return stream1;
}

3、base64转文件

public static String decryptByBase64(String base64, String filePath) {
    if (base64 == null && filePath == null) {
        return "生成文件失败,请给出相应的数据。";
    }
    try {
        Files.write(Paths.get(filePath), Base64.getDecoder().decode(base64), StandardOpenOption.CREATE);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "指定路径下生成文件成功!";
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值