将图像表单 clj-http 请求保存到文件 [英] Saving an image form clj-http request to file
本文介绍了将图像表单 clj-http 请求保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试保存使用 clj-http
我有以下代码:
(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))))
当我尝试将其作为字节数组时,出现异常:
and when I try to make it as a byte-array, I get an exception:
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"))
这篇关于将图像表单 clj-http 请求保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文