引入架包(有可能还有几个包要引入,懒得找了)
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
原理: 通过文件的url 找到文件并转化为字节流,创建一个文件包,把字节传入文件包,最后下载文件包
controller层
@ApiOperation(value = "批量下载", notes = "批量下载")
@GetMapping(value = "bulkDownload")
public void bulkDownload(HttpServletResponse response, @RequestParam(value = "ids") String[] ids) throws Exception {
//需要下载文件的id数组
recordManager.bulkDownload(response, ids);
}
service层
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.InputStream;
/**
* 批量下载
*
* @param ids 批量下载的id集合
*/
public void bulkDownload(HttpServletResponse res, String[] ids) throws Exception {
//通过ids集合获取当前文件在数据库上存的下载地址地址
List<SkyDriveRecord> records= 根据自己的查询方法写,SkyDriveRecord是我自己定义的类
int recordsSize = records.size();
SkyDriveRecord skyDriveRecord;
//提取出数据的url和文件名称
List<String> imgUrls = new ArrayList<>(recordsSize);
List<String> fileNames = new ArrayList<>(recordsSize);
for (int i = 0; i < recordsSize; i++) {
skyDriveRecord = records.get(i);
imgUrls.add(skyDriveRecord.getFilePath());
fileNames.add(skyDriveRecord.getFileName());
}
//获取当前的时间,jdk1.8才有的方法,如果不是1.8那就用自己的方法
String nowTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now());
if (!CollectionUtils.isEmpty(imgUrls)) {
//转换中文否则可能会产生乱码
String downloadFilename = URLEncoder.encode(nowTime, "UTF-8");
//设置内容类型为下载类型
res.setContentType("application/octet-stream");
//设置下载的文件名称 当前通过时间年月日来命名
res.setHeader("Content-disposition", "attachment;filename=" + downloadFilename + ".zip");
//新建文件包
ZipOutputStream zos = new ZipOutputStream(res.getOutputStream());
int imgSize = imgUrls.size();
//把上面从数据库查到的url的list转化为数组,此步骤可转可不转,用list也行
String[] files = new String[imgSize];
imgUrls.toArray(files);
int len = files.length;
for (int i = 0; i < len; i++) {
String url = files[i];
String fileName = fileNames.get(i);
//生成在压缩包中的文件名称
zos.putNextEntry(new ZipEntry(fileName));
//通过文件路径获取文件的字节流
InputStream fis = getInputStreamByGet(url);
if (!StringUtils.isEmpty(fis)) {
//通过缓存,将找到的字节流写入压缩包
byte[] buffer = new byte[1 * 1024 * 1024];
int r;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}
zos.flush();
zos.close();
}
}
// 通过get请求得到读取器响应数据的数据流
public static InputStream getInputStreamByGet(String url) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
return inputStream;
}
return null;
}
效果图: