该段代码实现了压缩文件,并下载到本地。注意事项:不能在ajax中弹出下载框,否则chrome可以下载。但是IE闪退!下载需要response的输出流写入操作
前台代码:// 创建文件夹
function makeDirs(){
if(ajList.length==0){
alertMsg("请添加打印列表");
return false;
}
window.open(‘${pageContext.request.contextPath}/business/dajg_dkb.do?makeDirByGdhsMore&ids=‘+ajList+"&fwlxid="+fwlxid[0]+"&tableName="+tableName);
}
后台代码:
@RequestMapping(params = "makeDirByGdhsMore" )
@ResponseBody
public void makeDirByGdhsMore(String ids ,String fwlxid ,String tableName,HttpServletResponse response){
SuccessMsg successMsg = new SuccessMsg();
try {
successMsg = dkbdaAjService.dirByGdhsMore(ids,fwlxid,tableName);
Map <String,Object> map =new HashMap<String,Object>(2);
if(successMsg.isSuccess()){
String pathBase = ResourceUtil.getResource("dbconfig.properties").get("dirpath");
pathBase = pathBase.replace("/","\\");
HttpSession session = ContextHolderUtil.getRequest().getSession();
SessionUser sessionUser=(SessionUser)session.getAttribute("login_session_user");
String name = sessionUser.getUserName(),zipPath =pathBase+"-"+name+"-"+count+".zip";
CompressUtil.zipNoCryption(pathBase,zipPath);
count++;
for(int i=0;i<3;i++){
CompressUtil.deleteAllDirs(new File(pathBase),pathBase);
}
zipPath = zipPath.replace("\\","/").split("/")[1];
map.put("dirName",zipPath);
successMsg.setDataMap(map);
successMsg.setMsg(successMsg.getMsg()+" 压缩成功!");
getFile(zipPath,response);
}
} catch (Exception e) {
successMsg.setMsg(e.getMessage());
successMsg.setSuccess(false);
e.printStackTrace();
}
}
public void getFile(String fileName, HttpServletResponse response){
InputStream in =null;
File file= null;
try {
file=new File("D:\\"+fileName);
if(file.exists()){
System.out.println(file.getName());
}
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
in=new FileInputStream(file);
FileUtil.downLoadZipFile("D:\\"+fileName,fileName, response);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 下载文件
* @param filePath
* @param fileName
* @param response
*/
public static HttpServletResponse downLoadZipFile(String filePath,String fileName,HttpServletResponse response){
File file=new File(filePath);
if(!file.exists())
return null ;
return download(filePath,fileName,response);
}
public static HttpServletResponse download(String path, String fileName, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
try{
fileName = URLEncoder.encode(fileName, "UTF-8");
}catch (Exception e){
e.printStackTrace();
}
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.setContentType("application/octet-stream; charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}