java如何下载视频到本地
生活随笔
收集整理的這篇文章主要介紹了
java如何下载视频到本地
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這篇文章主要介紹“java如何下載視頻到本地”,在日常操作中,相信很多人在java如何下載視頻到本地問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java如何下載視頻到本地”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
1.加載servlet容器
不能使用main方法直接調用
publicstaticbooleanhttpDownload(StringhttpUrl,StringsaveFile){
//1.下載網絡文件
intbyteRead;
URLurl;
try{
url=newURL(httpUrl);
}catch(MalformedURLExceptione1){
e1.printStackTrace();
returnfalse;
}
try{
//2.獲取鏈接
URLConnectionconn=url.openConnection();
//3.輸入流
InputStreaminStream=conn.getInputStream();
//3.寫入文件
FileOutputStreamfs=newFileOutputStream(saveFile);
byte[]buffer=newbyte[1024];
while((byteRead=inStream.read(buffer))!=-1){
fs.write(buffer,0,byteRead);
}
inStream.close();
fs.close();
returntrue;
}catch(FileNotFoundExceptione){
e.printStackTrace();
returnfalse;
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
}
@Test
publicvoidhttpDownload(){
httpDownload("http://video.zhihuishu.com/zhs/ablecommons/demo/201806/dddee1c446314b84a26c74a8def3c3c7.mp4","E:\\test/22.mp4");
}
2.添加common-io依賴
importorg.apache.commons.io.FileUtils;
importjava.io.File;
importjava.net.URL;
publicclassTestDownloadFile{
publicstaticvoidmain(String[]args)throwsException{
StringurlStr="https://img2018.cnblogs.com/i-beta/1278703/201911/1278703-20191128121650595-812419505.png";
URLurl=newURL(urlStr);
StringtempFileName="E://a.png";
Filetemp=newFile(tempFileName);
FileUtils.copyURLToFile(url,temp);
}
}
總結
以上是生活随笔為你收集整理的java如何下载视频到本地的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: multiple context con
- 下一篇: [译]MDX 介绍