public class URLdemo02 {
public static void main(String[] args) throws Exception {
//获取下载地址
URL url = new URL("https://adinsights-1258892624.cos.accelerate.myqcloud.com/videos/2020/1/5/x_v_ff0358b06ca143c9bbf4bc4b4bf62178.mp4");
//连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("file.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();
}
}
需要注意的地方就是需要知道资源的地址以及命名时要包括其格式
现在很多网上的资源都会隐藏具体的资源地址,所以仅供学习