首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

java捉网站html

2012-10-21 
java抓网站htmlimport java.io.DataInputStreamimport java.io.IOExceptionimport java.net.MalformedUR

java抓网站html

import java.io.DataInputStream;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;//用url获取网上的资源html文件  (html源代码)public class GetHtmlByUrl {public void getHtmlbyurl(){try {URL url = new URL("http://www.iteye.com");DataInputStream di = new DataInputStream(url.openStream());byte [] bytes = new byte[1024];while (di.read(bytes, 0, 1024)!=-1) {System.out.println(new String(bytes,"utf-8"));}di.close();} catch (MalformedURLException e) {// e.printStackTrace();} catch (IOException e) {// e.printStackTrace();}}public static void gethtmlbyurlcon(){try {URL url = new URL("http://www.csdn.org");URLConnection urlcon = url.openConnection();DataInputStream di = new DataInputStream(url.openStream());byte [] bytes = new byte[1024];while (di.read(bytes, 0, 1024)!=-1) {System.out.println(new String(bytes,"utf-8"));}di.close();} catch (MalformedURLException e) {// e.printStackTrace();} catch (IOException e) {// e.printStackTrace();}}public static void main(String[] args) {GetHtmlByUrl.gethtmlbyurlcon();}}

热点排行