/**
* @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;
}
* @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;
}