- import java.io.IOException;
- import java.io.*;
- import java.net.URL;
- import java.net.URLConnection;
-
-
- public class Url {
- public static void getImageResourcByURL(String imagesFile)
- throws IOException {
- URL url = new URL(imagesFile);
- Object obj = url.getContent();
- System.out.println(obj.getClass().getName());
- }
-
- public static void getHtmlResourceByURL(String htmlFile) throws IOException {
- URL url = new URL(htmlFile);
- URLConnection uc = url.openConnection();
- InputStream in = uc.getInputStream();
- int c;
- while ((c = in.read()) != -1) {
- System.out.print((char) c);
- }
- System.out.println();
- in.close();
- }
-
- public static void getHTMLResource(String htmlFile) throws IOException {
- URL url = new URL(htmlFile);
- Reader reader = new InputStreamReader(new BufferedInputStream(
- url.openStream()));
- int c;
- while ((c = reader.read()) != -1) {
- System.out.print((char) c);
- }
- System.out.println();
- reader.close();
- }
-
- public static void getResourceOfHTML(String htmlFile) throws IOException {
- URL url = new URL(htmlFile);
- InputStream in = url.openStream();
- int c;
- while ((c = in.read()) != -1) {
- System.out.print((char) c);
- }
- System.out.println();
- in.close();
- }
-
- public static void supportURLType(String host, String file) {
- String[] schemes = { "http", "https", "ftp", "mailto", "telnet",
- "file", "ldap", "gopher", "jdbc", "rmi", "jndi", "jar", "doc",
- "netdoc", "nfs", "verbatim", "finger", "daytime",
- "systemresource" };
- for (int i = 0; i < schemes.length; i++) {
- try {
- URL u = new URL(schemes[i], host, file);
- System.out.println(schemes[i] + "是java所支持的URL类型\r\n");
- } catch (Exception ex) {
- System.out.println(schemes[i] + "不是java所支持的URL类型\r\n");
- }
- }
- }
-
- public static void main(String[] args) throws IOException {
-
- String imageFile = "http://www.baidu.com/001.jpg";
- String htmlFile = "http://www.baidu.com/";
- String host = "http://www.baidu.com";
- String file = "";
- System.out.println("1.//获取URL指定的图像资源信息");
- getImageResourcByURL(imageFile);
- System.out.println("2.获取URL指定的HTML网页资源信息");
- getHtmlResourceByURL(htmlFile);
- System.out.println("3.根据URL创建读对象读取网页内容");
- getHTMLResource(htmlFile);
- System.out.println("4.根据URL创建输入流读取网页内容");
- getResourceOfHTML(htmlFile);
- System.out.println("5.判断Java所支持的URL类型 ");
- supportURLType(host, file);
- }
- }
|