天天看点

JAVA根据URL路径将网络视频保存到本地

开发项目遇到这么一个功能就是,通过http或者https的视频或者图片的浏览地址去下载视频,或者图片保存到本地

1.我们可以看到远程的视频

JAVA根据URL路径将网络视频保存到本地

这个视频是在远程的,需要用浏览器浏览

2.想要下载此视频,以下程序代码可以做到,一个测试的类

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author lrx
 * @description: TODO  视频或者图片下载工具类
 * @date 2022/10/27 17:00
 */
public class VideoDownload {
    /**
     * 保存到本地的工具类
     * @param args
     */
    public static void main(String[] args) {
        String txUrl = "URl地址";
        // 生成视频名称
        String spName = System.currentTimeMillis() + ".mp4";
        // 保存到本地的地址 /Users/tp/shipi/ 因为我是mac本所以和win系统不一样
        // 所以使用win系统的自行更换
        // String bdPath = "/Users/tp/shipi/"+spName;
        String bdPath = "保存到本地的地址"+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();
            }
        }
    }
}      

 控制台输出

JAVA根据URL路径将网络视频保存到本地

说明下载成功了,我们可以去本地文件夹查看是否可以播放

JAVA根据URL路径将网络视频保存到本地

点击播放看看

JAVA根据URL路径将网络视频保存到本地

说明没有问题了,成功了

3.同理我们可以去下载图片,只需要修改以下代码

这个是网上的图片

JAVA根据URL路径将网络视频保存到本地

咱们修改以下代码

String txUrl = "xx.png"; //修改这里
        // 生成视频名称
        String spName = System.currentTimeMillis() + ".png"; //修改这里      

 控制台输出运行的结果

JAVA根据URL路径将网络视频保存到本地

 在文件夹查看图片是否可以打开

JAVA根据URL路径将网络视频保存到本地
JAVA根据URL路径将网络视频保存到本地