To store a Base64-encoded string as a file, you can follow these steps:
- Decodes a Base64-encoded string into a byte array. You can use Java's Base64 classes to decode, for example:
import java.util.Base64;
String base64String = "your_base64_encoded_string";
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
- Specify the file path and file name to create a File object:
String filePath = "path_to_directory/file_name.ext";
File file = new File(filePath);
- Write the decoded byte array to a file using FileOutputStream:
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(decodedBytes); // 写入解码后的字节数组到文件
fos.flush(); // 刷新缓冲区
}
Now, the Base64 encoded string has been decoded and stored as a file.
Please ensure that the provided file path (filePath) is valid and has write permission for your application.