[WIKI] Added: Simple file download from URL

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[WIKI] Added: Simple file download from URL

Raffaele Castagno-3
How to download a file from within groovy, given an URL.

http://docs.codehaus.org/display/GROOVY/Simple+file+download+from+URL

--
        */Raffaele CASTAGNO
/*/Logistica & Retail/
/[hidden email]
<mailto:[hidden email]>/

/www.gruppotesi.com/

------------------------------------------------------------------------
*TESI S.p.A.*
Sede Legale: Via Mendicita' Istruita, 24 - 12042 Bra (CN)
Sede Operativa: Via Savigliano, 48 - 12060 Loc. Roreto di Cherasco (CN)
Tel. 0172 476301 - Fax 0172 476399 - [hidden email]

/
Disclaimer
Questo messaggio è rivolto unicamente al destinatario indicato e
potrebbe contenere informazioni riservate o confidenziali. Nel caso lo
avesse ricevuto per errore, ci scusiamo per l'inconveniente: lo segnali
cortesemente al mittente e distrugga subito l'originale. Ogni altro
utilizzo sarà considerato illegale. Tesi SpA non si assume alcuna
responsabilità per usi impropri di questo messaggio o di sue parti./

/This message is for the designated recipient only and may contain
privileged or confidential information. If you have received it in
error, we apologize for this: please notify the sender immediately and
delete the original. Any other use of the message is illegal. Tesi SpA
does not accept any liability for corruption, interception or damages,
if any, caused by unauthorized use of this message or parts./



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

tugwilson

On 17 Jan 2007, at 07:58, Raffaele Castagno wrote:

> How to download a file from within groovy, given an URL.
>
> http://docs.codehaus.org/display/GROOVY/Simple+file+download+from+URL


Have a look at the eachByte method on URL (http://groovy.codehaus.org/ 
groovy-jdk.html).

new URL(address).eachByte{out << it} // this will close the stream  
after use

You don't need to import java.io, Groovy does that for you.

You should really def identifiers when declaring them

Semicolons are optional in Groovy - we generally encourage people to  
omit them it makes the code less noisy.

Should you not close the stream rather than flush ?



John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

Raffaele Castagno-3
Just put together pieces found around, and I'm still a novice groovy
programmer so any correction is warmly appreciated.
I'll correct the script ASAP.

Anyway, I've posted this, and other scripts, and will continue to,
because I think that the better way to learn something is by examples.
And actually there are very few groovy examples, so a not-so-groovy
example is better than no-example at all, and it can always be modified.
 
Maybe you should encourage users to publish useful chunk of code,
something like a very informal "script of the month" contest, or "tip of
the day".

Thanks!

John Wilson ha scritto:

>
> On 17 Jan 2007, at 07:58, Raffaele Castagno wrote:
>
>> How to download a file from within groovy, given an URL.
>>
>> http://docs.codehaus.org/display/GROOVY/Simple+file+download+from+URL
>
>
> Have a look at the eachByte method on URL
> (http://groovy.codehaus.org/groovy-jdk.html).
>
> new URL(address).eachByte{out << it}    // this will close the stream
> after use
>
> You don't need to import java.io, Groovy does that for you.
>
> You should really def identifiers when declaring them
>
> Semicolons are optional in Groovy - we generally encourage people to
> omit them it makes the code less noisy.
>
> Should you not close the stream rather than flush ?
>
>
>
> John Wilson
> The Wilson Partnership
> web http://www.wilson.co.uk
> blog http://eek.ook.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

--
        */Raffaele CASTAGNO
