java-base64转file,file转base64

  • Base64Utils
package com.smart.hnj.common.utils;

import java.io.*;
import java.util.Base64;

public class Base64Utils {

    /**
     * base64转化为wav并生成到本地
     * @param base64
     * @param path
     * @return
     */
    public static boolean base64ToWav(String base64, String path) {
        Boolean flag = false;
        File newFile = new File(path);
        File parentFile = newFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();// 创建目录,带s多级,mkdir单个
        }
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            if (!newFile.exists()) {
                newFile.createNewFile();
            }
            byte[] bytes = Base64.getDecoder().decode(base64);
            fos = new java.io.FileOutputStream(newFile);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
            flag = true;
        } catch (Exception e) {
            flag = false;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    flag = false;
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    flag = false;
                }
            }
        }
        return flag;
    }

    /**
     * base64转化为file流
     * @param base64
     * @return
     */
    public static File base64ToFile(String base64) {
        if (base64 == null || "".equals(base64)) {
            return null;
        }
        byte[] buff = Base64.getDecoder().decode(base64);
        File file = null;
        FileOutputStream out = null;
        try {
            file = File.createTempFile("tmp", null);
            out = new FileOutputStream(file);
            out.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

    /**
     * file流转化为base64(未测试)
     * @param file
     * @return
     */
    public static String fileToBase64(File file) {
        if (file == null) {
            return null;
        }
        String base64 = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] buff = new byte[in.available()];
            in.read(buff);
            base64 = Base64.getEncoder().encodeToString(buff);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值