1 private void downLoade(String url, String savePath){ 2 FileWriter fw = New FileWriter(savePath); 3 PrintWritrer pw = New PrintWriter(fw); 4 URL resourseUrl = new URL(url); 5 InputStream conten = resourseUrl.getConten(); 6 BufferedReader in = newBufferedReader(new InputStreamReader(content)); 7 String line; 8 while((line = in.readLine()) != null){ 9 pw.println(line); 10 } 11 pw.close(); 12 fw.close(); 13 14 }
第二种方法:
public void downLoad(String url,String savePath){
try{
InputStream in = null;
int connectTimeOut = 30*1000; //链接超时30S
int readTimeOut = 60*1000; //IO超时1min
byte[] buffer = new byte[8*1024]; //IO缓冲区:8KB
URL url = new URL(url);
URLConnection conn = url.openConnection();
conn.setConnectTime(connectTimeOut );
conn.setReadTimeOut();
conn.connect();
in = conn.getInputStream();
File file = new File(savePath);
out = newFileOutputStream(file);
for(;;){ //无限循环的写法
int bytes = in.read(buffer);
if(bytes == -1){
break;
}
out.write(buffer,0,bytes);
}
in.close();
out.close();
}catch(Exception e){
}
}
浙公网安备 33010602011771号