MENU

Java8 Base64编码、解码

概述

Java8的java.util包中新增了Base64类用于处理Base64的编码和解码

使用方法

分步使用

// 解码器
Base64.Decoder decoder = Base64.getDecoder();
// 编码器
Base64.Encoder encoder = Base64.getEncoder();

// 编码
String text = "https://blog.wanvale.com";
byte[] encodeResult = encoder.encode(text.getBytes(StandardCharsets.UTF_8));
String encodeText = new String(encodeResult, StandardCharsets.UTF_8);
System.out.println("encodeText = " + encodeText);

// 解码
byte[] decodeResult = decoder.decode(encodeText.getBytes(StandardCharsets.UTF_8));
String decodeText = new String(decodeResult, StandardCharsets.UTF_8);
System.out.println("decodeText = " + decodeText);

简略写法

// 编码
String text1 = "https://www.wanvale.com";
String encode1 = Base64.getEncoder().encodeToString(text1.getBytes(StandardCharsets.UTF_8));
System.out.println("encode1 = " + encode1);
// 解码
String decode1 = new String(Base64.getDecoder().decode(encode1), StandardCharsets.UTF_8);
System.out.println("decode1 = " + decode1);

文章标题:Java8 Base64编码、解码
如果文中内容侵犯了您的权益,请及时与博主取得联系进行删除!
本站文章未经许可禁止转载,本文地址:https://blog.wanvale.com/archives/424/

Archives QR Code Tip
QR Code for this page
Tipping QR Code