Сохранение формы изображения запроса 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))))
и когда я пытаюсь сделать это как массив байтов, я получаю исключение:
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)
Помогите!
1 ответ
Решение
Использовать двоичный вывод.
(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"))