最近的项目上用到的是JSF,需求是做一个页面在的下载,实现可以下载文件到客户端。并且和单独的文件下载不同的是,此下载是ZIP文件,就是先把指定目录下的文件先压缩,再进行页面下载。具体代码如下:
/**
* 程序自动打包
* @throws IOException
*/
public String getZipData(String fileName) throws IOException{
ZipOutputStream zos = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
String zipFileName = ""; //--压缩的文件名
String realInputFilePath = ""; //--压缩文件输入流路径
try{
String realNewFilePath = "d:/xxx"; //--压缩文件的路径
zipFileName = realNewFilePath + fileName + ".zip";
realInputFilePath ="e:/xxx"; //--要压缩文件的路径
File directory = new File(realInputFilePath);
File[] files = directory.listFiles(); //--得到指定目录下所有文件名
if(null != files){
int fileLen = files.length;
fos = new FileOutputStream(zipFileName); //--打包后路径 + 文件名
dos = new DataOutputStream(fos);
zos = new ZipOutputStream(dos); //--定义zip输流
for(int i=0; i<fileLen; i++){
FileInputStream fis = null;
DataInputStream dis = null;
String tempfilePath = files[i].getAbsolutePath(); //--得到每个文件绝对路径 + 文件名
fis = new FileInputStream(tempfilePath);
dis = new DataInputStream(fis);
zos.putNextEntry(new ZipEntry(tempfilePath));
zos.setEncoding("UTF-8"); //--指定编码,否则文件名乱码
int c=0;
while((c=dis.read()) != -1){
zos.write(c);
}
zos.closeEntry();
if(null != fis){
fis.close();
}
if(null != dis){
dis.close();
}
}
}
}catch(Exception e){
e.printStackTrace();
} finally{
if(null != zos){
zos.close();
}
if(null != dos){
dos.close();
}
if(null != fos){
fos.close();
}
}
return zipFileName;
}
在action中调用此方法
String zipFileName = batchDownloadService.getZipData(); //--进行文件的自动打包
batchDownloadService.downloadFile(zipFileName,fileName,pagebatchDownloadModel); //--下载文件到客户端
dao中的方法:
/**
* 下载文件到客户端
*/
public void downloadFile(String zipFileName,String fileName,BatchDownloadModel pagebatchDownloadModel){
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
try{
File zipFile = new File(zipFileName);
if(zipFile.exists()){
ServletOutputStream outstream =response.getOutputStream();
FileInputStream fis = new FileInputStream(zipFile);
String downloadZipName = fileName+"_"+"xx年_" + "xx月_" +".zip";
response.setContentType("application/zip");
response.setHeader("Transfer-Encoding", "chunked");
response.addHeader("Content-disposition", "attachment;filename="+new String(downloadZipName.getBytes("GB2312"),"ISO8859-1") );
BufferedInputStream bufInStrm = new BufferedInputStream (fis);
int readBytes = 0;
int bufferSize = 8192;
byte[] buffer = new byte[bufferSize];
while ((readBytes = bufInStrm.read(buffer)) != -1){
if (readBytes == bufferSize) {
outstream.write(buffer);
}else{
outstream.write(buffer, 0, readBytes);
}
outstream.flush();
response.flushBuffer();
}
fis.close();
outstream.close();
FacesContext.getCurrentInstance().responseComplete(); //--非常关键,否则提示文件损坏
}else{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
//response.getWriter().write("<script type='text/javascript'>alert('当前条件未查询到任何数据!')</script>");
response.getWriter().write("<script>location.href='"+request.getContextPath() + "/faces/sysMag/batchDownload/downloadFail.jsp"+"'</script>");
FacesContext.getCurrentInstance().responseComplete(); //--非常关键,否则提示文件损坏
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 删除指定文件夹目录即目录中文件
*/
public void deleteDir(File dir) {
if (dir == null || !dir.exists() || !dir.isDirectory()) //-- 检查参数
return;
for (File file : dir.listFiles()) {
if (file.isFile())
file.delete(); //--删除所有文件
else if (file.isDirectory())
deleteDir(file); //--递规的方式删除文件夹
}
dir.delete(); //--删除目录本身
}
这里要注意的是,打包用的工具类并不是JDK所自带的ZIP的jar,而是apache的一个开源jar包,在我的资源中有提供下载,下载地址:
http://download.csdn.net/detail/yixiaotian1988/4554880