這期內容當中小編將會給大家帶來有關使用java怎么下載視頻到本地,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Java主要應用于:1. web開發;2. Android開發;3. 客戶端開發;4. 網頁開發;5. 企業級應用開發;6. Java大數據開發;7.游戲開發等。
1.加載servlet容器
不能使用main方法直接調用
public static boolean httpDownload(String httpUrl, String saveFile) {
// 1.下載網絡文件
int byteRead;
URL url;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e1) {
e1.printStackTrace();
return false;
}
try {
//2.獲取鏈接
URLConnection conn = url.openConnection();
//3.輸入流
InputStream inStream = conn.getInputStream();
//3.寫入文件
FileOutputStream fs = new FileOutputStream(saveFile);
byte[] buffer = new byte[1024];
while ((byteRead = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteRead);
}
inStream.close();
fs.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Test
public void httpDownload() {
httpDownload("http://video.zhihuishu.com/zhs/ablecommons/demo/201806/dddee1c446314b84a26c74a8def3c3c7.mp4","E:\\test/22.mp4");
}2.添加common-io依賴
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
public class TestDownloadFile {
public static void main(String[] args) throws Exception{
String urlStr = "https://img2018.cnblogs.com/i-beta/1278703/201911/1278703-20191128121650595-812419505.png";
URL url = new URL(urlStr);
String tempFileName = "E://a.png";
File temp = new File(tempFileName);
FileUtils.copyURLToFile(url, temp);
}
}上述就是小編為大家分享的使用java怎么下載視頻到本地了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。