当前位置:网站首页>批量下载文件
批量下载文件
2022-07-28 16:19:00 【DC_Zhang】
public static void main(String[] args) throws IOException {
DataOutputStream os = null;
ZipOutputStream zipOs = null;
List<String> fileUrls = Arrays.asList("D:\\test\\test1.txt", "D:\\test\\\\test.txt");
try {
File outFile = new File("D:\\test\\zzz.zip");
FileOutputStream fos = new FileOutputStream(outFile);
zipOs = new ZipOutputStream(new BufferedOutputStream(fos));
//设置压缩方法
zipOs.setMethod(ZipOutputStream.DEFLATED);
//遍历文件信息(主要获取文件名/文件路径等)
for (String t : fileUrls) {
File file = new File(t);
FileInputStream fis = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
fis = new FileInputStream(file);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
byte[] data = bos.toByteArray();
//文件名(包含后缀名,如:测试.pdf)
String name = t;
log.info("batchDownloadFile:[fileName:{}]", name);
//添加ZipEntry,并将ZipEntry中写入文件流
zipOs.putNextEntry(new ZipEntry(name));
//读入需要下载的文件的内容,打包到zip文件
os = new DataOutputStream(zipOs);
os.write(data);
//关闭流
zipOs.closeEntry();
}
outFile.createNewFile();
} catch (Exception e) {
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
if (zipOs != null) {
zipOs.close();
}
} catch (IOException e) {
log.error("流关闭异常{}", e);
}
}
}
边栏推荐
- The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
- 侦察机与预警机的区别
- Leetcode70 suppose you are climbing stairs. You need n steps to reach the roof. You can climb one or two steps at a time. How many different ways can you climb to the roof?
- Jsonarray traversal
- The 2021 ICPC ASIA Taipei Regional programming contest L. leadfoot (combinatorics /2-adic assignment function +kummer theorem)
- 华为Mate 40系列曝光:大曲率双曲面屏,5nm麒麟1020处理器!还将有天玑1000+的版本
- HTAP comes at a price
- Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market
- Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
- Easypoi --- excel file export
猜你喜欢

MySQL安装教程

kubernetes service 原理解析

Probability theory and mathematical statistics Chapter 1

数据库故障容错之系统时钟故障

负整数及浮点数的二进制表示

概率论与数理统计第一章

Unity editor learning (I) using features to change the display of fields in components

总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对

Egg (19): use egg redis performance optimization to cache data and improve response efficiency

Application of Pegasus d200s UAV and airborne lidar in large-scale DEM construction
随机推荐
飞马D200S无人机与机载激光雷达在大比例尺DEM建设中的应用
In 2020q2, shipments in the global tablet market soared by 26.1%: Huawei ranked third and Lenovo increased the most!
Create a self-organizing / safe / controllable Lora network! Semtech responded for the first time to the impact of the "new regulations of the Ministry of industry and information technology"
Re13: read the paper gender and racial stereotype detection in legal opinion word embeddings
火了 2 年的服务网格究竟给微服务带来了什么?(转载)
Games101-assignment05 ray tracing - rays intersect triangles
Unity shader global fog effect
Egg (19): use egg redis performance optimization to cache data and improve response efficiency
Unity shader screen post-processing
【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)
概率论与数理统计第一章
Leetcode9. Palindromes
Unity shader cartoon style rendering
Re13:读论文 Gender and Racial Stereotype Detection in Legal Opinion Word Embeddings
2022牛客多校第二场CDE
Rsync 服务部署与参数详解
Applet: scroll view slides to the bottom by default
Make full use of English
Probability theory and mathematical statistics Chapter 1
Realization of reflection and refraction effect in unity shader cube texture