将Base64编码解码为文件groovy

8
尝试使用Groovy解码base64并将其写入文件
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()

这将创建一个类似于[-121,25,-180....]的byte[]值,并打印到文件中。如何将原始数据导入文件中。


你好,能否分享一下在Groovy中对文件进行编码和解码的代码? - td4u
2个回答

8
您可以使用二进制流代替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())

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接