分享

JAVA URL 操作使用工具类

 真爱图书 2012-11-14
  1. import java.io.IOException;   
  2. import java.io.*;   
  3. import java.net.URL;   
  4. import java.net.URLConnection;   
  5.   
  6. //操作从URL中获取网络资源的类   
  7. public class Url {   
  8.     public static void getImageResourcByURL(String imagesFile)   
  9.             throws IOException {// 获取URL指定的资源   
  10.         URL url = new URL(imagesFile);   
  11.         Object obj = url.getContent();// 获得此URL的内容   
  12.         System.out.println(obj.getClass().getName());// 显示名称   
  13.     }   
  14.   
  15.     public static void getHtmlResourceByURL(String htmlFile) throws IOException {// 获取URL指定的资源   
  16.         URL url = new URL(htmlFile);   
  17.         URLConnection uc = url.openConnection();// 创建远程对象连接对象   
  18.         InputStream in = uc.getInputStream();// 打开的连接读取的输入流   
  19.         int c;   
  20.         while ((c = in.read()) != -1) {// 循环读取资源信息   
  21.             System.out.print((char) c);   
  22.         }   
  23.         System.out.println();   
  24.         in.close();   
  25.     }   
  26.   
  27.     public static void getHTMLResource(String htmlFile) throws IOException {// 读取URL指定的网页内容   
  28.         URL url = new URL(htmlFile);// 创建URL对象   
  29.         Reader reader = new InputStreamReader(new BufferedInputStream(   
  30.                 url.openStream()));// 打开URL连接创建一个读对象   
  31.         int c;   
  32.         while ((c = reader.read()) != -1) {// 循环读取资源信息   
  33.             System.out.print((char) c);   
  34.         }   
  35.         System.out.println();   
  36.         reader.close();   
  37.     }   
  38.   
  39.     public static void getResourceOfHTML(String htmlFile) throws IOException {// 读取URL指定的网页内容   
  40.         URL url = new URL(htmlFile);   
  41.         InputStream in = url.openStream();// 打开URL连接创建输入流   
  42.         int c;   
  43.         while ((c = in.read()) != -1) {// 循环读取资源信息   
  44.             System.out.print((char) c);   
  45.         }   
  46.         System.out.println();   
  47.         in.close();   
  48.     }   
  49.   
  50.     public static void supportURLType(String host, String file) {// Java所支持的URL类型   
  51.         String[] schemes = { "http""https""ftp""mailto""telnet",   
  52.                 "file""ldap""gopher""jdbc""rmi""jndi""jar""doc",   
  53.                 "netdoc""nfs""verbatim""finger""daytime",   
  54.                 "systemresource" };// 创建URL类型数组   
  55.         for (int i = 0; i < schemes.length; i++) {// 循环遍历数组判断是否是java支持的URL类型   
  56.             try {   
  57.                 URL u = new URL(schemes[i], host, file);   
  58.                 System.out.println(schemes[i] + "是java所支持的URL类型\r\n");   
  59.             } catch (Exception ex) {   
  60.                 System.out.println(schemes[i] + "不是java所支持的URL类型\r\n");   
  61.             }   
  62.         }   
  63.     }   
  64.   
  65.     public static void main(String[] args) throws IOException {   
  66.         // java程序主入口处   
  67.         String imageFile = "http://www.baidu.com/001.jpg";   
  68.         String htmlFile = "http://www.baidu.com/";   
  69.         String host = "http://www.baidu.com";   
  70.         String file = "";   
  71.         System.out.println("1.//获取URL指定的图像资源信息");   
  72.         getImageResourcByURL(imageFile);   
  73.         System.out.println("2.获取URL指定的HTML网页资源信息");   
  74.         getHtmlResourceByURL(htmlFile);   
  75.         System.out.println("3.根据URL创建读对象读取网页内容");   
  76.         getHTMLResource(htmlFile);   
  77.         System.out.println("4.根据URL创建输入流读取网页内容");   
  78.         getResourceOfHTML(htmlFile);   
  79.         System.out.println("5.判断Java所支持的URL类型 ");   
  80.         supportURLType(host, file);   
  81.     }   
  82. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多