易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
如何使用Java读取Web页面
如何使用Java读取Web页面:
import
static
java.lang.System.out;
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.net.URL;
public
class
HtmlHelper {
private
static
String urlstring =
"http://www.csdn.net"
;
private
static
String filename =
"C:/tmp/csdn.html"
;
public
static
void
main(String[] args){
//comment out the following section to add proxy server support
//System.setProperty("http.proxyHost",conf.proxyhost);
//System.setProperty("http.proxyPort",conf.proxyport);
InputStream is =
null
;
BufferedReader br =
null
;
BufferedWriter bw =
null
;
try
{
out.println(
"Downloading html page from "
+ urlstring);
URL url =
new
URL(urlstring);
is = url.openStream();
// throws an IOException
br =
new
BufferedReader(
new
InputStreamReader(is));
bw =
new
BufferedWriter(
new
FileWriter(filename));
String line =
null
;
while
((line = br.readLine()) !=
null
) {
bw.write(line);
bw.newLine();
}
bw.flush();
out.println(
"Downloaded to "
+ filename);
}
catch
(IOException ioe) {
ioe.printStackTrace();
}
finally
{
try
{
if
(is !=
null
) {is.close();}}
catch
(IOException ignore) {}
try
{
if
(br !=
null
) {br.close();}}
catch
(IOException ignore) {}
try
{
if
(bw !=
null
) {bw.close();}}
catch
(IOException ignore) {}
}
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图