FAQ

[Clojure] Simple way to get image from url

Yakovlev Roman
Oct 15, 2012 at 9:13 am
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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
reply

Search Discussions

9 responses

  • Dennis zhuang at Oct 15, 2012 at 9:30 am
    I think you can use clj-http:
    https://github.com/dakrone/clj-http

    (client/get "http://site.com/favicon.ico" {:as :byte-array})



    2012/10/15 Yakovlev Roman <fel...@...com>
    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 clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en



    --
    庄晓丹
    Email: kil...@...com xzh...@...com
    Site: http://fnil.net
    Twitter: @killme2008

    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Andreas Liljeqvist at Oct 15, 2012 at 10:29 am
    Haven't got access to my tools, but couldn't you just slurp it?

    (slurp "http://somesite/picture.jpg")
    On Mon, Oct 15, 2012 at 11:08 AM, dennis zhuang wrote:

    I think you can use clj-http:
    https://github.com/dakrone/clj-http

    (client/get "http://site.com/favicon.ico" {:as :byte-array})



    2012/10/15 Yakovlev Roman <fel...@...com>
    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 clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en



    --
    庄晓丹
    Email: kil...@...com xzh...@...com
    Site: http://fnil.net
    Twitter: @killme2008



    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Yakovlev Roman at Oct 15, 2012 at 1:23 pm
    slurp works with text only as i know
    On Monday, October 15, 2012 2:29:41 PM UTC+4, Andreas Liljeqvist wrote:

    Haven't got access to my tools, but couldn't you just slurp it?

    (slurp "http://somesite/picture.jpg")

    On Mon, Oct 15, 2012 at 11:08 AM, dennis zhuang <killm...@gmail.com<javascript:>
    wrote:
    I think you can use clj-http:
    https://github.com/dakrone/clj-http

    (client/get "http://site.com/favicon.ico" {:as :byte-array})



    2012/10/15 Yakovlev Roman <feli...@gmail.com <javascript:>>
    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 clo...@googlegroups.com<javascript:>
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+u...@googlegroups.com <javascript:>
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en



    --
    庄晓丹
    Email: killm...@gmail.com <javascript:> xzh...@avos.com<javascript:>
    Site: http://fnil.net
    Twitter: @killme2008



    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clo...@googlegroups.com<javascript:>
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+u...@googlegroups.com <javascript:>
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • AtKaaZ at Oct 15, 2012 at 5:01 pm
    => *(use 'clj-http.client)*
    nil
    => *(= (:body (clj-http.client/get "http://google.com/favicon.ico" {:as
    :steam})) (slurp "http://google.com/favicon.ico"))*
    true

    or if you want to save it locally as a file(thanks Apage43):
    => *(with-open [bodystream (:body (clj-http.client/get "
    http://google.com/favicon.ico" {:as :stream}))]
    (clojure.java.io/copy bodystream
    (clojure.java.io/file"google_favicon.ico"))
    )*
    nil
    On Mon, Oct 15, 2012 at 12:29 PM, Andreas Liljeqvist wrote:

    Haven't got access to my tools, but couldn't you just slurp it?

    (slurp "http://somesite/picture.jpg")
    On Mon, Oct 15, 2012 at 11:08 AM, dennis zhuang wrote:

    I think you can use clj-http:
    https://github.com/dakrone/clj-http

    (client/get "http://site.com/favicon.ico" {:as :byte-array})



    2012/10/15 Yakovlev Roman <fel...@...com>
    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 clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en



    --
    庄晓丹
    Email: kil...@...com xzh...@...com
    Site: http://fnil.net
    Twitter: @killme2008



    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Dave Ray at Oct 15, 2012 at 3:36 pm
    Something like this perhaps:

    (with-open [in (clojure.java.io/input-stream "http://google.com/favicon.ico")]
    (clojure.java.io/copy in (clojure.java.io/file "favicon.ico")))

    Dave
    On Mon, Oct 15, 2012 at 6:23 AM, AtKaaZ wrote:
    => (use 'clj-http.client)
    nil
    => (= (:body (clj-http.client/get "http://google.com/favicon.ico" {:as
    :steam})) (slurp "http://google.com/favicon.ico"))
    true

    or if you want to save it locally as a file(thanks Apage43):
    => (with-open [bodystream (:body (clj-http.client/get
    "http://google.com/favicon.ico" {:as :stream}))]
    (clojure.java.io/copy bodystream (clojure.java.io/file
    "google_favicon.ico")))
    nil

    On Mon, Oct 15, 2012 at 12:29 PM, Andreas Liljeqvist wrote:

    Haven't got access to my tools, but couldn't you just slurp it?

    (slurp "http://somesite/picture.jpg")

    On Mon, Oct 15, 2012 at 11:08 AM, dennis zhuang <kil...@...com>
    wrote:
    I think you can use clj-http:
    https://github.com/dakrone/clj-http

    (client/get "http://site.com/favicon.ico" {:as :byte-array})



    2012/10/15 Yakovlev Roman <fel...@...com>
    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 clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en



    --
    庄晓丹
    Email: kil...@...com xzh...@...com
    Site: http://fnil.net
    Twitter: @killme2008



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

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

    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your
    first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Yakovlev Roman at Oct 15, 2012 at 2:44 pm
    it gets whole session so i guess our data in :body and then what to do with
    that array
    I need something like a curl:
    curl http://example.com/img.jpg > img.jpg
    get file via url and put it to local directory...

    понедельник, 15 октября 2012 г., 13:08:45 UTC+4 пользователь dennis написал:
    I think you can use clj-http:
    https://github.com/dakrone/clj-http

    (client/get "http://site.com/favicon.ico" {:as :byte-array})



    2012/10/15 Yakovlev Roman <feli...@gmail.com <javascript:>>
    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 clo...@googlegroups.com<javascript:>
    Note that posts from new members are moderated - please be patient with
    your first post.
    To unsubscribe from this group, send email to
    clojure+u...@googlegroups.com <javascript:>
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en



    --
    庄晓丹
    Email: killm...@gmail.com <javascript:> xzh...@avos.com<javascript:>
    Site: http://fnil.net
    Twitter: @killme2008


    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Yakovlev Roman at Oct 16, 2012 at 10:38 am
    Ok that were good examples thanks
    Final variant

    add to project.clj

    [clj-http "0.5.6"]

    add ref to lib in ns

    (ns myapp.app
    *(:require [clj-http.client :as client] ))*

    function

    (defn write-file [url]
    (with-open [w (clojure.java.io/output-stream "img.jpg" )] ; output file
    (.write w (:body (client/get url {:as :byte-array})))) ; here we get
    image as byte-array
    ))

    Which trlanslates to ( i hope i get it right ) :
    Open file in stream mode (w) and write to it byte stream (image file ) from
    url.
    On Monday, October 15, 2012 1:06:19 PM UTC+4, Yakovlev Roman wrote:

    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 clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Zhitong He at Oct 16, 2012 at 12:49 pm
    another way to solve this, from
    http://users.utu.fi/machra/posts/2011-08-24-2-reddit-clojure.html

    (ns myapp.app
    (:require [clojure.java.io :as io]))

    (defn copy [uri file]
    (with-open [in (io/input-stream uri)
    out (io/output-stream file)]
    (io/copy in out)))


    On Tue, Oct 16, 2012 at 6:32 PM, Yakovlev Roman wrote:
    Ok that were good examples thanks
    Final variant

    add to project.clj

    [clj-http "0.5.6"]

    add ref to lib in ns

    (ns myapp.app
    (:require [clj-http.client :as client] ))

    function

    (defn write-file [url]
    (with-open [w (clojure.java.io/output-stream "img.jpg" )] ; output file
    (.write w (:body (client/get url {:as :byte-array})))) ; here we get
    image as byte-array
    ))

    Which trlanslates to ( i hope i get it right ) :
    Open file in stream mode (w) and write to it byte stream (image file ) from
    url.

    On Monday, October 15, 2012 1:06:19 PM UTC+4, Yakovlev Roman wrote:

    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 clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your
    first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en


    --
    Zhitong He
    Sun Yat-sen University

    --
    You received this message because you are subscribed to the Google
    Groups "Clojure" group.
    To post to this group, send email to clojure@googlegroups.com
    Note that posts from new members are moderated - please be patient with your first post.
    To unsubscribe from this group, send email to
    clojure+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Jim foo.bar at Oct 16, 2012 at 12:29 pm

    On 16/10/12 13:25, Zhitong He wrote:
    another way to solve this, from
    http://users.utu.fi/machra/posts/2011-08-24-2-reddit-clojure.html

    (ns myapp.app
    (:require [clojure.java.io :as io]))

    (defn copy [uri file]
    (with-open [in (io/input-stream uri)
    out (io/output-stream file)]
    (io/copy in out)))
    this looks nice...also, no dependencies - you can run this on your android!

    Jim

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

Related Discussions