JAVA通过url获取网页内容.docx
- 文档编号:22790666
- 上传时间:2023-04-28
- 格式:DOCX
- 页数:3
- 大小:14.48KB
JAVA通过url获取网页内容.docx
《JAVA通过url获取网页内容.docx》由会员分享,可在线阅读,更多相关《JAVA通过url获取网页内容.docx(3页珍藏版)》请在冰豆网上搜索。
JAVA通过url获取网页内容
importjava.io.*;
import.URL;
import.URLConnection;
publicclassTestURL{
publicstaticvoidmain(String[]args)throwsIOException{
test4();
test3();
test2();
test();
}
/**
*获取URL指定的资源。
*
*@throwsIOException
*/
publicstaticvoidtest4()throwsIOException{
URLurl=newURL("
//获得此URL的内容。
Objectobj=url.getContent();
System.out.println(obj.getClass().getName());
}
/**
*获取URL指定的资源
*
*@throwsIOException
*/
publicstaticvoidtest3()throwsIOException{
URLurl=newURL("
//返回一个URLConnection对象,它表示到URL所引用的远程对象的连接。
URLConnectionuc=url.openConnection();
//打开的连接读取的输入流。
InputStreamin=uc.getInputStream();
intc;
while((c=in.read())!
=-1)
System.out.print(c);
in.close();
}
/**
*读取URL指定的网页内容
*
*@throwsIOException
*/
publicstaticvoidtest2()throwsIOException{
URLurl=newURL("
//打开到此URL的连接并返回一个用于从该连接读入的InputStream。
Readerreader=newInputStreamReader(newBufferedInputStream(url.openStream()));
intc;
while((c=reader.read())!
=-1){
System.out.print((char)c);
}
reader.close();
}
/**
*获取URL的输入流,并输出
*
*@throwsIOException
*/
publicstaticvoidtest()throwsIOException{
URLurl=newURL("
//打开到此URL的连接并返回一个用于从该连接读入的InputStream。
InputStreamin=url.openStream();
intc;
while((c=in.read())!
=-1)
System.out.print(c);
in.close();
}
}
魔域私服t6NqB0FRx5YZ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 通过 url 获取 网页 内容