URL 获取网络资源
2017-03-27 11:13
218 查看
URL 获取网络资源
package com.itheima.net; import java.io.BufferedInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLConnection; public class TextURL { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String imageFile="http://www.iteye.com/upload/logo/user/715302/3e63bcb1-8e77-369f-9e65-e97017c47322.jpg?1343613979"; String htmlFile="http://takeme.iteye.com/"; String host="http://www.iteye.com/"; String file="/index.html"; System.out.println("===============1.获取URL指定图像资源信息"); getImageResourceByURL(imageFile); System.out.println("===============2.获取URL指定的HTML资源信息"); getHtmlResourceByURL(htmlFile); System.out.println("===============3.根据URL创建读对象读取网页内容"); getHTMLResouce(htmlFile); System.out.println("===============4.根据URL创建输入流读取网页内容"); getResourceOfHtml(htmlFile); System.out.println("===============5.判断Java所支持的URL类型"); supportURLType(host, file); } public static void getImageResourceByURL(String imageFile) throws Exception{ URL url=new URL(imageFile); Object obj=url.getContent(); //获得此URL的内容 System.out.println(obj.getClass().getName());//显示名称 } //根据制定的URL获取资源 public static void getHtmlResourceByURL(String htmlFile) throws Exception{ URL url=new URL(htmlFile); URLConnection uc=url.openConnection(); //创建远程对象的连接对象 uc.setRequestProperty("Charset", "UTF-8"); //好像没有用 InputStream in=uc.getInputStream(); //打开连接 读取输入流 int c; while ((c=in.read())!=-1) { System.out.print((char)c); } System.out.println(); in.close(); } //读取URL指定的网页内容 public static void getHTMLResouce(String htmlFile) throws Exception{ URL url=new URL(htmlFile); Reader reader=new InputStreamReader(new BufferedInputStream(url.openStream()),"UTF-8"); int c; while ((c=reader.read())!=-1) { System.out.print((char)c); } System.out.println(); reader.close(); } //读取URL制定的网页内容 public static void getResourceOfHtml(String htmlFile) throws Exception{ 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","systemrecource"}; 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 e) { System.out.println(schemes[i]+"不是java所支持的URL类型\r\n"); } } } }
相关文章推荐
- Android-通过URL获取网络资源 Json 字符串
- android-使用URL获取网络资源
- Android通过URL获取网络资源
- android之通过URL来获取网络资源并下载资源简单实例
- HttpURL网络获取资源之:GET方法
- 通过URL来获取网络上的资源下载
- url 获取网络资源
- 使用url获取网络资源
- Android-通过URL获取网络资源
- 通过URL获取网络资源
- Android-通过URL获取网络资源
- Android 通过URL获取网络资源
- 通过URL和HTTP请求获取网络资源两者有什么区别
- 通过URL获取并下载网络资源
- Java网络编程笔记(二):获取网络资源(URL)
- Android-通过URL获取网络资源
- Android网络开发:通过URL获取网络资源
- 通过URL获取网络资源
- URL 获取网络资源
- 详解GET方法:HttpURLConnection从网络获取资源