Descargado Ficheros desde una URL

La clase URL permite la descarga de ficheros remotos.

groovy:000> u = new URL('http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html')
===> http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html
groovy:000> u.text[0..100] 
===> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
O también:
groovy:000> u = 'http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html'.toURL()                    
===> http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html
groovy:000> result = []
===> []
groovy:000> u.eachLine { if (it =~ /href/) result << it }  
===> null
groovy:000> result.size()
===> 2
groovy:000> result[0]    
===>  <B>Err:</B> <a href="perl_errata_form.html" target="_blank">Si hallas una errata ...</a>
Otra forma:
groovy:000> u = 'http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html'.toURL()
===> http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html
groovy:000> u.eachLine { if (it =~ /(?i)href/)println it }                                       
<LINK REL="STYLESHEET" HREF="mystyle.css">
<LINK REL="next" HREF="node27.html">
<LINK REL="previous" HREF="node25.html">
<LINK REL="up" HREF="node22.html">
<LINK REL="next" HREF="node27.html">
..............................................
  HREF="node22.html">Tipos de Datos Simples</A>
  HREF="node25.html">Especificación de Tipos Opcional</A>
 <B>Err:</B> <a href="perl_errata_form.html" target="_blank">Si hallas una errata ...</a>
===> null
groovy:000>

El siguiente programa usa la clase para descargar los contenidos de una URL:

generaciondecodigos@nereida:~/src/groovy/files$ cat -n wget.groovy
     1  def download(address)
     2  {
     3      def file = new FileOutputStream(address.tokenize("/")[-1])
     4      def out = new BufferedOutputStream(file)
     5      out << new URL(address).openStream()
     6      out.close()
     7  }
     8
     9  if (args.length) {
    10    download(args[0])
    11  }

Ejecución:

~/src/groovy/files$ groovy wget.groovy 'http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node17.html'
~/src/groovy/files$ ls -l node17.html
-rw-r--r-- 1 generaciondecodigos generaciondecodigos 9110 2010-02-10 14:06 node17.html

El método tokenize

La función tokenize descompone la cadena según la cadena delimitadora en una lista de cadenas:

groovy:000> "This,,should,have,five,items".tokenize(',')
===> [This, should, have, five, items]
groovy:000> "This,,should,have,five,items".tokenize(',').class
===> class java.util.ArrayList
groovy:000> "This, ,should,have,six,items".tokenize(',')
===> [This,  , should, have, six, items]
groovy:000> "This, ,should,have,six,items".tokenize(',').size()
===> 6
Su comportamiento es similar a split salvo por el tratamiento de las cadenas vacías, que son ignoradas por tokenize:
groovy:000> "This,,should,have,five,items".split(',')   
===> [Ljava.lang.String;@268c858a
groovy:000> "This,,should,have,five,items".split(',').size()
===> 6
groovy:000> println "This,,should,have,five,items".split(',')
[This, , should, have, five, items]
===> null

El Método openStream

El método Java openStream de la clase URL tiene la siguiente declaración:

public final InputStream openStream() throws IOException

Opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for:
             openConnection().getInputStream()
Returns: an input stream for reading from the URL connection. Throws: IOException - if an I/O exception occurs.



Subsecciones
Casiano Rodríguez León
2010-04-30