gpt4 book ai didi

groovy - Base64 解码到文件 groovy

转载 作者:行者123 更新时间:2023-12-04 14:11:55 26 4
gpt4 key购买 nike

尝试解码 base64 并使用 groovy 将其写入文件

File f = new File("c:\\document1.doc")
PrintWriter writer = null
byte[] b1 = Base64.decodeBase64(info.getdata());
writer = new PrintWriter(f)
writer.print(b1)
writer.close()

这会创建一个 byte[] 值,如 [-121,25,-180....] 打印到文件。
如何获取原始数据到文件中。

最佳答案

您可以使用二进制流代替 Writer :

File f = new File("c:\\document1.doc")
FileOutputStream out = null
byte[] b1 = Base64.decodeBase64(info.getdata());
out = new FileOutputStream(f)
try {
out.write(b1)
} finally {
out.close()
}

但更简单的是使用 Groovy JDK 扩展 File.setBytes :
new File("c:\\document1.doc").bytes = Base64.decodeBase64(info.getdata())

关于groovy - Base64 解码到文件 groovy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28884795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com