URL
是什么:统一资源定位符
定位互联网上的每个资源
https://www.baidu.com/
DNS 域名解析 域名->ip
端口有默认值
协议://IP地址:端口/项目名/资源
//获取网站资源
public class UrlDown {
public static void main(String[] args) throws IOException {
//下载地址
URL url = new URL("https://m801.music.126.net/20210805183627/ad9b8c170d795f0496a4dde2764adf8f/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/9263552142/6364/a5c4/b17e/91b4a977439bac28e59573f48e8decf5.m4a");
//连接资源
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("D:\\学习\\java\\NetStudy\\test.m4a");
byte[] bytes = new byte[1024];
int len;
while ((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}