首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • JSF下载文件问题 [已结贴,结贴人:java2000_net]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-01 20:58:08 楼主
    帮我分析一下,搞了我好几天了
    源代码如下:
    Java code
    package demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.logging.Logger; import javax.faces.application.Application; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.operamasks.faces.annotation.ManagedBean; import org.operamasks.faces.annotation.ManagedBeanScope; @ManagedBean(name="downFile", scope=ManagedBeanScope.SESSION) public class DownFile { public String download() { //OaMailAttach oaMailAttach = getOaMailAttach(); String path="G:\\sun\\"; String fileName="20074111452140.doc"; try { FacesContext ctx = FacesContext.getCurrentInstance(); ctx.responseComplete(); //String contentType = "application/octet-stream;charset=utf-8"; 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("\""); //log.debug(System.getProperty("file.encoding")); response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1")); // log.debug(contentDisposition.toString()); ServletOutputStream out = response.getOutputStream(); //log.debug(new Long(oaMailAttach.getAtFile().length())); byte[] bytes = new byte[0xffff]; InputStream is = new FileInputStream(new File(path + fileName)); int b = 0; while ((b = is.read(bytes, 0, 0xffff)) > 0) { out.write(bytes, 0, b); } is.close(); out.flush(); ctx.responseComplete(); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } return null; } }

    jsp如下:
    Java code
    <%@ page contentType="text/html;charset=UTF-8"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %> <%@ taglib prefix="l" uri="http://www.apusic.com/jsf/layout" %> <%@ taglib prefix="ajax" uri="http://www.apusic.com/jsf/ajax" %> <%@ taglib prefix="w" uri="http://www.apusic.com/jsf/widget" %> <f:view renderKitId="AJAX"> <w:page title="Stock Quote" skin="aqua"> <h:head> <style type="text/css"> #grid-example { height:280px; border:1px solid #cbc7b8; } #stock-detail { width:400px; } </style> <w:stylesheet src="/common/resources/examples.css"/> </h:head> <h:form> <h:commandLink id="file1" value="下载文件" action="#{downFile.downfile1}" type="button"> </h:commandLink> </h:form> </w:page> </f:view>
    50  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-01 20:59:591楼 得分:0
    上面的源代码无法实现文件下载,只能显示文件内容,不是文本文件的,只显示几个乱码,头都晕了,谢谢帮忙
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-02 06:56:342楼 得分:0
    <h:commandLink id="file1" value="下载文件" action="#{downFile.downfile1}" type="button">
    打错了,应该是downFile.download

    自己顶
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-02 10:14:373楼 得分:0
    怎么没人帮忙啊??!!!!!!!!!!!!!!!!!!!!!1
    1!!?!?!?!?!?!?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-02 11:25:514楼 得分:25
    <h:commandLink actionListener="#{downFile.download }" styleClass="highLightLink">  <h:outputText value="download"/>  <f:param name="productId" value="#{productBean.id}"/> </h:commandLink>
    你这么写一下

    Java code
    String fileName="" FileInputStream fis=new FileInputStream(fileName); BufferedInputStream bis=new BufferedInputStream(fis); ByteArrayOutputStream baos=new ByteArrayOutputStream(); BufferedOutputStream bos=new BufferedOutputStream(baos); int i; while((i=bis.read())!=-1) { bos.write(i); } bos.flush();//提交文件流,很关键 bis.close(); HttpServletResponse response=FacesUtils.getServletResponse(); response.setHeader("Content-disposition", "attachment; filename="" ); //不是内嵌显示(inline),而是作为附件下载 response.setContentLength(baos.size()); ServletOutputStream sos=response.getOutputStream(); baos.writeTo(sos); baos.close(); sos.flush(); } catch (IOException ex) { logger.debug(ex); }


    太乱了你整理改一下吧
    用流操作
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wu_07
    • 等级:
    发表于:2007-11-02 12:48:395楼 得分:25
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-disposition","attachment; filename="+name);
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-02 13:39:576楼 得分:0
    还是一样,不行
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-04 10:33:357楼 得分:0
    最近csdn人气好差啊,都没人回答??!!?!!!!!!!!!!!!!!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wu_07
    • 等级:
    发表于:2007-11-04 11:16:128楼 得分:0
    直接这样看行不行

    HTML code
    getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); getResponse().setHeader("Location",file);
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-04 11:46:509楼 得分:0
    下载怎么没人能答对啊,晕了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-11-07 12:28:5510楼 得分:0
    最近csdn人气好差,小小问题都没人能搞对
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-05 17:20:1911楼 得分:0
    这么老的帖子了,不知道楼主还看得见不.自己也碰到了同样的问题.希望搜索到这个帖子的朋友别走弯路吧.
    关键在这里 <f:view renderKitId="AJAX">
    OperaMasks在注明了使用原生AJAX之后就会导致这个问题,去掉 renderKitId="AJAX"就 OK.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-24 12:17:3812楼 得分:0
    楼主这个问题解决没有啊,我和你碰到的一样的问题,搞了一天还没有解决
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved