日期:2014-05-20  浏览次数:20656 次

怎么用Java下载中文网页?请给出代码。小弟将十分感谢!!!
尤其是怎么解决汉字问题

------解决方案--------------------
正好有一份,有点地方自己修改下,我试过了,中文没问题
public void download(){
String strurl= "http://www.saylaw.cn/yxzy/yxjc/200612/895_2.html ";

try{
System.out.println( "正在链接到URL... "+strurl);
url=new URL(strurl);
InputStream in=url.openStream();
// String filename= "f:/desktop/wangbj/ ";
String name=url.openConnection().getURL().toString();

String realname=name.substring(name.lastIndexOf( '/ ')+1);
FileOutputStream out=new FileOutputStream( "f:/desktop/wangbj/桌面/ "+realname);

byte[] b=new byte[1024];
int offset=0;
long localSize=0;
long beginTime=System.currentTimeMillis();
while ((offset = in.read(b)) != -1) {
out.write(b,0,offset);
System.out.println(offset);
}
long endTime=System.currentTimeMillis();
int h=0;
int m=0;
int s=0;
s=(int)(endTime-beginTime)/1000;
if(s> 60){
m=s/60;
s=s%60;
}
if(m> 60){
h=m/60;
m=m%60;
}
System.out.println( "total use : "+h+ "小时 "+m+ "分钟 "+s+ "秒 ");
System.out.println(realname);
System.out.println( "Done... ");
//1178594476375 1178594458562
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
fileLength=urlcon.getContentLength();
if(urlcon.getResponseCode()> =400){
System.out.println( "服务器响应错误 ");
System.exit(-1);
}
if(fileLength <=0)
System.out.println( "无法获知文件大小 ");
// printMIME(urlcon);
System.out.println( "文件大小为 "+fileLength/1024+ "K ");
String trueurl=urlcon.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf( '/ ')+1);
fileOut=new File( "f:/desktop/wangbj/ "+filename);
}
catch(Exception err){
err.printStackTrace();
}
// init();
}