日期:2014-05-18  浏览次数:20820 次

jsf里怎么实现download功能?
就是把数据库中的blob字段download到本地上。
以前struts都是用request,response来实现的。
jsf里基本上不怎么用request,response。
我想问下jsf里有没有直接的download功能?
难道还要费力气去从最基本的servlet写起?
希望高手帮忙!谢谢!

------解决方案--------------------
先取出写成文件,然后传参数给下面的Jsp文件

<%@ page contentType= "text/html; charset=gb2312 " %>
<%@page import= "java.util.* "%>
<%@page import= "java.io.* "%>
<%@page import= "java.net.* "%>
<%
String filename = " ";
if (request.getParameter( "filename ") != null) {
filename = request.getParameter( "filename ");
}
String name = filename.substring(filename.lastIndexOf( "/ ") + 1);
//response.setContentType( "application/msword ");
response.setContentType( "application/x-msdownload ");
response.setHeader( "Content-disposition ", "attachment; filename= "+name);

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath( " " + filename)));
bos = new BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];
int bytesRead;

while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}

} catch(final IOException e) {
System.out.println ( "出现IOException. " + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
%>


------解决方案--------------------
使用jsf的API直接取
(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()