Java实现批量下载选中文件功能
1.在action中定义变量
|
1
2
3
4
5
6
|
private List<String> downLoadPaths = new ArrayList<String>();//存储选中文件的下载地址 private OutputStream res; private ZipOutputStream zos; private String outPath; private String lessionIdStr;// 选中文件ID拼接的字符串 private String fileName; //浏览器下载弹出框中显示的文件名 |
分别给出get和set方法
2. 主方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/** * 下载多个文件:压缩成zip * * @return * @throws Exception */ public String downLoadLessionsZip() { downLoadPaths.clear(); String firstFileName = "";// 第一个文件的文件名 List<DownLoadFileVo> fileVos = new LinkedList<DownLoadFileVo>(); if (StringUtils.isNotEmpty(lessionIdStr)) { int end = lessionIdStr.lastIndexOf(","); if (end > 0) { if (end == lessionIdStr.length() - 1) { lessionIdStr = lessionIdStr.substring(0, end); } String[] ids = lessionIdStr.split(","); for (int i = 0; i < ids.length; i++) { if (StringUtils.isNumeric(ids[i])) { BkPersonLession lession = bkPersonLessionService.downLoadLession(Integer.parseInt(ids[i])); if (lession != null) { fileVos.add(new DownLoadFileVo(lession .getLessionName(), getContextRealPath() + lession.getLessionSavePath())); downLoadPaths.add(getContextRealPath() + lession.getLessionSavePath()); } if (i == 0) { firstFileName = lession.getLessionName(); } } } } } // 有数据可以下载 if (downLoadPaths.size() != 0) { // 进行预处理 preProcess(firstFileName); } else { // 没有文件可以下载,返回nodata return "nodata"; } // 处理 writeZip(fileVos); // 后处理关闭流 afterProcess(); return null; } // 压缩处理 public void writeZip(List<DownLoadFileVo> fileVos) { byte[] buf = new byte[8192]; int len; for (DownLoadFileVo fileVo : fileVos) { File file = new File(fileVo.getFileSavePath()); if (!file.isFile()) continue; ZipEntry ze = new ZipEntry(fileVo.getFileName() + fileVo.getFileSavePath().substring( fileVo.getFileSavePath().lastIndexOf("."))); try { zos.putNextEntry(ze); BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file)); while ((len = bis.read(buf)) > 0) { zos.write(buf, 0, len); } bis.close(); zos.closeEntry(); } catch (IOException e) { e.printStackTrace(); } } } // 预处理 public void preProcess(String firseFileName) { String zipName = "【批量下载】" + firseFileName + "等.zip"; String filename = ""; try { filename = new String(zipName.getBytes("GBK"), "8859_1"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } this.fileName = filename; HttpServletResponse response = ServletActionContext.getResponse(); try { res = response.getOutputStream(); // 清空输出流(在迅雷下载不会出现一长窜) response.reset(); // 设定输出文件头 response.setHeader("Content-Disposition", "attachment;fileName=" + filename); response.setContentType("application/zip"); zos = new ZipOutputStream(res); } catch (IOException e) { e.printStackTrace(); } } // 后处理 public void afterProcess() { try { if (zos != null) { zos.close(); } if (res != null) { res.close(); } } catch (IOException e) { e.printStackTrace(); } } |
3. 在struts.xml中配置
|
1
2
3
4
5
6
|
<action name="downLoadBkPersonLessionsZip" class="bkPersonLessionAction" method="downLoadLessionsZip">//class值为bean.xml中配置的bean <result name="nodata" type="httpheader"> <param name="status">204</param>//表示响应执行成功,但没有数据返回,浏览器不用刷新,不用导向新页面 </result> </action> |
DEMO下载地址:https://dwz.cn/Jw3z6fVq
Java实现批量下载选中文件功能的更多相关文章
- Java读取并下载网络文件
CreateTime--2017年8月21日10:11:07 Author:Marydon import java.io.ByteArrayOutputStream; import java.io ...
- Python3 根据m3u8下载视频,批量下载ts文件并且合并
Python3 根据m3u8下载视频,批量下载ts文件并且合并 m3u8是苹果公司推出一种视频播放标准,是一种文件检索格式,将视频切割成一小段一小段的ts格式的视频文件,然后存在服务器中(现在为了减少 ...
- java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码
java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码 作者:Vashon package com.ywx.batchrename; import java.io.File; import ...
- Aras Innovator客户端批量下载关联文件
<button onclick="btnDownload();" id="downfilebtn">批量下载关联文件</button> ...
- java/resteasy批量下载存储在阿里云OSS上的文件,并打包压缩
现在需要从oss上面批量下载文件并压缩打包,搜了很多相关博客,均是缺胳膊少腿,要么是和官网说法不一,要么就压缩包工具类不给出 官方API https://help.aliyun.com/documen ...
- java+web+批量下载文件
JavaWeb 文件下载功能 文件下载的实质就是文件拷贝,将文件从服务器端拷贝到浏览器端,所以文件下载需要IO技术将服务器端的文件读取到,然后写到response缓冲区中,然后再下载到个人客户端. 1 ...
- java多线程批量下载文件
多线程下载文件 平时开发中有时会用到文件下载,为了提高文件的下载速率,采用多线程下载能够达到事半功倍的效果: package test; /** * 文件下载类 * @author luweichen ...
- Java实现批量下载《神秘的程序员》漫画
上周看了西乔的博客“西乔的九卦”.<神秘的程序员们>系列漫画感觉很喜欢,很搞笑.这些漫画经常出现在CSDN“程序员”杂志末页的,以前也看过一些. 后来就想下载下来,但是一张一张的点击右键“ ...
- 【数据下载】利用wget命令批量下载ftp文件和文件夹
这是一个“”数据大发现”的时代,大家都在创造数据,使用数据以及分享数据,首先一步我们就需要从数据库download我们需要的数据. Ftp是一种常见的在线数据库,今天介绍一种可以批量下载文件夹的方法, ...
随机推荐
- cubase 的FX轨道使用方法
FX为辅助通道!
- C# 各个版本特征
历史版本 C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECM ...
- 由函数$y=\sin x$的图像伸缩变换为函数$y=\sin(\omega x)$的图像(交互式)
可以拖动滑动条\(\omega\)显示动态效果
- 全国主要城市(电信)ip地址
全国主要城市(电信)ip地址 2011-09-08 16:17:18 标签:ip地址 全国主要城市(电信) 职场 休闲 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声 ...
- github高速下载的方法
windows修改host文件: C:\Windows\System32\drivers\etc\hostslinux 修改host文件: /etc/hosts 在文件后面加上这两行 151.101. ...
- 坚果Pro2刷魔趣系统教程,刷回锤子系统教程
一.刷魔趣系统 1.高通驱动安装 https://blog.csdn.net/qq_43653944/article/details/86702169 2.刷入twrp rec https://blo ...
- java+上传大视频文件断点续传
上周遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误. 查看了系统的配置文件没有看到文件大小限制, web.xml中s ...
- Mockito 2 关于打标(stubbing)
请参考下面有关于打标的代码. //You can mock concrete classes, not just interfaces LinkedList mockedList = mock(Lin ...
- 20190716NOIP模拟赛T1 礼物(概率dp+状压)
题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种 礼物的喜悦值不能重复获得). 每次,店员会 ...
- Reborn & Recover
高考总算是结束了 而我们的旅程却还在继续 现在姑且是个ACMer(暂定) 而我已经将近一年没写代码了 接下来是全新的开始 全新的学校 全新的未来 总之从现在开始 试着努力看看吧!