在Java編程中,如何讀取和下載網頁?
以下示例顯示如何使用net.URL
類的URL()
構造函數來讀取和下載網頁。
package com.yiibai; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.InputStreamReader; import java.net.URL; public class DownloadingWebpage { public static void main(String[] args) throws Exception { URL url = new URL("http://www.yiibai.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter(new FileWriter("save2yiibai-index.html")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); } }
上述代碼示例將產生以下結果(輸出易百教程的首頁頁面源代碼,並保存到save2yiibai-index.html文件中) -
<!--
輸出易百教程的首頁頁面源代碼
-->
<!DOCTYPE HTML>
<html>
<head><!--
-->
<!DOCTYPE HTML>
<html>
<head>
... ... 省略
示例-2
Java讀取和下載網頁的另一個示例:
package com.yiibai; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class DownloadingWebpage2 { public static void main(String[] args) { URL url; InputStream is = null; BufferedReader br; String line; try { url = new URL("http://www.yiibai.com/javaexamples/date_time_month.html"); 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) { } } } }
上述代碼示例將產生以下結果(輸出頁面源代碼) -
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
...... 省略