新聞中心
這篇文章主要介紹“java如何下載視頻到本地”,在日常操作中,相信很多人在java如何下載視頻到本地問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java如何下載視頻到本地”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、芝罘網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為芝罘等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
1.加載servlet容器
不能使用main方法直接調(diào)用
public static boolean httpDownload(String httpUrl, String saveFile) { // 1.下載網(wǎng)絡(luò)文件 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); } }
到此,關(guān)于“java如何下載視頻到本地”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
當(dāng)前標(biāo)題:java如何下載視頻到本地
文章源于:http://www.ef60e0e.cn/article/jjcpoi.html