java多文件以压缩文件夹的方式导出
多文件导出的时候,不是一个个导出。把多个文件放到一个压缩文件夹里面,直接导出压缩文件夹
一、前端多选文件
function DownAll() {
//hdIdValue 为多文件的路径,以逗号分开
var hdIdValue = $("#hdIdValue").val();
if(hdIdValue=="")
alert("请选择数据!");
else
location.href = "downloadFiles.do?recordpath="+window.encodeURI(hdIdValue);
}
二、downloadFiles.do后端代码
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
req.setCharacterEncoding("UTF-8");
String recordpathss=req.getParameter("recordpath");//传过来的路径们,以逗号分开
String[] recordpaths=recordpathss.split(",");
try {
String downloadFilename = URLEncoder.encode("录音文件_"+String.valueOf(System.currentTimeMillis())+".zip", "UTF-8");
ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
FileInputStream ips = null;
File file = new File("");
byte[] buffer = new byte[1024];
int r = 0;
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "attachment;filename=\""+downloadFilename+"\"");
for(int i=0;i<recordpaths.length;i++) {
String filename = recordpaths[i].split("/")[recordpaths[i].split("/").length-1];
zos.putNextEntry(new ZipEntry(filename));
file = new File (recordpaths[i]);
if(file.exists()){
ips = new FileInputStream(file);
resp.setContentType("multipart/form-data");
while ((r = ips.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
if (null != ips) {
ips.close();
}
}else{
logger.info(filename+"@@@找不到文件");
}
}
zos.flush();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}