**1.请求下载URL定义**
@RequestMapping("/downExcel")
public void downExcel(HttpServletRequest request, HttpServletResponse response, String type, String campId) {
//下载文件配置
//ResponseUtil.configDownloadExcel(response, outputFileName);
ResponseUtil.configDownloadZip(response, campaignInfos.get(0).getCampName()+".zip");
//将文件打zip包
UploadAndDownUtil.generateZip(request, String.valueOf(campaignInfos.get(0).getCampId()), response);
}
**2.下载文件头配置**
/**
* 设置response头 为下载zip, 设置文件名. 应该在response写出之前被调用
*/
public static void configDownloadZip(HttpServletResponse response, String fileName) {
Preconditions.checkState(!response.isCommitted(), "config download excel should be called before response is committed");
response.setHeader("Content-Type", "application/zip");
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
}
**3.生成zip文件流并下载**
/**
* 生成压缩包
* @param request
* @return
*/
public static void generateZip(HttpServletRequest request,
HttpServletResponse response){
//获取excel存储路径
String excelDir = getServerPath(request, Constants.ExcelPath);
// checkDir(excel);
//遍历excel存储路径
//获取pathName的File对象
File dirFile = new File(excelDir);
//获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
ZipOutputStream zos = null;
try {
//关联response输出流,直接将zip包文件内容写入到response输出流并下载
zos = new ZipOutputStream(response.getOutputStream());
//循环读取文件路径集合,获取每一个文件的路径
for(String fp : fileList){
File f = new File(excelDir, fp); //根据文件路径创建文件
zipFile(f, zos); //将每一个文件写入zip文件包内,即进行打包
//刷新缓冲区
response.flushBuffer();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(zos != null){
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 封装压缩文件的方法
* @param inputFile
* @param zipoutputStream
*/
public static void zipFile(File inputFile,ZipOutputStream zipoutputStream) {
try {
if(inputFile.exists()) { //判断文件是否存在
if (inputFile.isFile()) { //判断是否属于文件,还是文件夹
//创建输入流读取文件
FileInputStream fis = new FileInputStream(inputFile);
BufferedInputStream bis = new BufferedInputStream(fis);
//将文件写入zip内,即将文件进行打包
ZipEntry ze = new ZipEntry(inputFile.getName()); //获取文件名
zipoutputStream.putNextEntry(ze);
//写入文件的方法,同上
byte [] b=new byte[1024];
long l=0;
while(l<inputFile.length()){
int j=bis.read(b,0,1024);
l+=j;
zipoutputStream.write(b,0,j);
}
//关闭输入输出流
fis.close();
bis.close();
} else { //如果是文件夹,则使用穷举的方法获取文件,写入zip
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], zipoutputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
---------------------
Java多文件批量生成zip下载
最新推荐文章于 2024-07-10 10:25:26 发布