vue前端代码
downloadFile(id, name) {
downloadPdf(sdiId).then(response => {
// 创建一个新的Blob对象
const blob = new Blob([response], { type: "application/octet-stream" });
// 创建一个URL.createObjectURL指向这个Blob对象
const downloadUrl = window.URL.createObjectURL(blob);
// 创建一个隐藏的a标签用于触发下载
const aLink = document.createElement('a');
aLink.style.display = 'none';
aLink.href = downloadUrl;
aLink.download = sdName; // 文件名可以从响应头或者其他途径获取
// 将a标签添加到DOM中并模拟点击,触发下载
document.body.appendChild(aLink);
aLink.click();
// 下载完成后移除a标签并释放内存
setTimeout(() => {
document.body.removeChild(aLink);
window.URL.revokeObjectURL(downloadUrl);
}, 0);
}).catch(error => {
console.error('下载文件失败:', error);
});
},
js方法调用后台
/**
* 下载文档,responseType一定要设置,否则下载到的文件为空白
* @param data
*/
export function batchDownload(data) {
return request({
url: '自己后端的地址' ,
method: 'post',
data: data,
timeout: 100000,
responseType: 'blob'
})
}
java后台代码逻辑
@Override
public void batchDowmload(Long[] id, HttpServletResponse response, HttpServletRequest request) {
List<ZipDto> list = new ArrayList<>();
for (int i = 0; i < sdiIds.length; i++) {
//获取集合
try {
//从minio获取文件,这是自己的方法。根据自己实际情况来
Response fileResponse = remoteFileService.downloadMaterialFile(info.getSdName(), info.getSdiUrl());
list.add(new ZipDto(info.getSdName(), fileResponse.body().asInputStream()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
);
String fileName = "batch" + ".zip";
listStreamToZipStream(list, fileName, response);
}
/**
* 将多个流转成zip文件输出
* @param listStream 文件流实体类对象
* @param fileName zip包的名称
* @param response
* @return
*/
public static boolean listStreamToZipStream(List<ZipDto> listStream, String fileName, HttpServletResponse response) {
boolean flag = false;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
OutputStream out = null;
try {
out = response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
response.setHeader("Access-Control-Allow-Origin","*");
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
zos = new ZipOutputStream(out);
byte[] bufs = new byte[1024 * 10];
for (ZipDto zipDto : listStream) {
String streamfilename = zipDto.getName();
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(streamfilename);
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
bis = new BufferedInputStream(zipDto.getInputstream(), 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
}
flag = true;
zos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
// 关闭流
try {
if (null != bis){
bis.close();
}
if (null != zos){
zos.close();
}
if (null != out){
out.close();
}
} catch (IOException e) {
// e.printStackTrace();
log.error(e.getMessage());
}
}
return flag;
}