以下是使用Java进行Base64转换的例子:
- 使用Java Base64工具类进行转换
```java
// 导入相关包
import java.util.Base64;
import java.io.FileOutputStream;
import java.io.IOException;
// 将Base64字符串转换为字节数组
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
// 将字节数组写入到文件中
try (FileOutputStream fos = new FileOutputStream("output.doc")) {
fos.write(decodedBytes);
} catch (IOException e) {
e.printStackTrace();
}
2. 使用FileInputStream进行转换
```java
// 导入相关包
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
// 读取Base64字符串
String base64String = "your_base64_string";
// 将Base64字符串转换为字节数组
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
// 将字节数组写入到文件中
try (FileOutputStream fos = new FileOutputStream("output.doc")) {
fos.write(decodedBytes);
} catch (IOException e) {
e.printStackTrace();
}
AI 代码解读