Java学习

java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已经搬家了,新的地址是 http://www.javaly.cn 如果有对文章有任何疑问或者有任何不懂的地方,欢迎到www.javaly.cn (Java乐园)指出,我会尽力帮助解决。一起进步

 

JSF 下载文件 怎么实现

 
 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;
}


-------------------------------------------------------------------------

<%@ 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 >
<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.download}" type="button">
</h:commandLink>
</h:form>

</w:page>
</f:view>


}

posted on 2008-08-22 10:35 找个美女做老婆 阅读(1634) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

本blog已经搬到新家了, 新家:www.javaly.cn
 http://www.javaly.cn

常用链接

留言簿(6)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