码迷,mamicode.com
首页 > 编程语言 > 详细

Java如何读取和下载网页?

时间:2018-09-10 11:00:13      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:pre   buffer   catch   页面   trace   nec   nload   ade   import   

在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();
    }
}
Java

上述代码示例将产生以下结果(输出易百教程的首页页面源代码,并保存到save2yiibai-index.html文件中) -

<!--
输出易百教程的首页页面源代码
-->
<!DOCTYPE HTML>
<html>
<head><!--

-->
<!DOCTYPE HTML>
<html>
<head>
... ... 省略
Shell

示例-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) {
            }
        }
    }
}
Java

上述代码示例将产生以下结果(输出页面源代码) -

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
...... 省略

Java如何读取和下载网页?

标签:pre   buffer   catch   页面   trace   nec   nload   ade   import   

原文地址:https://www.cnblogs.com/borter/p/9617173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!