【java 通过视频url下载】


    /**
     * 保存到本地的工具类
     * @param args
     */
    public static void main(String[] args) {
        String txUrl = "https://video.com/372900665-1-16.mp4?e=i";
        // 生成视频名称
        String spName = System.currentTimeMillis() + ".mp4";
    
        // win系统的自行更换
        String bdPath = "E:\\shiping/"+spName; // 保存到服务器的地址
        boolean downVideo = downVideo(txUrl, bdPath);
    }


    /**
     * 下载视频
     * @param videoUrl 视频网络地址
     * @param downloadPath  视频保存地址
     */
    public static boolean downVideo(String videoUrl, String downloadPath) {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        RandomAccessFile randomAccessFile = null;
        boolean re;
        try {

            URL url = new URL(videoUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Range", "bytes=0-");
            connection.connect();
            if (connection.getResponseCode() / 100 != 2) {
                System.out.println("连接失败...");
                return false;
            }
            inputStream = connection.getInputStream();
            int downloaded = 0;
            int fileSize = connection.getContentLength();
            randomAccessFile = new RandomAccessFile(downloadPath, "rw");
            while (downloaded < fileSize) {
                byte[] buffer = null;
                if (fileSize - downloaded >= 1000000) {
                    buffer = new byte[1000000];
                } else {
                    buffer = new byte[fileSize - downloaded];
                }
                int read = -1;
                int currentDownload = 0;
                long startTime = System.currentTimeMillis();
                while (currentDownload < buffer.length) {
                    read = inputStream.read();
                    buffer[currentDownload++] = (byte) read;
                }
                long endTime = System.currentTimeMillis();
                double speed = 0.0;
                if (endTime - startTime > 0) {
                    speed = currentDownload / 1024.0 / ((double) (endTime - startTime) / 1000);
                }
                randomAccessFile.write(buffer);
                downloaded += currentDownload;
                randomAccessFile.seek(downloaded);
                System.out.printf(downloadPath+"下载了进度:%.2f%%,下载速度:%.1fkb/s(%.1fM/s)%n", downloaded * 1.0 / fileSize * 10000 / 100,
                        speed, speed / 1000);
            }
            re = true;
            return re;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            re = false;
            return re;
        } catch (IOException e) {
            e.printStackTrace();
            re = false;
            return re;
        } finally {
            try {
                connection.disconnect();
                inputStream.close();
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以尝试使用多线程下载来提高下载速度。下面是一个简单的示例代码,使用Java的多线程来下载视频: ```java import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; public class VideoDownloader implements Runnable { private String url; private String fileName; public VideoDownloader(String url, String fileName) { this.url = url; this.fileName = fileName; } @Override public void run() { try { URL videoUrl = new URL(url); BufferedInputStream inputStream = new BufferedInputStream(videoUrl.openStream()); FileOutputStream outputStream = new FileOutputStream(fileName); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 你可以创建多个线程实例,并分别下载不同的视频片段。例如: ```java public class Main { public static void main(String[] args) { String url1 = "https://example.com/video1.mp4"; String url2 = "https://example.com/video2.mp4"; // 其他视频URL... String fileName1 = "video1.mp4"; String fileName2 = "video2.mp4"; // 其他视频文件名... Thread thread1 = new Thread(new VideoDownloader(url1, fileName1)); Thread thread2 = new Thread(new VideoDownloader(url2, fileName2)); // 其他视频的线程... thread1.start(); thread2.start(); // 其他线程的启动... // 等待所有线程下载完成 try { thread1.join(); thread2.join(); // 其他线程的等待... } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("视频下载完成!"); } } ``` 这样,每个视频将在单独的线程中下载,可以提高下载速度。请注意,这只是一个简单的示例,你可能需要根据实际需求进行适当的修改和优化。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值