下载网页出现乱码

meteorlg meteorlg 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我用UrlConnection下载网页,有时候中文会出现乱码(出现机率不是很高,但是下载的多了肯定会出现,比如下载会变成“?载?”),请问这是什么问题?该如何解决?谢谢
我下载网页的代码如下:
public StringBuffer download(String strUrl,String encoding) {
StringBuffer strContent=new StringBuffer();
URL myurl;
try {
myurl = new URL(strUrl);
URLConnection ucon =myurl.openConnection();
ucon.setConnectTimeout(httpConnectTimeOut);
ucon.setReadTimeout(httpReadTimeOut);
ucon.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)");
InputStream in=ucon.getInputStream();
byte buffer[]=new byte[1024];
while(true)
{
int n=in.read(buffer);
if(n<0)
break;
strContent.append(new String(buffer,0,n,encoding));
}
in.close();
} catch (MalformedURLException e) {
//e.printStackTrace();
Out.print(Out.ERROR_INFO,"False Url!");
} catch (IOException e) {
//e.printStackTrace();
//Out.print(Out.ERROR_INFO,"Http error!"+strUrl);
}
return strContent;
}

我传入的encoding参数根据网站不同,有utf-8,也有GB2312
...全文
评论 3
收藏 已收藏
分享
返回顶部
meteorlg
Web 开发 81,108 成员 341,721 内容