思路:1.下载的地址
2.连接到这个资源
3.创建输入流,创建输出流
4.缓冲区
5.写出数据
6.断开连接
====================================
public class UrlDown {
public static void main(String[] args) throws IOException {
//下载地址
String s=new String();
s="https://webfs.ali.kugou.com/202112141346/700a7e20667d5f9fa312d84b29c000df/part/0/960125/KGTX/CLTX001/6e5709f6ba796690b68c66e4bde4fbb5.mp3";
URL url=new URL(s);
//连接到这个资源
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
FileOutputStream fos=new FileOutputStream("2.mp3");//输出文件地址
//缓冲区
byte[] buffer=new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//写出数据
}
fos.close();。//先建后关
inputStream.close();
urlConnection.disconnect();//断开连接
}
}