/*/Logistica & Retail/
/[hidden email]
<mailto:[hidden email]>/

/www.gruppotesi.com/

------------------------------------------------------------------------
*TESI S.p.A.*
Sede Legale: Via Mendicita' Istruita, 24 - 12042 Bra (CN)
Sede Operativa: Via Savigliano, 48 - 12060 Loc. Roreto di Cherasco (CN)
Tel. 0172 476301 - Fax 0172 476399 - [hidden email]

/
Disclaimer
Questo messaggio è rivolto unicamente al destinatario indicato e
potrebbe contenere informazioni riservate o confidenziali. Nel caso lo
avesse ricevuto per errore, ci scusiamo per l'inconveniente: lo segnali
cortesemente al mittente e distrugga subito l'originale. Ogni altro
utilizzo sarà considerato illegale. Tesi SpA non si assume alcuna
responsabilità per usi impropri di questo messaggio o di sue parti./

/This message is for the designated recipient only and may contain
privileged or confidential information. If you have received it in
error, we apologize for this: please notify the sender immediately and
delete the original. Any other use of the message is illegal. Tesi SpA
does not accept any liability for corruption, interception or damages,
if any, caused by unauthorized use of this message or parts./



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

tugwilson

On 17 Jan 2007, at 08:28, Raffaele Castagno wrote:

> Just put together pieces found around, and I'm still a novice  
> groovy programmer so any correction is warmly appreciated.
> I'll correct the script ASAP.


I would not say you are correcting the script:) I'm sure it works  
fine (except it doesn't seem to close streams).

You are doing what all of us did when moving from Java to groovy. You  
are writing Java in  Groovy. The semicolon stuff and the redundant  
imports are a symptom of this. (and using connection.getInputStream()  
rathe than connection.inputStream).

It will take you a couple of weeks to get out of the Java habit.  
Please continue to post code and we will continue to give you  
constructive feedback.


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

Raffaele Castagno-3
That's what I mean with "a not-so-groovy example is better than
no-example at all".
Sadly, java and groovy programming are not my job, actually, it's just a
spare time activity.
Hopefully, sooner or later I'll be able to introduce groovy from the
backdoor for scripting and automation in my current activity.

About the script, it worked, but now is groovier!

Another problem about java-in-groovy programming is that most of the
examples available about "doing things" are in java.
And for an inexperienced groovist like me is not trivial to groovify them.
That's why I said that it would be better to provide a large compendium
of groovy examples for common tasks usually solved with java.

Anyway, I'll of course continue to post code here! :)

Raffaele

John Wilson ha scritto:

>
> On 17 Jan 2007, at 08:28, Raffaele Castagno wrote:
>
>> Just put together pieces found around, and I'm still a novice groovy
>> programmer so any correction is warmly appreciated.
>> I'll correct the script ASAP.
>
>
> I would not say you are correcting the script:) I'm sure it works fine
> (except it doesn't seem to close streams).
>
> You are doing what all of us did when moving from Java to groovy. You
> are writing Java in  Groovy. The semicolon stuff and the redundant
> imports are a symptom of this. (and using connection.getInputStream()
> rathe than connection.inputStream).
>
> It will take you a couple of weeks to get out of the Java habit.
> Please continue to post code and we will continue to give you
> constructive feedback.



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

tugwilson
In reply to this post by Raffaele Castagno-3

On 17 Jan 2007, at 08:28, Raffaele Castagno wrote:

> Just put together pieces found around, and I'm still a novice  
> groovy programmer so any correction is warmly appreciated.
> I'll correct the script ASAP.


You are still not closing out. The eachByte method will close the  
stream from the URL but it won't close out.

Also you are not declaring the variables using def. This means that  
you are creating the vraibales in the script's Binding. (i.e. you are  
creating global variables). You probably don't intend to do this -  
using def makes the variables local to the function.

Also, I have just remembered that java.net.* is automatically  
included by the groovy runtime so you can remove this import.


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

Marcelo Fukushima
In reply to this post by Raffaele Castagno-3
i have some scripts that i use at work... i always though of putting
them in, but they are too simple... so maybe not worth it...

On 1/17/07, Raffaele Castagno <[hidden email]> wrote:

> That's what I mean with "a not-so-groovy example is better than
> no-example at all".
> Sadly, java and groovy programming are not my job, actually, it's just a
> spare time activity.
> Hopefully, sooner or later I'll be able to introduce groovy from the
> backdoor for scripting and automation in my current activity.
>
> About the script, it worked, but now is groovier!
>
> Another problem about java-in-groovy programming is that most of the
> examples available about "doing things" are in java.
> And for an inexperienced groovist like me is not trivial to groovify them.
> That's why I said that it would be better to provide a large compendium
> of groovy examples for common tasks usually solved with java.
>
> Anyway, I'll of course continue to post code here! :)
>
> Raffaele
>
> John Wilson ha scritto:
> >
> > On 17 Jan 2007, at 08:28, Raffaele Castagno wrote:
> >
> >> Just put together pieces found around, and I'm still a novice groovy
> >> programmer so any correction is warmly appreciated.
> >> I'll correct the script ASAP.
> >
> >
> > I would not say you are correcting the script:) I'm sure it works fine
> > (except it doesn't seem to close streams).
> >
> > You are doing what all of us did when moving from Java to groovy. You
> > are writing Java in  Groovy. The semicolon stuff and the redundant
> > imports are a symptom of this. (and using connection.getInputStream()
> > rathe than connection.inputStream).
> >
> > It will take you a couple of weeks to get out of the Java habit.
> > Please continue to post code and we will continue to give you
> > constructive feedback.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
[]'s
Marcelo Takeshi Fukushima

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

Raffaele Castagno-3
My opinion is that houses are made of small bricks.
Your little scripts may save some valuable time to other users (me?),
even if it's just a script that implements word wrap, or that scan a
folder tree in search of .pdf files...

Maybe there's need of a page like "Tips and Tricks" where to put
one-line scripts (ie. easy executed from command line for direct usage)
or few-lines long scripts.

Can you add a page like that?

Raffaele

Marcelo Fukushima ha scritto:
> i have some scripts that i use at work... i always though of putting
> them in, but they are too simple... so maybe not worth it...



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

Sven Haiges
What about agreeing on a central code snippet repository. I guess also the groovy team would be interested in seeing which snippets are used most and make it eventually into GDK enhancements.

I've used snipplr.com and Guillaume pointed out there is also http://www.bytemycode.com/tags/tag/groovy/ and possibly others.

You can of course add the snippets to the wiki, but I somehow feel that a dedicated site would be a better fit.

Cheers\
Sven


On 1/18/07, Raffaele Castagno <[hidden email]> wrote:
My opinion is that houses are made of small bricks.
Your little scripts may save some valuable time to other users (me?),
even if it's just a script that implements word wrap, or that scan a
folder tree in search of .pdf files...

Maybe there's need of a page like "Tips and Tricks" where to put
one-line scripts (ie. easy executed from command line for direct usage)
or few-lines long scripts.

Can you add a page like that?

Raffaele

Marcelo Fukushima ha scritto:
> i have some scripts that i use at work... i always though of putting
> them in, but they are too simple... so maybe not worth it...



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email




--
Sven Haiges
[hidden email]

Skype: hansamann
Personal Homepage, Wiki & Blog: http://www.svenhaiges.de

Subscribe to the Grails Podcast:
http://hansamann.podspot.de/rss
Reply | Threaded
Open this post in threaded view
|

Re: [WIKI] Added: Simple file download from URL

Marcelo Fukushima
On 1/18/07, Sven Haiges <[hidden email]> wrote:

> What about agreeing on a central code snippet repository. I guess also the
> groovy team would be interested in seeing which snippets are used most and
> make it eventually into GDK enhancements.
>
> I've used snipplr.com and Guillaume pointed out there is also
> http://www.bytemycode.com/tags/tag/groovy/ and possibly
> others.
>
> You can of course add the snippets to the wiki, but I somehow feel that a
> dedicated site would be a better fit.

i totally agree (and, in fact, the reason i never put it before is
that i thought it would bloat the documentation with self-serving
scripts)

>
> Cheers\
> Sven
>
>
>
> On 1/18/07, Raffaele Castagno
> <[hidden email]> wrote:
> > My opinion is that houses are made of small bricks.
> > Your little scripts may save some valuable time to other users (me?),
> > even if it's just a script that implements word wrap, or that scan a
> > folder tree in search of .pdf files...
> >
> > Maybe there's need of a page like "Tips and Tricks" where to put
> > one-line scripts (ie. easy executed from command line for direct usage)
> > or few-lines long scripts.
> >
> > Can you add a page like that?
> >
> > Raffaele
> >
> > Marcelo Fukushima ha scritto:
> > > i have some scripts that i use at work... i always though of putting
> > > them in, but they are too simple... so maybe not worth it...
> >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
>
>
> --
> Sven Haiges
> [hidden email]
>
> Skype: hansamann
> Personal Homepage, Wiki & Blog: http://www.svenhaiges.de
>
> Subscribe to the Grails Podcast:
> http://hansamann.podspot.de/rss


--
[]'s
Marcelo Takeshi Fukushima

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email