• 使用 url.openConnection、IOUtils.write 从网站下载文件与本地文件对比


    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
    import org.junit.Test;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Collection;
    
    public class TestMain {
    
        /**
         * 使用 url.openConnection、IOUtils.write 从网站下载文件与本地文件对比
         *
         * @throws IOException
         */
        @Test
        public void test() throws IOException {
            String httpPrefix = "https://xxx.com.cn";
            File dist = new File("C:\Users\Nihaorz\Desktop\dist");
            File distNew = new File("C:\Users\Nihaorz\Desktop\dist-new");
            if (!distNew.exists()) {
                FileUtils.forceMkdir(distNew);
            } else {
                if (!distNew.isDirectory()) {
                    throw new RuntimeException(String.format("【%s】不是文件夹,请新建【%s】文件夹", distNew.getAbsolutePath(), distNew.getAbsolutePath()));
                }
                if (distNew.listFiles().length > 0) {
                    throw new RuntimeException(String.format("【%s】文件夹不为空,请清空文件夹后再试", distNew.getAbsolutePath()));
                }
            }
            Collection<File> files = FileUtils.listFiles(dist, null, true);
            int i = 0;
            for (File file : files) {
                String filename = file.getAbsolutePath();
                // 跳过目录和.git文件夹下的内容
                if (file.isFile() && !filename.startsWith("C:\Users\Nihaorz\Desktop\dist\.git")) {
                    URL url = new URL(httpPrefix + file.getAbsolutePath().substring(dist.getAbsolutePath().length()).replace(File.separator, "/"));
                    File outFile = new File(file.getAbsolutePath().replace(dist.getAbsolutePath(), distNew.getAbsolutePath()));
                    FileUtils.forceMkdirParent(outFile);
                    try {
                        IOUtils.write(IOUtils.toByteArray(url.openConnection().getInputStream()), new FileOutputStream(outFile));
                        System.out.println(String.format("【%s】文件下载完毕", url.toString()));
                    } catch (FileNotFoundException e) {
                        System.err.println(String.format("【%s】文件不存在", url.toString()));
                        i++;
                    } catch (IOException e) {
                        e.printStackTrace();
                        i++;
                    }
                }
            }
            System.out.println(String.format("所有文件处理完毕,其中【%d】个文件下载失败", i));
        }
    
    }
  • 相关阅读:
    weui-switch开关控件,表单提交后如何取值
    [转]判断存储过程、触发器、视图是否存在并删除
    修改服务器的3389端口
    如何在react-native 中优雅的使用 redux
    react native js 与 native 的通信与交互方式
    对 JS virtual DOM 的一知半解
    Python的实例方法,类方法,静态方法之间的区别及调用关系
    redux 管理你的 react 应用
    react- native 入门
    git 指南
  • 原文地址:https://www.cnblogs.com/nihaorz/p/15044426.html
Copyright © 2020-2023  润新知