java base64转文件
时间: 2023-08-25 18:05:50 浏览: 274
将Base64编码的字符串转换为文件可以按以下步骤进行:
1. 将Base64编码的字符串转换为字节数组。
```java
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
```
2. 创建输出文件流。
```java
FileOutputStream fos = new FileOutputStream("outputFile");
```
3. 将字节数组写入文件。
```java
fos.write(decodedBytes);
```
4. 关闭输出流。
```java
fos.close();
```
完整代码示例:
```java
import java.io.FileOutputStream;
import java.util.Base64;
public class Base64ToFile {
public static void main(String[] args) throws Exception {
String base64String = "WW91IGRpZCB5b3VyIHRlc3Qh";
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
FileOutputStream fos = new FileOutputStream("outputFile");
fos.write(decodedBytes);
fos.close();
}
}
```
注意:这里的Base64是Java8中自带的,如果是Java7及以下版本需要使用第三方库来实现。