随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

十六、从网络中获取网页数据

从网络中获取网页数据时,网页有可能使用GZIP压缩技术对页面进行压缩,这样就会减小通过网络传输的数据量,提高浏览的速度。因此在获取网络数据时要对其进行判断,对GZIP格式的数据使用GZIPInputStream对其特殊处理,否则在获取数据可能出现乱码哦.

                 

 以下为网络中获取网页数据的案例代码

 

复制代码
package com.ljq.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
* 从网络中获取网页数据
*
*
@author jiqinlin
*
*/
public class InternetTest2 {

@SuppressWarnings(
"static-access")
public static void main(String[] args) throws Exception {
String result
= "";
//URL url = new URL("http://www.sohu.com");
URL url = new URL("http://www.ku6.com/");
HttpURLConnection conn
= (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
6* 1000);//设置连接超时
if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");
InputStream is
= conn.getInputStream();//得到网络返回的输入流
if("gzip".equals(conn.getContentEncoding())){
result
= new InternetTest2().readDataForZgip(is, "GBK");
}
else {
result
= new InternetTest2().readData(is, "GBK");
}
conn.disconnect();
System.out.println(result);
System.err.println(
"ContentEncoding: " + conn.getContentEncoding());
}

//第一个参数为输入流,第二个参数为字符集编码
public static String readData(InputStream inSream, String charsetName) throws Exception{
ByteArrayOutputStream outStream
= new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = inSream.read(buffer)) != -1 ){
outStream.write(buffer,
0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String(data, charsetName);
}

//第一个参数为输入流,第二个参数为字符集编码
public static String readDataForZgip(InputStream inStream, String charsetName) throws Exception{
GZIPInputStream gzipStream
= new GZIPInputStream(inStream);
ByteArrayOutputStream outStream
= new ByteArrayOutputStream();
byte[] buffer =new byte[1024];
int len = -1;
while ((len = gzipStream.read(buffer))!=-1) {
outStream.write(buffer,
0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
gzipStream.close();
inStream.close();
return new String(data, charsetName);
}

}
复制代码
posted on   Ruthless  阅读(4032)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
< 2011年5月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示