Java,从网络URL中下载文件并保存到本地

Java,从网络URL中下载文件并保存到本地

1. 代码演示

  1. 创建Maven项目,导入Hutool工具包

    <dependency>
        <groupId>cn.hutoolgroupId>
        <artifactId>hutool-allartifactId>
        <version>5.4.5version>
    dependency>
    
  2. 代码

    import cn.hutool.core.date.BetweenFormater;
    import cn.hutool.core.date.DateTime;
    import cn.hutool.core.date.DateUnit;
    import cn.hutool.core.date.DateUtil;
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.io.StreamProgress;
    import cn.hutool.core.io.unit.DataSizeUtil;
    import cn.hutool.core.lang.Console;
    import cn.hutool.http.HttpUtil;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    
    
    /**
     * @className: FileDownload
     * @description: TODO
     * @author:
     * @date: 2021/11/1 19:04
     * @version: 1.0
     */
    public class FileDownload {
    
        /**
         * @return java.lang.String
         * @author
         * @description 从网络URL中下载文件
         * @date 15:33 2021/11/2
         * @params [fileUrl, diskPath]
         */
        public static Long downLoadFromUrl(String fileUrl, String diskPath) {
            String path;
            long size = 0;
            if (fileUrl != null) {
                //文件后缀
                String fileName = fileUrl.substring(fileUrl.lastIndexOf("."));
                try {
                    String uuidName = UUID.randomUUID().toString();
                    File file = new File(diskPath);
                    if (!file.exists()) {
                        //创建文件夹
                        boolean mkdir = file.mkdir();
                        if (!mkdir) {
                            throw new RuntimeException("创建文件夹失败,路径为:" + diskPath);
                        }
                    }
                    path = diskPath + File.separator + uuidName + fileName;
                    String begin = DateUtil.now();
                    DateTime beginTime = DateUtil.parse(begin);
                    //带进度显示的文件下载
                    size = HttpUtil.downloadFile(fileUrl, FileUtil.file(path), new StreamProgress() {
                        @Override
                        public void start() {
                            Console.log("开始下载,时间为:" + begin);
                        }
    
                        @Override
                        public void progress(long progressSize) {
                            Console.log("已下载:{}", FileUtil.readableFileSize(progressSize));
                        }
    
                        @Override
                        public void finish() {
                            String end = DateUtil.now();
                            DateTime endTime = DateUtil.parse(end);
                            long between = DateUtil.between(beginTime, endTime, DateUnit.MS);
                            Console.log("下载完成,用时:" + DateUtil.formatBetween(between, BetweenFormater.Level.SECOND));
                        }
                    });
                } catch (Exception e) {
                    Console.log("下载异常,异常信息为:" + e.getMessage());
                }
            }
            return size;
        }
    
        public static void main(String[] args) {
            //文件下载的URL
            String url = "......";
            //文件的存储位置
            String path = "F:\\test";
            long size = FileDownload.downLoadFromUrl(url, path);
            System.out.println("文件大小为:" + DataSizeUtil.format(size));
        }
    }
    

2. 参考资料

  • Hutool

你可能感兴趣的:(Java学习,java,开发语言,网络)