java 字符串转base64
时间: 2024-01-07 20:04:17 浏览: 173
Java Base64位编码与String字符串的相互转换,Base64与Bitmap的相互转换实例代码
以下是Java中字符串转换为Base64的示例代码:
```java
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// 将字符串转为Base64
String base64String = Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
System.out.println("Base64 encoded string: " + base64String);
// 将Base64字符串解析成原来的内容
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
String decodedString = new String(decodedBytes, StandardCharsets.UTF_8);
System.out.println("Decoded string: " + decodedString);
}
}
```
阅读全文