java通过url获得html文件(字节流)

		String urlString="要获取的地址";
		URL url = new URL(urlString);
		URLConnection connection = url.openConnection();
		//获得字节流
		InputStream inputStream = connection.getInputStream();
		//获得字节缓冲流
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
		FileOutputStream fileOutputStream = new FileOutputStream(System.currentTimeMillis()+".html");
		byte[] bytes = new byte[255];
		int total_number;
		while((total_number = bufferedInputStream.read(bytes)) != -1) {
			//控制台输出
			System.out.println(new String(bytes,"utf-8"));
			//写入文件
			fileOutputStream.write(bytes);
		}
		fileOutputStream.close();

你可能感兴趣的:(Java爬虫)