Java中如何使用下载网页呢?
下文笔者将讲述使用Java代码下载一个网页的方法分享,如下例所示:
实现思路:
借助URL类中的相关方法,实现对网页的下载
例:
package com.java265.other;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class test {
/*
* java265.com 下载网页的示例分享
*/
public static void main(String[] args) throws Exception {
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new URL("http://java265.com/");
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


