//批量下载报告
$.pess.page17008.manager.Downloadbatch = function() {
// 获取所选择的数据
var ids = $.pess.page17008.manager.getElementById(
$.pess.page17008.manager.gridId)
.jqGrid('getGridParam', 'selarrrow');
var eids = new Array();
if (ids.length == 0) {
$.toast("请选择数据!");
return;
} else {
for (var i = 0; i < ids.length; i++) {
var account = new Object();
var rowData = $.pess.page17008.manager.getElementById(
$.pess.page17008.manager.gridId).jqGrid("getRowData",
ids[i]);
var fPlanId = rowData["fPlanId"];
var fPartId = rowData['fPartId'];
//获取计划状态
var fChildPart = rowData['fPlanStatus'];
if (fChildPart != '评价报告待确认') {
$.toast("当前所选的数据中,存在计划状态不是评价报告待确认的数据,请确认!");
return;
} else {
account.fPlanId = fPlanId;
account.fPartId = fPartId;
eids.push(account);
}
}
}
$
.ajax({
type : "post",
url : 'pess/page17008/downloadbatch',
dataType : "json",
data : {
ids : JSON.stringify(eids)
},
beforeSend: function(){
$.wait();
},
success : function(resp) {
if (1 == resp["resultCode"]) {
var path = resp.object;
window.location.href = "pess/page17008/direct-download-delete?path="
+ path;
$.toast("导出成功!");
} else {
$.toast(resp.message);
}
},
complete: function () {
$.stopWait();
}
})
}
前台获取选中行的id和数据,通过ajax把数据传到后台,并返回zip包下载路径提供下载。
controller层
// 批量下载文件报告生成zip包并返回路径
@ResponseBody
@RequestMapping("/downloadbatch")
public ReturnMessage downloadbatch(String ids) {
//接受json格式的数据
List<Page17008Entity> list = JSONArray.parseArray(ids, Page17008Entity.class);
String path = mPage17008Service.downloadbatch(list);
//转换成‘/’ 返回能下载的路径
path = path.replaceAll("\\\\", "/");
return this.ok(path);
}
service层
@Override
public String downloadbatch(List<Page17008Entity> ids) {
// 获取品番名
getfchildpart(ids);
String url = PropertiesUtil.FILE_FOLDER;
java.util.Date today = new java.util.Date();
String fileMonth = DateUtil.formatDate(today, "HHmmss");
// 压缩包的名字
String zipname = "/report" + fileMonth + ".zip";
// 压缩包路径
String zippath = url + zipname;
ZipOutputStream zipOutput = null;
try {
zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zippath)));
// 第一层循环取fplanid 取对应的文件 然后进下一层循环取文件的路径
for (Page17008Entity ls : ids) {
int planid = Integer.parseInt(ls.getfPlanId());
List<Page17020Entity> list = mPage17020Mapper.getfilepath(planid);
// 取文件的路径 并且打包
for (Page17020Entity lst : list) {
// 文件名
String name = lst.getF_file_name();
// 文件路径
String urls = url + lst.getF_file_path();
File file = new File(urls);
// 当某个文件不存在时,就跳过下载下一个文件
if (file == null || !file.exists()) {
} else {
// 在zip中创建文件,可以在zip包里创建文件夹,把文件放到新文件夹里
ZipEntry zipEntry = new ZipEntry(ls.getfChildPart() + File.separator + name);
zipOutput.putNextEntry(zipEntry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while ((read = bis.read(buffer)) != -1) {
zipOutput.write(buffer, 0, read);
}
bis.close();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
zipOutput.closeEntry();
zipOutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return zippath;
}
// 获取品番名
public List<Page17008Entity> getfchildpart(List<Page17008Entity> ids) {
List<Page17008Entity> list = new ArrayList<Page17008Entity>();
for (Page17008Entity ls : ids) {
String partid = ls.getfPartId();
String fChildPart = mPage17008Mapper.getfchildpart(partid);
ls.setfChildPart(fChildPart);
}
return list;
}
}
文件打包和文件下载一样,把文件下载到zip包里,然后在下载zip包,
ZipEntry 创建zip包里文件的名字,如果想把文件分类放,可以在文件前加上新文件夹的名字,ls.getfChildPart() 文件分类放的文件夹名字,File.separator 代表“/”,name是指文件的名字带后缀
最后下载zip包
// 多文件报告打包下载后并删除服务器的临时文件
@ResponseBody
@RequestMapping("/direct-download-delete")
public Object downloadTempdelete(String path) throws Exception {
File downloadFile = new File(path);
//获取zip判断一下包里有没有文件,然后不管有没有成功下载zip包都把zip包删掉
ZipFile zipFile = new ZipFile(path);
if (zipFile.size() < 1) {
zipFile.close();
downloadFile.delete();
return this.error("下载的文件报告不存在");
} else {
zipFile.close();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment",
java.net.URLEncoder.encode(downloadFile.getName(), "UTF8"));
headers.setContentLength(downloadFile.length());
ResponseEntity result = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(downloadFile), headers,
HttpStatus.OK);
downloadFile.delete();
return result;
}
}