java URL下载网络资源
程序员文章站
2022-05-05 20:42:17
...
import sun.net.www.protocol.http.HttpURLConnection;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class URLDown {
public static void main(String[] args) throws Exception{
//1. 下载地址
URL url = new URL("http://localhost:8080/shisan/look.txt");
//2. 连接到这个资源
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("m1.mp4");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//写出这个数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
上一篇: C语言学习笔记(6)数组