下载文件夹
下载文件夹,我们可以先将文件夹打包,然后进行下载,可以保留包,不删除,用时间戳方式判断是否删除后重建。
文件下载代码:
response.setHeader("Content-Type", "application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + zipFile.getName());
try {
OutputStream to = response.getOutputStream();
Files.copy(zipFile, to);
to.flush();
response.flushBuffer();
} catch (IOException e) {
e.getStackTrace();
}
打包:
/*
* Copyright 2010.
*
* This document may not be reproduced, distributed or used
* in any manner whatsoever without the expressed written
* permission of Boventech Corp.
*
* $Rev: Rev $
* $Author: Author $
* $LastChangedDate: LastChangedDate $
*
*/
package com.boventech.zyk.util;
import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
/**
* 提供对文件进行压缩的操作
*/
public class Ziper {
public static File doZip(File srcDir) {
if (!srcDir.exists()) {
throw new RuntimeException(srcDir + " isn't exist!");
}
File zipFile = new File(srcDir.getPath() + ".zip");
/*
* 使用Ant的zip工具类,这里进行实例化
*/
Project project = new Project();
Zip zip = new Zip();
zip.setProject(project);
zip.setDestFile(zipFile);
/*
* 获取目标文件夹的文件集合
*/
FileSet fileSet = new FileSet();
fileSet.setProject(project);
fileSet.setDir(srcDir);
/*
* 执行压缩操作
*/
zip.addFileset(fileSet);
zip.execute();
return zipFile;
}
public static String unZip(File srcFile, String destDir) {
if (!srcFile.exists()) {
throw new RuntimeException(srcFile + " isn't exist!");
}
Expand expand = new Expand();
expand.setProject(new Project());
expand.setSrc(srcFile);
if (destDir == null) {
destDir = srcFile.getPath().substring(0, srcFile.getPath().lastIndexOf("."));
}
expand.setDest(new File(destDir));
expand.execute();
return destDir;
}
}
还没有评论,来说两句吧...