public static Boolean compressExcelToZip(HttpServletRequest request,HttpServletResponse response,Map<String, Object> params) {
/*清空输出流*/
response.reset();
response.setCharacterEncoding("utf-8");
//response.setContentType("multipart/form-data");
/*导出的文件名称*/
String fileName=(String) params.get("fileName");
/*执行导出的模块(gt:个体,zy:自营,ly:联营,hz:汇总)*/
String modelName=(String) params.get("modelName");
/*需要导出数据的纳税人识别号*/
String nsrsbh=(String) params.get("nsrsbh");
/*统计周期起始年月,格式:yyyy-mm*/
String tjzqq=(String) params.get("tjzqq");
/*统计周期截止年月,格式:yyyy-mm*/
String tjzqz=(String) params.get("tjzqz");
try {
/*设定输出文件头*/
//response.setHeader("Content-Disposition", "attachment;fileName="+URLEncoder.encode(fileName+".zip", "UTF-8"));
response.setHeader("Content-Disposition", "attachment;fileName="+new String(fileName.getBytes("gb2312"), "ISO8859-1")+".zip");
response.setContentType("application/zip;charset=utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return false;
}
ServletOutputStream out =null;
try {
out = response.getOutputStream();
String basePath =request.getSession().getServletContext().getRealPath("/")+"..\\";
/*excel存储位置基本路径*/
String excelBasePath=basePath+"ExcelFile\\"+nsrsbh+"\\"+modelName+"\\";
// System.out.println(basePath);
// System.out.println(excelBasePath);
// File zipFile =getZipFile(basePath+"downloadtemp\\"+UUID.randomUUID()+"\\"+"fileName"+".zip");
/*压缩文件存储路径*/
String zipPath=basePath+"ExcelFile\\ExcelFileZipTemp\\";
/*为了保证目录存在,如果没有则新建该目录 */
File temp = new File(zipPath);
if (!temp.exists()) {
temp.mkdirs();
}
/*根据统计周期起止时间生成介于两值间所有月份集合*/
List<String> tjzq=DateUtil.getMonthBetween(tjzqq,tjzqz);
/*生成压缩文件*/
makeZipFileByParams(excelBasePath,tjzq,zipPath+"\\"+fileName+".zip");
File zipFile=new File(zipPath+"\\"+fileName+".zip");
/*
* 将Zip文件输出给调用者
*/
FileInputStream fileInputStream = new FileInputStream(zipFile);
byte [] outByte = new byte[1024];
int len;
while((len = fileInputStream.read(outByte))!=-1){
out.write(outByte,0,len);
}
out.flush();
fileInputStream.close();
FileUtil.deleteFiles(zipFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* @Title: makeZipFileByParams
* @Description: TODO(根据查询条件制作压缩文件)
* @param basePath 基础路径
* @param tjzq 统计周期
* @param outDir 导出文件位置
* @return String
* @throws Exception
*/
public static String makeZipFileByParams(String basePath,List<String> tjzq,String outDir){
List<String> srcDirList=new ArrayList<String>();
/*获取需要导出的文件集合*/
for(int i = 0 ; i < tjzq.size() ; i++) {
srcDirList.add(basePath+tjzq.get(i).replaceAll("-", ""));
}
try {
String[] srcDirArray=new String[srcDirList.size()];
srcDirList.toArray(srcDirArray);
ZipUtil.toZip(srcDirArray, outDir, true);
} catch (RuntimeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
List<String> tjzq=new ArrayList<String>();
tjzq.add("2018-09");
tjzq.add("2018-10");
makeZipFileByParams("D://1234//xx/",tjzq,"D://1234//xx.zip");
}
生成压缩文件并下载
最新推荐文章于 2022-05-30 22:45:48 发布