Example usage for java.net URL URL

List of usage examples for java.net URL URL

Introduction

In this page you can find the example usage for java.net URL URL.

Prototype

public URL(String spec) throws MalformedURLException 

Source Link

Document

Creates a URL object from the String representation.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage image = null;/*w  w  w  .j  a v  a 2  s. c om*/

    URL url = new URL("URL_IMAGE");
    image = ImageIO.read(url);

    ImageIO.write(image, "jpg", new File("C:\\out.jpg"));
    ImageIO.write(image, "gif", new File("C:\\out.gif"));
    ImageIO.write(image, "png", new File("C:\\out.png"));

    System.out.println("Done");
}

From source file:Main.java

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();// w  ww  .j a  v a2 s .  c  o  m
    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();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    URL u = new URL("http://www.java2s.com");
    InputStream in = u.openStream();

    in = new BufferedInputStream(in);

    Reader r = new InputStreamReader(in);
    int c;/*from  w w  w  .java  2s  .c  o m*/
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:Main.java

public static void main(String... args) throws IOException {
    URL url = new URL("http://java2s.com");
    InputStream is = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuffer sb = new StringBuffer();

    String line;/*ww w .  j a v a2 s. co m*/

    while ((line = br.readLine()) != null)
        sb.append(line + System.lineSeparator());

    br.close();

    System.out.print(sb.toString());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.com");

    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));

    String line;//from   w w  w  . j  av a  2 s  . c om
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        writer.write(line);
        writer.newLine();
    }

    reader.close();
    writer.close();
}

From source file:Main.java

public static void main(String[] argv) {

    List<URL> urlList = new ArrayList<URL>();
    try {//  w w  w.  j  av a  2  s  .c o  m
        urlList.add(new URL("http://www.java2s.com"));
    } catch (MalformedURLException e) {
    }
    String s = urlList.get(0).getHost();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    URL url = new URL("http://www.java.com/");
    URLConnection urlConnection = url.openConnection();
    DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
    String html = "", tmp = "";
    while ((tmp = dis.readUTF()) != null) {
        html += " " + tmp;
    }/*from w  w w  .  j  av a2 s .  com*/
    dis.close();

    html = html.replaceAll("\\s+", " ");
    Pattern p = Pattern.compile("<title>(.*?)</title>");
    Matcher m = p.matcher(html);
    while (m.find() == true) {
        System.out.println(m.group(1));
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://www.java.com");
    URLConnection urlConnection = url.openConnection();
    HttpURLConnection connection = null;
    if (urlConnection instanceof HttpURLConnection) {
        connection = (HttpURLConnection) urlConnection;
    } else {/* ww w . j ava  2 s .c  o m*/
        System.out.println("Please enter an HTTP URL.");
        return;
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String urlString = "";
    String current;
    while ((current = in.readLine()) != null) {
        urlString += current;
    }
    System.out.println(urlString);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);/*from   w w w. j  av a2s.  co  m*/
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    writer.write("value=1&anotherValue=1");
    writer.flush();
    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    writer.close();
    reader.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url16 = new URL("http://www.java2s.com/style/download.png");
    URL url32 = new URL("http://www.java2s.com/style/download.png");

    final List<Image> icons = new ArrayList<Image>();
    icons.add(ImageIO.read(url16));
    icons.add(ImageIO.read(url32));

    JFrame f = new JFrame();
    f.setIconImages(icons);//from w  w  w .  j  a  v  a  2s .  c  om
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setSize(200, 100);
    f.setVisible(true);
}