//批量下载文件并打包
public static void downLoadZipAllFile(HttpServletRequest request, HttpServletResponse response, List<Map<String,String>> maps) {
OutputStream outp = null;
ZipOutputStream zos = null;
BufferedInputStream bis = null;
String downloadZipDir = "D:/ftpHome/downloadZip";
try {
outp = response.getOutputStream();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8"); //设置编码字符
response.setContentType("application/octet-stream"); //设置内容类型为下载类型
// response.setContentType("application/zip");// 定义输出类型
String timeSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String zipFilePath = downloadZipDir + "/" + timeSuffix + "_DataFile.zip";
String filedisplay = zipFilePath;
if (filedisplay.indexOf("/") >= 0) {
filedisplay = filedisplay.substring(filedisplay.lastIndexOf("/") + 1);
} else if (filedisplay.indexOf("\\") >= 0) {
filedisplay = filedisplay.substring(filedisplay.lastIndexOf("\\") + 1);
}
String agent = (String) request.getHeader("USER-AGENT");
// firefox
if (agent != null && agent.indexOf("MSIE") == -1) {
String enableFileName = "=?UTF-8?B?"
+ (new String(Base64.getBase64(filedisplay))) + "?=";
response.setHeader("Content-Disposition",
"attachment; filename=" + enableFileName);
} else {
filedisplay = URLEncoder.encode(filedisplay, "utf-8");
response.addHeader("Content-Disposition",
"attachment;filename=" + filedisplay);
}
try {
// File zip = new File(zipFilePath);
// if (!zip.exists()) {
// zip.createNewFile();
// }
zos = new ZipOutputStream(new FileOutputStream(zipFilePath));
for (Map<String, String> map : maps) {
String filePath = map.get("filePath");
String filename = map.get("fileName");
File file = new File(filePath);
if (file.exists()) {
zos.putNextEntry(new ZipEntry(filename));
zos.setEncoding("GBK");
bis = new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[1024 * 64];
int i = 0;
while ((i = bis.read(b)) > 0) {
zos.write(b, 0, i);
}
zos.closeEntry();
bis.close();
String isDelete = request.getParameter("isDelete");
if ("1".equals(isDelete)) {
file.delete();
}
}
// else {
// outp.write("文件不存在!".getBytes("GBK"));
// }
}
zos.close();
InputStream ins = new FileInputStream(zipFilePath);
BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面
BufferedOutputStream bouts = new BufferedOutputStream(outp);
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
ins.close();
bins.close();
outp.close();
bouts.close();
} catch (Exception e) {
e.printStackTrace();
}
// finally {
// if (bis != null) {
// bis.close();
// bis = null;
// }
//// if (zos != null) {
//// zos.close();
//// zos = null;
//// }
//// if (outp != null) {
//// outp.close();
//// outp = null;
//// }
// }
} catch (IOException e) {
e.printStackTrace();
}
}
目前的效果是能在文件夹中看到压缩好的包,但是把这个zip包通过流的方式发送到浏览器,也就是利用response.getOutputStream,为啥弹出来的不是zip包,而是其中一个文件,并且文件中乱码,求各位大神教教呀!



谷歌浏览器乱码情况如下:
