在 Clojure 中,如何从 Web 下载图像并将其保存到文件系统? [英] In Clojure, how do you download an image from the web and save it to your file system?
问题描述
如何使用 Clojure 从 Web 下载图像并将其保存到文件系统?我知道图像 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.
我想尽可能简单地做到这一点,理想情况下就像吐痰和啜饮一样.也就是说,没有很多额外的行使用缓冲区或字节数组.我想在完成后关闭流,但我不在乎它是否效率低下.
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 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)))
正如智通所说,你需要在你的命名空间中使用 (: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屋!