批量压缩文件并下载

/**
* @Title: downModelImage
* @Description: 下载图片
* @param request
* @param response
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
@RequestMapping("/CheAndCle/downModelImage.do")
public String downModelImage(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ReturnObject ro = new ReturnObject();
// 取登录人信息
String userName = request.getParameter("USER_NAME");
Map mapLoginInfo = (Map) CatchMap.loginUserMap.get(userName);
Map mapLoginUser = (Map) mapLoginInfo.get("USER");
 
try {

//取值
Map map = (Map) JSONObject.toBean(JSONObject.fromObject(StringUtil
.convertSqlString(request.getParameter("JSONDATA"))),
Map.class);
//将公司ID放入检索条件中
map.put("COMPANY_ID", (String)mapLoginUser.get("COMPANY_ID"));


//设置压缩包的名字  (注意:名字不要出现不合理字符,否则浏览器无法识别下载文件类型:比如火狐下载之后 //后无法查看) 
String downloadName = StringUtil.convertMapString(map, "TIME")+"_"+
StringUtil.convertMapString(map, "CODE_NAME")+".zip"; 
 
//响应头的设置  
            response.reset();  
            response.setCharacterEncoding("utf-8");  
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename="
    + new String(downloadName.getBytes("utf-8"), "iso8859-1"));

           //设置压缩流:直接写入response,实现边压缩边下载  
           ZipOutputStream zipos = null;  
           try {  
               zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));  
               zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法   
           } catch (Exception e) {  
               e.printStackTrace();  
           } 
           
           //循环将文件写入压缩流  
           DataOutputStream os = null;  
            //获取需批量下载的文件
List<Map<String,Object>> imageList = commonService.getList(CheAndCleSql.getImageList(map));
if(imageList.size() > 0){

for(int i = 0; i< imageList.size(); i++){

Map<String,Object> imageMap = imageList.get(i);
String filename = StringUtil.convertMapString(imageMap, "IMAGE");
// 文件的存放路径
String filepath = commonService.getPropertyValue("UPLOAD_FILE_DIR")+ filename;
// 读到流中
File file = new File(filepath);
if(file.exists()){  
                      
                      try {  
                          //添加ZipEntry,并ZipEntry中写入文件流  
                          //这里,加上i是防止要下载的文件有重名的导致下载失败  
                          zipos.putNextEntry(new ZipEntry(filename));  
                          os = new DataOutputStream(zipos);  
                          InputStream is = new FileInputStream(file);  
                          byte[] b = new byte[100];  
                          int length = 0;  
                          while((length = is.read(b))!= -1){  
                              os.write(b, 0, length);  
                          }  
                          is.close();  
                          zipos.closeEntry();  
                      } catch (IOException e) {  
                          e.printStackTrace();  
                      }   
                  }

}
//关闭流  
          try {  
              os.flush();  
              os.close();  
              zipos.close();  
          } catch (IOException e) {  
              e.printStackTrace();  
         }     
}


}catch (Exception ex) {
ex.printStackTrace();
ro.setErrorMessage(ex.getMessage(), "downModelImage");
}

return null;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值