Clojure 将图像表单clj http请求保存到文件

Clojure 将图像表单clj http请求保存到文件,clojure,Clojure,我正在尝试保存使用下载的文件 我有以下代码: (def test-file (cl/get "http://placehold.it/350x150")) (defn write-file [] (with-open [w (clojure.java.io/writer "test-file.gif" :append true)] (.write w (:body test-file)))) 当我尝试将其设置为字节数组时,会出现一个异常: user=>

我正在尝试保存使用下载的文件

我有以下代码:

(def test-file
  (cl/get "http://placehold.it/350x150"))

(defn write-file []
   (with-open [w (clojure.java.io/writer  "test-file.gif" :append true)]
(.write w (:body test-file))))
当我尝试将其设置为字节数组时,会出现一个异常:

       user=>     (def test-file
                    (cl/get "http://placehold.it/350x150" {:as :byte-array}))
       #'user/test-file
       user=> (write-file)
       IllegalArgumentException No matching method found: write for class java.io.BufferedWriter  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:79)
救命啊

使用二进制输出

(def test-file
  (client/get "http://placehold.it/350x150" {:as :byte-array}))

(defn write-file []
   (with-open [w (java.io.BufferedOutputStream. (java.io.FileOutputStream. "test-file.gif"))]
     (.write w (:body test-file))))
编辑: 输出流更好:

(defn write-file []
   (with-open [w (clojure.java.io/output-stream "test-file.gif")]
     (.write w (:body test-file))))
更新:

优雅的方式:

(clojure.java.io/copy
 (:body (client/get "http://placehold.it/350x150" {:as :stream}))
 (java.io.File. "test-file.gif"))

更重要的是,writer.write需要char[]和outputstream.write需要byte[],但您可能应该使用clojure.java.io/output-stream虽然您可以使用clj http,但这完全可以通过clojure.java.io完成: