收藏 分享(赏)

JSF文件下载实现.doc

上传人:scg750829 文档编号:7131246 上传时间:2019-05-06 格式:DOC 页数:5 大小:27KB
下载 相关 举报
JSF文件下载实现.doc_第1页
第1页 / 共5页
JSF文件下载实现.doc_第2页
第2页 / 共5页
JSF文件下载实现.doc_第3页
第3页 / 共5页
JSF文件下载实现.doc_第4页
第4页 / 共5页
JSF文件下载实现.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、public static void downloadFile(String path,String fileName) try / 获得 JSF 上下文环境FacesContext context = FacesContext.getCurrentInstance();/ 获得 ServletContext 对象ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();/ 取得文件的绝对路径String realName = servletContext.getReal

2、Path(path) + “/“+ fileName;HttpServletResponse httpServletResponse =(HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();downloadFile(httpServletResponse,realName,fileName); catch (IOException e) e.printStackTrace();FacesContext.getCurrentInstance().responseComp

3、lete();public static void downloadFile(HttpServletResponse response,String realName,String fileName)throws IOException response.setHeader(“Content-disposition“,“attachment; filename=“ + fileName);response.setContentType(“application/x-download“); /File exportFile = new File(realName);/response.setCo

4、ntentLength(int) exportFile.length();ServletOutputStream servletOutputStream = response.getOutputStream();byte b = new byte1024;int i = 0;FileInputStream fis = new java.io.FileInputStream(realName);while (i = fis.read(b) 0) servletOutputStream.write(b, 0, i);使用方法1、在 backing bean 的方法中调用函数1即可。如 Abean 中download 方法调用了该方法,前台可以这样调用:或者2、jsp 页面可以这样调用:于是 jsf 页面我们可以借助 outputlink 来调用该页面

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报