Read a GIF from a URL and save it locally in Java

Description

The following code shows how to read a GIF from a URL and save it locally.

Example


    //from   w ww . j  a  v  a 2s  . c om

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class Main {
  public static void main(String args[]) throws Exception {
    byte[] b = new byte[1];
    URL url = new URL("http://www.server.com/a.gif");
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());

    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
      fo.write(b, 0, 1);
    di.close();
    fo.close();
  }
}




















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode