Home > Java > javaTutorial > body text

How to download video to local in java

WBOY
Release: 2023-05-14 23:22:04
forward
1587 people have browsed it

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");
    }
Copy after login

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);
    }
}
Copy after login

The above is the detailed content of How to download video to local in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template