1、直接上代码,看注释
public void exportMonthData1(Date dateTo,HttpServletRequest request, HttpServletResponse response) throws BaseBizException {
List<String> fileNameList = new ArrayList<>();
Date dateFrom = new Date();
String systemPath = "xxxxx/xxxxx/";
Calendar cal = Calendar.getInstance();
//这里是要获取要压缩的所有文件
while(dateFrom.compareTo(dateTo) <= 0) {
cal.setTime(dateFrom);
Integer fromYear = cal.get(Calendar.YEAR);
Integer fromMonth = cal.get(Calendar.MONTH);
String dateFile = fromYear + String.format("%02d", fromMonth);
File patchFile = new File(systemPath + dateFile);
if(patchFile.exists()) {
File[] listFile = patchFile.listFiles();
for(File fileTemp : listFile) {
fileNameList.add(fileTemp.getPath());
}
}
//加一个月
cal.add(Calendar.MONTH, 1);
dateFrom = cal.getTime();
}
//设置响应头信息
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
//设置压缩包的名字,date为时间戳
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat("yyMMddHHmm");
String dateStr = dateformat.format(date);
String downloadName = "压缩包" + dateStr + ".zip";
//返回客户端浏览器的版本号、类型
String agent = request.getHeader("USER-AGENT");
try {
//针对IE或者以IE为内核的浏览器:
if (agent.contains("MSIE") || agent.contains("Trident")) {
downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
} else {
//非IE浏览器的处理:
downloadName = new String(downloadName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
} catch (Exception e) {
//e.printStackTrace();
logger.error("{}",e);
}
response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
//先创建一个ZipOutputStream,这里可以是FileOutputStream,还没时间测试
//
try(ZipOutputStream zipOs = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))){
for(String fileName : fileNameList) {
File file = new File(fileName);
//这里的文件名,是压缩包里定义的,显示出来也是按这个目录
ZipEntry entry = new ZipEntry(file.getName());
zipOs.putNextEntry(entry);
//增加文件内容,逐个添加
try(FileInputStream fis = new FileInputStream(fileName)){
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
zipOs.write(buf, 0, len);
}
zipOs.flush();
fis.close();
}
zipOs.closeEntry();
}
} catch (IOException e) {
//e.printStackTrace();
logger.error("exportMonthData error : {} " ,e.getMessage());
}
}
}
2、配上controller和vue前端代码
@RequestMapping(value = "/exportmonthdata")
public void exportMonthData(HttpServletRequest request, HttpServletResponse response) throws IOException {
storagechargesService.exportMonthData( request, response);
}
前端vue的代码:
monthStoragechargeExport(){
var _this = this;
_this.exportLoad = true; //开启转圈圈
_this.$axios.post("/xxxxx/xxxxx",_this.monthQuery,{
responseType: "arraybuffer" }).then((result)=>{
var downloadElement = document.createElement("a");
var blob = new Blob([result], {
type: "application/vnd.ms-excel;charset=utf-8" });
var href = window.URL.createObjectURL(blob); //创建下载的链接
downloadElement.style.display = "none";
downloadElement.href = href;
downloadElement.download = "xxxxx.zip"; //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(href); //释放掉blob对象
_this.exportLoad = false; //关闭转圈圈
});
},