前端请求
oneDownload : function(employeeImage,name){ // 单个图片下载
window.location.href = encodeURI(App.getUrl("/api/rearService/safetyManagement/rearFaceRecognitionApplies/oneImgDownload?imgId="+ employeeImage+"&name="+name));
},
batchDownload :function(){ // 图片批量下载
// alert($("#id").val())
window.location.href = encodeURI(App.getUrl("/api/rearService/safetyManagement/rearFaceRecognitionApplies/batchDownload?id="+$("#id").val()))
},
后端响应
@Override
public void oneImgDownload(String imgId, String name, HttpServletResponse response) {
IStoreManager storeManager = SiteContext.getBean(IStoreManager.class);
try {
name = URLEncoder.encode(name+".jpg", "UTF-8");
InputStream in = storeManager.download(imgId);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+name);
byte[] buff = new byte[1024];
int len = 0;
OutputStream output = response.getOutputStream();
while ((len=in.read(buff))>0){
output.write(buff,0,len);
}
output.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void batchImgDownload(String id, HttpServletResponse response) {
// 获取子表数据
RearFaceRecognitionManageForm manageForm = recognitionManageService.getForm(id);
List applyList = manageForm.getApplyList();
try{
String downloadFilename = manageForm.getTitle()+".zip";//文件的名称
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
if(!applyList.isEmpty()) {
IStoreManager storeManager = SiteContext.getBean(IStoreManager.class);
int count = 1;
for (RearFaceRecognitionApply faceRecognitionApply : applyList) {
// 定义每个图片名称+索引防止重名覆盖
zos.putNextEntry(new ZipEntry(faceRecognitionApply.getEmployeeName()+(count++)+".jpg"));
// 根据图片id,到统一存储获取图片流
InputStream download = storeManager.download(faceRecognitionApply.getEmployeeImage());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = download.read(buffer)) != -1) {
zos.write(buffer, 0, len);
}
download.close();
}
}
zos.flush();
zos.close();
}catch (IOException e){
e.printStackTrace();
}
}



