新西兰服务器

java如何下载视频到本地


java如何下载视频到本地

发布时间:2022-06-02 18:05:48 来源:高防服务器网 阅读:53 作者:iii 栏目:大数据

这篇文章主要介绍“java如何下载视频到本地”,在日常操作中,相信很多人在java如何下载视频到本地问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java如何下载视频到本地”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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如何下载视频到本地”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注高防服务器网网站,小编会继续努力为大家带来更多实用的文章!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[