URL编程
public class URLTest1 {
public static void main(String
[] args
){
HttpURLConnection openConnection
= null
;
InputStream is
= null
;
BufferedOutputStream bos
= null
;
try {
URL url
= new URL("https://www.bilibili.com/video/BV1Kb411W75N?p=629");
openConnection
= (HttpURLConnection
) url
.openConnection();
openConnection
.connect();
is
= openConnection
.getInputStream();
bos
= new BufferedOutputStream(new FileOutputStream("c.mp4"));
byte[] buffer
= new byte[1024];
int len
;
while ((len
= is
.read(buffer
)) != -1){
bos
.write(buffer
,0,len
);
}
System
.out
.println("下载完成");
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (is
!= null
){
try {
is
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (bos
!= null
){
try {
bos
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (openConnection
!= null
){
try {
openConnection
.disconnect();
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-15996.html