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)); } }