温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用java怎么下载视频到本地

发布时间:2021-04-21 16:28:59 来源:亿速云 阅读:655 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关使用java怎么下载视频到本地,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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怎么下载视频到本地了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI