Java使用url下载文件
package com.davis.lesson04;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDown {
public static void main(String[] args) throws Exception{
URL url=new URL("www.baidu.com");
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
FileOutputStream fos=new FileOutputStream("file.txt");
byte[]buffer=new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}