使用 Clojure 将 base64 编码的文件解码回原始格式
如何将经过 Base64 编码的文件转换回其原始格式并将其写入磁盘?例如,我有一个已进行 mime64 编码的 pdf 文件。该文件开头为:
data:application/pdf;base64,JVBER
我想以正确的格式将其写入磁盘。我已经尝试了几个将字符串解码为字节数组的库(例如ring.util.codec),但是如果我将生成的字节数组写入文件(使用spit),则文件会显示损坏。
更新:
PHP 函数 base64_decode 似乎正在做我正在寻找的事情,因为它返回一个字符串。 Java 中的等价物是什么?
How does one convert a file that has been base64 encoded back to its original format and write it to disk? For instance I have a pdf file which has been mime64 encoded. The file starts with:
data:application/pdf;base64,JVBER
I would like to write this out to disk in the proper format. I have tried several libraries (e.g. ring.util.codec) that decode the string into a byte-array, but if I write the resulting byte-array out to a file (using spit) the file appears corrupted.
UPDATE:
The PHP function base64_decode appears to be doing what I am looking for, as it returns a string. What is the equivalent in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Clojure 中,有
data.codec
(以前在 clojure-contrib )。使用 Java 互操作性:
javax .xml.bind.DatatypeConverter/parseBase64Binary
和javax. xml.bind.DatatypeConverter/printBase64Binary
所以这些是我在使用
data.codec
时用于图像的辅助函数:In Clojure, there is
data.codec
(formerly in clojure-contrib).Using Java interoperability :
javax.xml.bind.DatatypeConverter/parseBase64Binary
andjavax.xml.bind.DatatypeConverter/printBase64Binary
So those are the helper functions I used for images when using
data.codec
:任何java库都应该可以工作(这里是一个,来自 Apache Commons,这是一个完全用 Clojure 编写的 Clojure-contrib
我怀疑内容被以某种方式修改,这意味着字节可能会使用某种编码转换为字符串,然后尝试使用不同的编码将该字符串读回字节
。可能是为了检查服务器端的文件和您尝试读取的文件是否具有完全相同的字节数。
另外,尝试确认校验和(MD5)是否相同。
无论如何,PDF 文件都是二进制文件,因此您不应将其转换为任何地方的字符串,而是直接字节。
Any java library should work (here's one, from Apache Commons, here's one totally in Clojure from Clojure-contrib
I suspect the content is modified somehow, meaning bytes may be converted to string using some encoding, and then trying to read this string back to bytes using a different encoding.
The first step may be to check you have the exact same number of bytes in the file on the server side, and the file you are trying to read.
Also, try to confirm the checksum (MD5) is the same.
In any case, a PDF file is a binary file, so you should NOT convert it to string anywhere, but straight bytes.