java中Base64编码转图片

import sun.misc.BASE64Decoder;
import java.io.*;

/**
 * Base64编码转图片
 * 
 */
public class Base64TransitionToImageUtil {
    public static void main(String[] args) {
        String base64Str="";//base64字符串
        String parentPath="";//图片存放路径
        String fileName="";//图片存放路径名称
        base64StrToFile(base64Str,fileName,parentPath);
    }

    public static File base64StrToFile(String base64Str,String fileName,String parentPath){
        File file = new File(parentPath,fileName);
        FileOutputStream out = null;
        try {
            byte[] bytes1 = new BASE64Decoder().decodeBuffer(base64Str);
            ByteArrayInputStream in = new ByteArrayInputStream(bytes1);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(file);
            int byteSum = 0;
            int byteRead = 0;
            while ((byteRead = in.read(buffer)) != -1){
                byteSum += byteRead;
                out.write(buffer,0,byteRead);
            }
        }catch (Exception ex){
            throw new RuntimeException("transform base64 String into file 出错",ex);
        }finally {
            try {
                if(null != out){
                    out.close();
                }
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
        return file;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值