在Clojure中,如何从Web下载图像并将其保存到文件系统? [英] In Clojure, how do you download an image from the web and save it to your file system?
问题描述
如何从Web下载图像并使用Clojure将其保存到文件系统?我知道图像url,我知道我不能使用 spit
和 slurp
这样做,因为它二进制数据,而不是文本。
How do you download an image from the web and save it to your file system using Clojure? I know the image url and I'm aware that I can't use spit
and slurp
to do this because it's binary data, not text.
我想尽可能地做到这一点,理想情况下如何吐和slurp工作。也就是说,没有很多额外的行使用缓冲区或字节数组。我想关闭流,当我完成,但我不在乎如果它是低效的。
I'd like to do this as simply as possible, ideally like how spit and slurp work. That is, without a lot of extra lines using buffers or byte arrays. I want to close the streams when I'm done, but I don't care if it's inefficient.
推荐答案
Zhitong He 指向我到此解决方案,它最适合我的目的:
Zhitong He pointed me to this solution, which worked best for my purposes:
(defn copy [uri file]
(with-open [in (io/input-stream uri)
out (io/output-stream file)]
(io/copy in out)))
正如Zhitong所说,你需要(:require [clojure.java.io:as io])
在您的命名空间中使用它作为编码。或者,您可以直接参考clojure.java.io:
As Zhitong notes, you'll need (:require [clojure.java.io :as io])
in your namespace to use this as coded. Alternatively, you could refer to clojure.java.io directly:
(defn copy-uri-to-file [uri file]
(with-open [in (clojure.java.io/input-stream uri)
out (clojure.java.io/output-stream file)]
(clojure.java.io/copy in out)))
这篇关于在Clojure中,如何从Web下载图像并将其保存到文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!