URL 获取网络资源

来源:互联网 发布:数控车工编程 编辑:程序博客网 时间:2024/06/06 20:22

URL 获取网络资源
Java代码  收藏代码
  1. package com.itheima.net;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.InputStream;  
  5. import java.io.InputStreamReader;  
  6. import java.io.Reader;  
  7. import java.net.URL;  
  8. import java.net.URLConnection;  
  9.   
  10. public class TextURL {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      * @throws Exception  
  15.      */  
  16.     public static void main(String[] args) throws Exception {  
  17.         // TODO Auto-generated method stub  
  18.         String imageFile="http://www.iteye.com/upload/logo/user/715302/3e63bcb1-8e77-369f-9e65-e97017c47322.jpg?1343613979";  
  19.         String htmlFile="http://takeme.iteye.com/";  
  20.         String host="http://www.iteye.com/";  
  21.         String file="/index.html";  
  22.         System.out.println("===============1.获取URL指定图像资源信息");  
  23.         getImageResourceByURL(imageFile);  
  24.         System.out.println("===============2.获取URL指定的HTML资源信息");  
  25.         getHtmlResourceByURL(htmlFile);  
  26.         System.out.println("===============3.根据URL创建读对象读取网页内容");  
  27.         getHTMLResouce(htmlFile);  
  28.         System.out.println("===============4.根据URL创建输入流读取网页内容");  
  29.         getResourceOfHtml(htmlFile);  
  30.         System.out.println("===============5.判断Java所支持的URL类型");  
  31.         supportURLType(host, file);  
  32.     }  
  33.       
  34.     public static void getImageResourceByURL(String imageFile) throws Exception{  
  35.         URL url=new URL(imageFile);   
  36.         Object obj=url.getContent(); //获得此URL的内容  
  37.         System.out.println(obj.getClass().getName());//显示名称  
  38.     }  
  39.     //根据制定的URL获取资源  
  40.     public static void getHtmlResourceByURL(String htmlFile) throws Exception{  
  41.         URL url=new URL(htmlFile);  
  42.         URLConnection uc=url.openConnection();  //创建远程对象的连接对象  
  43.         uc.setRequestProperty("Charset""UTF-8");  //好像没有用    
  44.         InputStream in=uc.getInputStream(); //打开连接 读取输入流  
  45.       
  46.         int c;  
  47.         while ((c=in.read())!=-1) {  
  48.             System.out.print((char)c);  
  49.         }  
  50.         System.out.println();  
  51.         in.close();  
  52.     }  
  53.       
  54.     //读取URL指定的网页内容  
  55.     public static void getHTMLResouce(String htmlFile) throws Exception{  
  56.         URL url=new URL(htmlFile);  
  57.         Reader reader=new InputStreamReader(new BufferedInputStream(url.openStream()),"UTF-8");  
  58.         int c;  
  59.         while ((c=reader.read())!=-1) {  
  60.             System.out.print((char)c);  
  61.               
  62.         }  
  63.         System.out.println();  
  64.         reader.close();  
  65.           
  66.     }  
  67.     //读取URL制定的网页内容  
  68.     public static void getResourceOfHtml(String htmlFile) throws Exception{  
  69.         URL url=new URL(htmlFile);  
  70.           
  71.         InputStream in =url.openStream();  
  72.         int c;  
  73.         while ((c=in.read())!=-1) {  
  74.             System.out.print((char)c);  
  75.         }  
  76.         System.out.println();  
  77.         in.close();  
  78.     }  
  79.       
  80.     public static void supportURLType(String host,String file){  
  81.         String [] schemes={"http","https","ftp","mailto","telnet","file","ldap",  
  82.                 "gopher","jdbc","rmi","jndi","jar","doc","netdoc","nfs","verbatim","finger","daytime","systemrecource"};  
  83.         for (int i = 0; i < schemes.length; i++) {  
  84.             try {  
  85.                 URL u=new URL(schemes[i],host,file);  
  86.                 System.out.println(schemes[i]+"是java所支持的URL类型\r\n");  
  87.             } catch (Exception e) {  
  88.                 System.out.println(schemes[i]+"不是java所支持的URL类型\r\n");  
  89.             }  
  90.         }  
  91.           
  92.           
  93.     }  
  94.       
  95.   


原创粉丝点击