JSF实现文件下载功能

1、首先,附上页面的代码,只需要加个按钮触发事件就行了,但是必须要加多个参数ajax=“false”,不然点击下载就一直在旋转:

<p:commandLink value="下载" ajax="false" actionListener="#{projectAttach.downloadFile(id)}">

2、java代码实现的话就直接在ProjectAttach里加一个downloadFile方法即可,具体代码如下:

	public void downloadFile(String id) throws IOException{
		ProjectAttachaPo aPo = projectAttachaPoService.find(accId);
		String fileName = aPo.getFilename();
		String path =  aPo.getFilePath();
		ServletOutputStream out = null;
		InputStream is = null;
		try {
			FacesContext ctx = FacesContext.getCurrentInstance();
			ctx.responseComplete();
			String contentType = "application/x-download";
			HttpServletResponse response = (HttpServletResponse) ctx
					.getExternalContext().getResponse();
			response.setContentType(contentType);
			StringBuffer contentDisposition = new StringBuffer();
			contentDisposition.append("attachment;");
			contentDisposition.append("filename=\"");
			contentDisposition.append(fileName);
			contentDisposition.append("\"");
			response.setHeader(
					"Content-Disposition",
					new String(contentDisposition.toString().getBytes(
							System.getProperty("file.encoding")), "iso8859_1"));
			out = response.getOutputStream();
			byte[] bytes = new byte[0xffff];
			is = new FileInputStream(new File(path));
			int b = 0;
			while ((b = is.read(bytes, 0, 0xffff)) > 0) {
				out.write(bytes, 0, b);
			}
			out.flush();
			ctx.responseComplete();
		} catch (Exception e) {
			if(is!=null){
				is.close();
			}
			if(out!=null){
				out.close();
			}
			e.printStackTrace();
		}finally{
			if(is!=null){
				is.close();
			}
			if(out!=null){
				out.close();
			}
		}
	}

3、其中上面需要注意的下面的代码,需要使用下面的代码来获取response对象,不能使用this.getResponse()来获取,否则在后面使用response.getOutputStream()获取输出流的时候会报个错误:getOutputStream() has already been called for this response

FacesContext ctx = FacesContext.getCurrentInstance();
ctx.responseComplete();
String contentType = "application/x-download";
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();

4、这样就可以实现JSF下载功能了,以上内容仅供学习参考,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSF实现文件下载功能 public static void downloadFile(String path,String fileName) { try { // 获得JSF上下文环境 FacesContext context = FacesContext.getCurrentInstance(); // 获得ServletContext对象 ServletContext servletContext = (ServletContext) context .getExternalContext().getContext(); // 取得文件的绝对路径 String realName = servletContext.getRealPath(path) + "/" + fileName; HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext .getCurrentInstance().getExternalContext().getResponse(); downloadFile(httpServletResponse,realName,fileName); } catch (IOException e) { e.printStackTrace(); } FacesContext.getCurrentInstance().responseComplete(); } 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.setContentLength((int) exportFile.length()); ServletOutputStream servletOutputStream = response.getOutputStream(); byte[] b = new byte[1024]; 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页面可以这样调用: <% String filename = ""; if (request.getParameter("filename") != null) { filename = request.getParameter("filename"); } try { framewo

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值