java的URL类读取网页2

本文介绍了一种使用Java进行网络编程的方法,具体展示了如何从指定的URL读取网页内容,并将其保存到本地文件中。通过使用BufferedReader、InputStreamReader和OutputStreamWriter等类,文章详细解释了按UTF-8编码格式读取和写入的过程。



package com.javabase.p1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class Test_url {

	/**
	 * 按utf-8编码格式读取
	 * 
	 * @param urlstr   网页地址
	 * @param fileName 需要保存的文件名全路径
	 * @throws IOException void
	 */
	public static void readHttp(String urlstr, String fileName) throws IOException {

		// 装饰器 缓存读取
		BufferedReader bReader = null;
		File f = new File(fileName);
		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 文件输出流
		FileOutputStream fout = new FileOutputStream(f);
		// 写入流
		OutputStreamWriter myWriter = new OutputStreamWriter(fout);
		// 输入流
		InputStreamReader myReader = null;
		StringBuffer sbuf = new StringBuffer();
		// URL对象
		URL url = new URL(urlstr);
		// 输入流
		myReader = new InputStreamReader(url.openStream(), "utf-8");
		// 把输入流放到装饰器 缓存读取
		bReader = new BufferedReader(myReader);
		String s = null;
		// 按行读取网页
		while ((s = bReader.readLine()) != null) {

			sbuf.append(s);
		}
		// 写入到文件
		myWriter.write(sbuf.toString());

		System.out.println("完成");

		bReader.close();
		myReader.close();
		myWriter.close();

	}

	
	
	/**
	 * 按utf-8编码格式读取
	 * @param http_str 网址
	 * @param path     保存的磁盘位置 全路径
	 * @throws IOException
	 */
	public static void read_http1(String http_str, String path) throws IOException {

		URL url = new URL(http_str);
		// 获取主机
		String host = url.getHost();
		// 获取端口
		int port = url.getPort();
		// 获取协议
		String pStr = url.getProtocol();

		System.out.println(host);
		System.out.println(port);
		System.out.println(pStr);

		// 打开连接 连接参数设置
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		// 获取网页内容
		InputStream in = conn.getInputStream();
		// 新建读取网页流对象
		InputStreamReader inReader = new InputStreamReader(in, "utf-8");
		// 放入缓存读取
		BufferedReader bMyReader = new BufferedReader(inReader);

		StringBuilder sbuf = new StringBuilder();
		String s = null;
		// 按行读取网页内容
		while ((s = bMyReader.readLine()) != null) {
			sbuf.append(s);

		}

		// 文件流
		FileOutputStream f_out_s = new FileOutputStream(path);
		// 写入到文件流
		OutputStreamWriter myWriter = new OutputStreamWriter(f_out_s);
		myWriter.write(sbuf.toString());
		// 关闭各种资源
		myWriter.close();
		f_out_s.close();

		bMyReader.close();
		inReader.close();
		in.close();
		conn.disconnect();

		System.out.println("读取网页完成!");

	}

	public static void main(String[] args) {
		String url = "https://www.hao123.com/index.html";
		String path1 = "C:/3331111.txt";

		String path2 = "C:/1223100.txt";
		//两种方式读取网页内容
		try {
			read_http1(url,path1);
		} catch (IOException e) {
			e.printStackTrace();
		}

		
		//两种方式读取网页内容
		try {
			readHttp(url, path2);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值