说明 : maven项目打成可执行jar包,与jre放一起,并根据配置文件批量下载文件到指定目录。
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 CONFIGS = new LinkedList<>();
/**
* 远程下载地址与文件名
*/
private static Map 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 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 read(String fileName) {
try {
List 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 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();
}
}
4.0.0
cn.nordrassil
nordrassil
1.0-SNAPSHOT
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
UTF-8
org.apache.maven.plugins
maven-assembly-plugin
cn.nordrassil.DownloadUtil
jar-with-dependencies
mvn assembly:assembly
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/[email protected]=百度1
图片2.txt
https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/822.jpg=冰山2
https://www.baidu.com/img/[email protected]=百度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。