怎么用Java下载中文网页?请给出代码。小弟将十分感谢!!! 尤其是怎么解决汉字问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 正好有一份,有点地方自己修改下,我试过了,中文没问题 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(); } 再说一下,上面的System.out.println("Done...");以后的代码可以注释掉没用到,copy给你时我忘去掉了, swing 极品五笔用 KeyEvent 无法获得输入的数字 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Prio 如何画虚线的圆? log4j配置问题?? properties文件,无法打入jar包 commons-net-1.4.1关于listFiles问题 问个java数组比较的问题 Hibernate的分页问题 初学java,一段简单代码求解 java.lang.NoClassDefFoundError 求解决 求助:构造方法私有化后如何在子类创建对象? 抛出异常出错的程序!
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();
}