luni, 28 martie 2016

Java: URL.getContent() Sample

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class ContentGetter {
 void printContentType(String url) {
  // Open the URL for reading
  try {
   URL u = new URL(url);
   Object o = u.getContent();
   System.out.println("I got a " + o.getClass().getName());
  } catch (MalformedURLException ex) {
   System.err.println(url + " is not a parseable URL");
  } catch (IOException ex) {
   System.err.println(ex);
  }
 }
}


import org.junit.Test;
import junit.framework.TestCase;
public class Test_ContentGetter extends TestCase{

 @Test
 public void test_printContentType() {
  ContentGetter contentG = new ContentGetter(); 
  contentG.printContentType("https://lh3.googleusercontent.com/"
    + "--Oiu2RWjYnY/VqtIQuFdYCI/AAAAAAAAkCU/"
    + "QuuYvZsHqjgWVSMIBqTYMgvlTMAiZ2GAQCCo/"
    + "s1280-Ic42/20160122_193915.jpg");
  contentG.printContentType("http://georgelache.blogspot.ro"
    + "/2015/06/java-making-remote-service.html");
 }
}
Output:
I got a sun.awt.image.URLImageSource
I got a sun.net.www.protocol.http.HttpURLConnection$HttpInputStream


Niciun comentariu:

Trimiteți un comentariu