java File base64流 byte[] 的相互转换

工作中经常用到文件与base64 等其他状态的互相转换,每次都记不住,只好把各种文件的转换方法写个博客记录下来,方便下次使用的时候,直接复制就好。

File转Base64

//文件路径转换base64
Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get("../data/1.jpg")))


Base64转File

Files.write(Paths.get("../data/1.jpg"), Base64.getDecoder().decode(base64),StandardOpenOption.CREATE);

File转InputStream

InputStream in = new FileInputStream(File);

InputStream转File

Files.copy(inputStream,new File("/data/2.jpg").toPath(),StandardCopyOption.REPLACE_EXISTING);

Base64转byte[]

//1.java8
byte[] bytes = Base64.getDecoder().decode(base64)

//2.
byte[] bytes = new BASE64Decoder().decodeBuffer(base64)

byte[]转Base64

byte[] bytes = Files.readAllBytes(Paths.get("../data/1.jpg");
String base64 =  Base64.getEncoder().encodeToString(bytes);

File转byte[]

//1.文件路径获取
byte[] bytes = Files.readAllBytes(Paths.get("../data/1.jpg");

//2.File获取
byte[] bytes = null;
BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(file)).write(bytes);

byte[]转File

Files.write(Paths.get("../data/2.jpg"),bytes , StandardOpenOption.CREATE);

文件复制

Files.copy (Paths.get("/data/1.jpg"),new FileOutputStream (new File ("/data/2.jpg")));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值