java与windows(1) : 批量下载文件

说明 : maven项目打成可执行jar包,与jre放一起,并根据配置文件批量下载文件到指定目录。

1. 代码



import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.SimpleDateFormat;
import java.util.*;

public class DownloadUtil {


	/**
	 * 下载目录
	 */
	private static String LOCAL_PATH = "";

	/**
	 * 配置文件地址
	 */
	private static List<String> CONFIGS = new LinkedList<>();

	/**
	 * 远程下载地址与文件名
	 */
	private static Map<String, String> URL_AND_NAMES = new HashMap<>();

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static Integer PI_CI = 0;

	private static String SUBJECT = "";

	static {
		DownloadUtil d = new DownloadUtil();
		String path = d.getPath();
		try {
			path = URLDecoder.decode(path, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<String> list = read(path + "/" + "配置文件.txt");
		LOCAL_PATH = list.get(0).split("=")[1] + "\\";
		String[] subjects = list.get(1).split("=")[1].split(",");

		for (String subject : subjects) {
			CONFIGS.add(path + "/config/" + subject + ".txt");
		}
	}

	public static void main(String[] args) throws Exception {
		batchDownload();
		//DownloadUtil d = new DownloadUtil();
		//System.err.println(d.getPath());
	}

	public String getPath() {
		String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		if (System.getProperty("os.name").contains("dows")) {
			path = path.substring(1, path.length());
		}
		if (path.contains("jar")) {
			path = path.substring(0, path.lastIndexOf("."));
			return path.substring(0, path.lastIndexOf("/"));
		}
		return path.replace("target/classes/", "");
	}


	private static void batchDownload() {
		long t = System.currentTimeMillis();
		int i = 1;
		for (String c : CONFIGS) {
			PI_CI = i;
			SUBJECT = c.split("/config/")[1].replace(".txt", "");
			System.err.println("正在下载第" + (i++) + "个批次...当前时间:" + sdf.format(new Date()) + "...共" + CONFIGS.size() + "个批次...");
			URL_AND_NAMES.clear();
			addContent(c);
			start();
			System.err.println("-------------------------------------------------------------------------------------------------");

		}
		System.err.println("共完成" + CONFIGS.size() + "个下载批次,共耗时" + doubleBit((System.currentTimeMillis() - t) / 1000 / 60.0, 2) + "分钟。");
	}

	public static void addContent(String fileName) {
		try {
			File exists = new File(fileName);
			if (!exists.exists()) {
				throw new RuntimeException("文件不存在");
			}
			File file = new File(LOCAL_PATH + exists.getName().split("[.]")[0]);
			if (!file.exists()) {
				file.mkdirs();
			}
			InputStreamReader in = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
			BufferedReader br = new BufferedReader(in);
			String s = null;
			while ((s = br.readLine()) != null && s.length() > 0) {
				if (s.indexOf("=") != -1) {
					String[] ss = s.split("=");
					if (ss.length < 2) {
						System.err.println("配置格式错误,跳过往下执行...");
						continue;
					}
					URL_AND_NAMES.put(ss[0], file.getPath() + "\\" + ss[1]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static List<String> read(String fileName) {
		try {
			List<String> list = new LinkedList<>();
			File file = new File(fileName);
			if (!file.exists()) {
				throw new RuntimeException("文件不存在");
			}
			InputStreamReader in = new InputStreamReader(new FileInputStream(fileName), "GBK");
			BufferedReader br = new BufferedReader(in);
			String s = null;
			while ((s = br.readLine()) != null) {
				list.add(s);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	private static void download(String url, String name) {
		try {
			URL website = new URL(url.replace("\uFEFF", ""));
			ReadableByteChannel rbc = Channels.newChannel(website.openStream());
			FileOutputStream fos = new FileOutputStream(name);//例如:test.txt
			fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void start() {
		int index = 1;
		long totalTime = 0;
		for (Map.Entry<String, String> entry : URL_AND_NAMES.entrySet()) {
			System.err.print("正在第" + PI_CI + "个批次(" + SUBJECT + ")第" + (index++) + "个文件,共" + PI_CI + "个批次...当前时间:" + sdf.format(new Date()) + "...");
			long t1 = System.currentTimeMillis();
			String[] ss = entry.getKey().split("[.]");
			download(entry.getKey(), entry.getValue() + "." + ss[ss.length - 1]);
			long time = System.currentTimeMillis() - t1;
			totalTime += time;
			System.err.println("下载完成,耗时:" + doubleBit(time / 1000.0, 2) + "秒,共" + URL_AND_NAMES.size() + "个文件...");
		}
		System.err.println("第" + PI_CI + "个批次下载完成,共完成下载" + URL_AND_NAMES.size() + "个文件,耗时:" + doubleBit(totalTime / 1000 / 60.0, 2) + "分钟");
	}

	public static Double doubleBit(Double d, Integer bit) {
		if (d == null || bit == null)
			return null;
		BigDecimal bg = new BigDecimal(d).setScale(bit, RoundingMode.DOWN);
		return bg.doubleValue();
	}


}

2. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.nordrassil</groupId>
    <artifactId>nordrassil</artifactId>
    <version>1.0-SNAPSHOT</version>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>cn.nordrassil.DownloadUtil</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>
                            jar-with-dependencies
                        </descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3. 打成jar包

mvn assembly:assembly

4. 运行

4.1 文档结构及说明

jre : windows下java运行环境

download.jar : 项目打成的jar

config : 批量下载配置

  图片1.txt

https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/822.jpg=冰山1
https://www.baidu.com/img/baidu_resultlogo@2.png=百度1

  图片2.txt

https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/822.jpg=冰山2
https://www.baidu.com/img/baidu_resultlogo@2.png=百度2

start.bat

@echo off
jre\bin\java.exe -jar download.jar
pause

配置文件.txt

下载路径=F:\0_test
下载批次(多个批次以英文逗号分隔)=图片1,图片2

使用说明.txt

1.配置下载路径与下载科目
编辑"配置文件.txt",第一行为下载后文件存放地址,第二行为下载批次名(多个批次以英文逗号分隔)
2.执行程序,双击 start.bat
当出现 "请按任意键继续. . ." 时说明已下载完成。



附 : 
下载链接说明:
  1.config目录为下载配置,文件后缀必须为.txt,文件名需配置文件第二行的名称保持一致
  2.下载配置内容格式为 "链接=下载后文件名",下载后文件后缀为 .+链接的最后一个"."后面的
  3.批次名就是config下的文件名称
  4.若下载之完成后没有出现文件夹可以尝试刷新

项目及示例 : 

  链接:https://pan.baidu.com/s/1aPiqQasg_qGj08fHQEMSaw 
  提取码:ouzq 

最新jar : 

  链接:https://pan.baidu.com/s/1pXxYG4_duMIJZcuK0zZYlw 
  提取码:ejao 

更新:

   2019-07-16 当文件名为含有*时文件创建失败修复;

END。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值