描述:指定一个目录/文件,如果是文件直接下载,如果是目录则可以把该目录以及子目录和目录下的文件全部压缩打包下载
/**
* 下载文件:单个文件和zip压缩包
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public void downloadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
OutputStream out = null;
FileInputStream fr = null;
try {
String path = request.getParameter("path");
String fileName = null;
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
fileName = path.substring(path.lastIndexOf("\\")+1, path.length());
}else{
fileName = path.substring(path.lastIndexOf("/")+1, path.length());
}
File file = new File(path);
long fileSize = file.length();
//文件直接下载,目录需要打包zip再下载
if(file.isDirectory()){
this.downloadZipFile(response, path, fileName);
}else{
response.setCharacterEncoding("UTF-8");
response.setContentLength((int) fileSize);
response.setContentType("application/x-download");
response.setHeader("Pragma","public");
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes("GBK"),"ISO8859-1") + "\"");
out = response.getOutputStream();
fr = new FileInputStream(file);
int bufSize = 1024 * 1024;
byte[] buffer = new byte[bufSize];
int len;
while ((len = fr.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
if(fr != null){
fr.close();
}
}
}
public void downloadZipFile(HttpServletResponse response, String path,String zipName){
zipName = zipName+".zip";
File zipFile = new File(zipName);
FileOutputStream fos = null;
ZipOutputStream zos = null;
//写入Zip
try {
File files = new File(path);
fos = new FileOutputStream(zipName);
zos = new ZipOutputStream(fos);
zos.setEncoding("gbk");
writeZip(files, "", zos);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(zos != null){
try {
zos.close();
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//下载Zip
OutputStream out = null;
try {
out = response.getOutputStream();
response.reset();
response.setHeader("Pragma","public");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(zipName.getBytes("GBK"), "ISO-8859-1"));
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
out.write(FileUtils.readFileToByteArray(zipFile));
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(out != null){
try {
out.close();
//下载完成之后,把打包的zip文件删掉
//commons-io-1.4.jar
FileUtils.deleteQuietly(zipFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//打包
public static void writeZip(File file,String parentPath, ZipOutputStream zos){
if(file.exists()){
if(file.isDirectory()){
parentPath+=file.getName()+File.separator;
File[] files = file.listFiles();
if(files.length != 0){
for (File f : files) {
writeZip(f,parentPath,zos);
}
}else{
try {
zos.putNextEntry(new ZipEntry(parentPath));
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ZipEntry ze = new ZipEntry(parentPath+file.getName());
zos.putNextEntry(ze);
byte[] content = new byte[1024];
int len;
while((len = fis.read(content)) != -1){
zos.write(content);
zos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}