java实现文件打包下载
public void getMrtFileAllDown(Map paramMap, HttpServletRequest request, HttpServletResponse response) {
try{
int QYLX = StringUtils.getIntegerByObj(paramMap.get("QYLX"));
String uuidArrayStr = StringUtils.getStringByObj(paramMap.get("uuidArrayStr"));
String[] uuidArray = uuidArrayStr.split(",");
String path = request.getSession().getServletContext().getRealPath("/");
String save_xml_dir = "/uploadFiles/" + "license/mrt/" + QYLX + "/";
String suffix = "";
String zipSavePath = save_xml_dir + TimeUtils.getYYYY_MM_DD(new Date()) + suffix+".zip";
File tempXmlFile = new File(path+zipSavePath);
File fileParent = tempXmlFile.getParentFile();
if (!fileParent.exists()) {
fileParent.mkdirs();
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path+zipSavePath));
String xklx = "";
for (int i = 0; i <uuidArray.length; i++) {
String UUID = uuidArray[i];
Map searchMap = new HashMap();
searchMap.put("UUID", UUID);
searchMap.put("QYLX", QYLX);
Map mrtItem = this.saveCreateMrtFile(request,searchMap);
String mrtUrl = StringUtils.getStringByObj(((Map)mrtItem.get("data")).get("mrtUrl"));
if (!xklx.isEmpty()) {
xklx += ",";
}
xklx += StringUtils.getStringByObj(((Map) mrtItem.get("data")).get("XKLX"));
if (QYLX == 95) {
suffix = "【再注册】";
} else if (QYLX == 38) {
if (("," + xklx + ",").indexOf(",1,") > -1 && ("," + xklx + ",").indexOf(",51,") > -1) {
suffix = "【包装备案】【补充申请】";
} else if (",1,".indexOf("," + xklx + ",") > -1) {
suffix = "【包装备案】";
} else if (",51,".indexOf("," + xklx + ",") > -1) {
suffix = "【补充申请】";
}
}
FileInputStream fis = new FileInputStream(path + mrtUrl);
String fileName = mrtUrl.substring(mrtUrl.lastIndexOf("/")+1);
out.putNextEntry(new ZipEntry(fileName));
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
out.flush();
if (StringUtils.isNotEmptyObject(PropertiesUtil.FILE_SAVE_PATH)) {
LicenseUtil.copyFile(path + zipSavePath, PropertiesUtil.FILE_SAVE_PATH + zipSavePath);
}
File zipFile = new File(path + zipSavePath);
InputStream fis = null;
OutputStream toClient = null;
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String((TimeUtils.getYYYY_MM_DD(new Date()) + suffix+".zip").getBytes("gbk"), "iso-8859-1"));
response.addHeader("Content-Length", "" + zipFile.length());
response.setContentType("application/x-msdownload");
try {
fis = new BufferedInputStream(new FileInputStream(path + zipSavePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
toClient.close();
fis.close();
zipFile.delete();
}
}catch (Exception e){
e.printStackTrace();
}
}
public Map saveCreateMrtFile(HttpServletRequest request,Map paramMap) {
Map<String,Object> dataMap = new HashMap<>();
String UUID = StringUtils.getStringByObj(paramMap.get("UUID"));
int QYLX = StringUtils.getIntegerByObj(paramMap.get("QYLX"));
try {
Map searchMap=new HashMap();
searchMap.put("LICENSECOMPANYBASEUUID",UUID);
searchMap.put("QYLX",QYLX);
dataMap=mobileShareRestful.findByBaseInfoUtil(ResultCodeWraper.SUCCESS,searchMap);
int XKLX = StringUtils.getIntegerByObj(dataMap.get("XKLX"));
String path = request.getSession().getServletContext().getRealPath("");
String mrtModel = "";
if(QYLX == 117){
mrtModel = "/upload/license/mrtMuBan/YCXJKYPBA.xml";
}else if(QYLX == 95){
mrtModel = "/upload/license/mrtMuBan/GCYPZZCSP.xml";
}else if(QYLX == 38){
mrtModel = "/upload/license/mrtMuBan/GCYPZCBCSQ.xml";
}
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new File(path + mrtModel));
Element root = doc.getRootElement();
String mrtFileName = "";
if (QYLX == 117) {
mrtFileName = "_YCXJKYPBA";
Xml_Mrt.analysisYcxjkypbaXml(root, dataMap);
}else if(QYLX == 95){
mrtFileName = StringUtils.getStringByObj(dataMap.get("QYMC"))+"_"+StringUtils.getStringByObj(dataMap.get("FZJG"))+"_"+StringUtils.getStringByObj(dataMap.get("YP_GG"))+"_再注册";
Xml_Mrt.analysisGcypzzcspXml(root, dataMap,licenseCompanyBaseDao,this);
} else if (QYLX == 38) {
String suffix = "_包装备案";
if (XKLX == 51) {
suffix = "_补充申请";
}
mrtFileName = StringUtils.getStringByObj(dataMap.get("QYMC")) + "_" + StringUtils.getStringByObj(dataMap.get("FZJG")) + "_" + StringUtils.getStringByObj(dataMap.get("YP_GG")) + suffix;
Xml_Mrt.analysisGcypzcbcsqXml(root, dataMap, this);
}
XMLOutputter xmloutputter = new XMLOutputter();
xmloutputter.setFormat(Format.getCompactFormat().setEncoding("GBK").setIndent(" "));
String save_xml_dir = "/uploadFiles/" + "license/mrt/" + QYLX + "/";
String save_xml_path = save_xml_dir + mrtFileName + ".xml";
String save_mrt_path = save_xml_dir + mrtFileName + ".mrt";
File xmlFile = new File(path + save_xml_path);
File xmlfileParent = xmlFile.getParentFile();
if (!xmlfileParent.exists()) {
xmlfileParent.mkdirs();
}
OutputStream outputStream = new FileOutputStream(path + save_xml_path);
xmloutputter.output(doc, outputStream);
System.err.println("xml文档生成成功!");
if (StringUtils.isNotEmptyObject(PropertiesUtil.FILE_SAVE_PATH)) {
LicenseUtil.copyFile(path + save_xml_path, PropertiesUtil.FILE_SAVE_PATH + save_xml_path);
}
if (xmlFile.exists()) {
FileInputStream fis = new FileInputStream(xmlFile);
String destFileName = xmlFile.getName().replaceAll("\\.xml$", "\\.mrt");
FileOutputStream fos = new FileOutputStream(new File(xmlFile.getParent().toString().replace(path+save_xml_path, path+save_xml_dir), destFileName));
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
System.err.println("mrt文件生成成功!");
if (StringUtils.isNotEmptyObject(PropertiesUtil.FILE_SAVE_PATH)) {
LicenseUtil.copyFile(path + save_mrt_path, PropertiesUtil.FILE_SAVE_PATH + save_mrt_path);
}
licenseCompanyBaseDao.execSQL(" update license_company_base set LSZDCOMPANYBASEUUID = '1' where uuid = '" + UUID + "' ");
Map recordMap = new HashMap<String,Object>();
recordMap.put("UUID",UUID);
recordMap.put("huanJie", "mrt文件导出");
recordMap.put("yiJian", mrtFileName + "mrt文件导出");
recordMap.put("jieLun", "导出成功");
recordMap.put("shenHeRen", StringUtils.getStringByObj(((SysUser) request.getSession().getAttribute("user")).getId()));
this.saveShenHeJiLu(recordMap);
} else {
System.out.println("文件不存在!");
}
dataMap.put("mrtUrl",save_mrt_path);
dataMap.put("XKLX",XKLX);
return LicenseUtil.reMapSucces(dataMap);
} catch (Exception e) {
e.printStackTrace();
return LicenseUtil.reMapError();
}
}