Hi
I am pretty new to java world so as i found there is no simple way to get 
image file from url like : http://example.com/image.jpg.
What i need just to get file from url and store it to db.
So we need to use buffer, stream and stuff to get file. I found this code 
but i guess there is a way to get things simplier.

(defn fetch-data [url]
  (let  [con    (-> url java.net.URL. .openConnection)
         fields (reduce (fn [h v] 
                          (assoc h (.getKey v) (into [] (.getValue v))))
                        {} (.getHeaderFields con))
         size   (first (fields "Content-Length"))
         in     (java.io.BufferedInputStream. (.getInputStream con))
         out    (java.io.BufferedOutputStream. 
                 (java.io.FileOutputStream. "out.file")) ; *Here is our 
file *
         buffer (make-array Byte/TYPE 1024)]

; Not sure about that loop it's just prints size to repl if we don't need 
that we can omit that part i guess

    (loop [g (.read in buffer)
           r 0]
      (if-not (= g -1)
        (do
          (println r "/" size) 
          (.write out buffer 0 g)
          (recur (.read in buffer) (+ r g)))))

    (.close in)
    (.close out)
    (.disconnect con)))

(fetch-data "http://google.com";)


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to