JSF 文件下载
来源:互联网 发布:淘宝转运仓单号填错 编辑:程序博客网 时间:2024/05/17 07:44
最近的项目上用到的是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);ZipEntry ze = new ZipEntry(files[i].getName()); //--解压文件后直接得到文件,没有目录结构zos.putNextEntry(ze);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()){
String downloadZipName = fileName+"_"+ "xx年_" + "xx月"+".zip";
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.addHeader("Content-disposition", "attachment;filename="+new String(downloadZipName.getBytes("GB2312"),"ISO8859-1") );
response.setHeader("Content-Transfer-Encoding","binary");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
ServletOutputStream outstream =response.getOutputStream();
FileInputStream fis = new FileInputStream(zipFile);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();
}if(null != fis){
fis.close();
}if(null != bufInStrm){
bufInStrm.close();
}if(null != outstream){
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
- jsf文件下载
- jsf 实现文件下载
- JSF 文件下载
- JSF 文件下载
- jsf 文件下载
- JSF文件上传与下载
- 用JSF实现文件下载
- 【JSF小记】JSF实现简单的文件下载
- 实例-用JSF实现文件下载
- JSF点滴积累--通用文件下载函数
- 实例-用JSF实现文件下载
- JSF文件下载的不同实现
- jsf浏览器文件下载的实现及问题!
- 在基于WebSphere JSF 1.2 Portlet Bridge的JSF Portlet中实现文件下载
- JSF下载地址
- JSF学习一 下载
- JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结
- jsf 文件上传
- 在 FlashDevelop 的移动项目 (Adobe AIR) 中使用 ane
- android的TabActivity
- flex内阴影效果
- Cross Platform library in C++ for Windows, WM, WinCE, MacOS, iPhone
- 相约世纪末
- JSF 文件下载
- 20120907 重回计算机路 地平线……
- 面试训练二维数组
- 动态链接库DLL编程(一)之简介
- Python中的正则表达式(1)
- upd通信代码-linux版本
- smartd监控磁盘
- javascript自动排版
- 推荐一系列优秀的Android开发源码