springboot Base64Util
时间: 2023-06-18 19:06:51 浏览: 64
在Spring Boot中使用Base64编码和解码可以使用Java自带的Base64类或者Apache Commons Codec库中的Base64类。
使用Java自带的Base64类:
1. 编码:
```java
String str = "Hello World";
byte[] encodedBytes = Base64.getEncoder().encode(str.getBytes());
String encodedStr = new String(encodedBytes);
System.out.println("Encoded String: " + encodedStr); // SGVsbG8gV29ybGQ=
```
2. 解码:
```java
String encodedStr = "SGVsbG8gV29ybGQ=";
byte[] decodedBytes = Base64.getDecoder().decode(encodedStr.getBytes());
String decodedStr = new String(decodedBytes);
System.out.println("Decoded String: " + decodedStr); // Hello World
```
使用Apache Commons Codec库中的Base64类:
1. 添加依赖:
```xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
```
2. 编码:
```java
String str = "Hello World";
byte[] encodedBytes = Base64.encodeBase64(str.getBytes());
String encodedStr = new String(encodedBytes);
System.out.println("Encoded String: " + encodedStr); // SGVsbG8gV29ybGQ=
```
3. 解码:
```java
String encodedStr = "SGVsbG8gV29ybGQ=";
byte[] decodedBytes = Base64.decodeBase64(encodedStr.getBytes());
String decodedStr = new String(decodedBytes);
System.out.println("Decoded String: " + decodedStr); // Hello World
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)