Estas lineas de código nos permite descargar un archivo desde la red y guardarlo en algún medio de almacenamiento local.

JAVA:
  1. import java.io.BufferedInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6.  
  7. public class GetFileExample
  8. {
  9.     public GetFileExample()
  10.     {
  11.         try
  12.         {
  13.             byte[] bite = new byte[2048];
  14.             int read;
  15.             URL url = new URL("http://switch.dl.sourceforge.net/sourceforge/pys60miniapps/FlickrS60_src_v0.1b.tar.bz2");
  16.            
  17.             BufferedInputStream bis = new BufferedInputStream(url.openStream());
  18.             FileOutputStream fos = new FileOutputStream("/tmp/file.tar.bz2");
  19.            
  20.             while((read = bis.read(bite))>0)
  21.             {
  22.                 fos.write(bite, 0, read);
  23.                 System.out.println("--");
  24.             }
  25.            
  26.             fos.close();
  27.             bis.close();
  28.            
  29.             System.out.println("FINE");
  30.            
  31.         }
  32.         catch(MalformedURLException e)
  33.         {
  34.             e.printStackTrace();
  35.         }
  36.         catch(IOException e)
  37.         {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.    
  42.     public static void main(String[] args)
  43.     {
  44.         new GetFileExample();
  45.     }
  46. }

Popularidad: 46%