In Clojure, how do you download an image from the web and save it to your file system?

后端 未结 1 1735
臣服心动
臣服心动 2021-01-17 09:35

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 sl

相关标签:
1条回答
  • 2021-01-17 10:20

    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)))
    

    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)))
    
    0 讨论(0)
提交回复
热议问题