jsf文件下载

下面是一个使用action listener 的一个例子.
添加下面的代码到backing bean的action listener中:
public void viewPdf(ActionEvent event) {
 String filename = "filename.pdf";

 // use your own method that reads file to the byte array
 byte[] pdf = getTheContentOfTheFile(filename)

 FacesContext faces = FacesContext.getCurrentInstance();
 HttpServletResponse response = (HttpServletResponsefaces.getExternalContext().getResponse();
 response.setContentType("application/pdf");
 response.setContentLength(pdf.length);
 response.setHeader"Content-disposition""inline; filename=/""+fileName+"/"");
 try {
  ServletOutputStream out;
  out = response.getOutputStream();
  out.write(pdf);
 catch (IOException e) {
  e.printStackTrace();
 }
 faces.responseComplete();
}

下面是JSP代码:

<h:commandButton immediate="true" actionListener="#{backingBean.viewPdf}" value="Read PDF" />

缺点是:当出发这个事件下载一次以后,再去点页面上的选择按钮或者submmit时候会字段再次跳出下载画面。
解决办法是不用commandLink而改用commandButton。至于这里为什么要把方法放到action listener 中不太明白
我是把他放到了action中也可以。

代码
public String downloadFile(){
//生成文件
Writer osw = null;
String file="";
try{
file=bof.utils.Util.getTempFileName()+".xls";
OutputStream os=new FileOutputStream(init.base+"tmp"+File.separatorChar+file);
BufferedOutputStream bos=new BufferedOutputStream(os,65536);
osw =new OutputStreamWriter(bos);
osw.write("数量"+"/t");
osw.write("合同号"+"/t");
osw.write("合同日期"+"/t");
osw.write("交货日期"+"/t");
osw.write("销售员"+"/t");
osw.write("物料"+"/t");
osw.write("厂商价格"+"/t");
osw.write("合同数"+"/t");
osw.write("已申请"+"/t");
osw.write("下单"+"/t");
osw.write("入库"+"/t");
osw.write("销售"+"/t");
osw.write("未清"+"/t");
osw.write("/n");

//写入list数据
for (int i=0;i<selectedContractsInfo.size();i++) {
ContractInfo contract = (ContractInfo)selectedContractsInfo.get(i);
if(contract.isMarkedForGenerate()){
osw.write(contract.getNewRequest()+"/t");
osw.write(contract.getCoNumber()+"/t");
osw.write(contract.getSignDate()+"/t");
osw.write(contract.getDeliveryDate()+"/t");
osw.write(contract.getItcode()+"/t");
osw.write(contract.getItemName()+"/t");
osw.write((contract.getFactoryPrice()==null ? "" : contract.getFactoryPrice()) +"/t");
osw.write(contract.getItemQuantity()+"/t");
osw.write(contract.getRequestCount()+"/t");
osw.write(contract.getPurchaseCount()+"/t");
osw.write(contract.getInstockCount()+"/t");
osw.write(contract.getSaleCount()+"/t");
osw.write(contract.getUnDelivered()+"/t");
osw.write("/n");
osw.flush();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
osw.close();
}catch(Exception e){
e.printStackTrace();
}
}

//下载文件
String filepath=bof.init.base+"tmp"+java.io.File.separatorChar+file;
FacesContext ctx = FacesContext.getCurrentInstance();
ResponseWriter writer = ctx.getResponseWriter();
HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=/"" + file + "/"");
try{
// java.io.InputStream fileInputStream =new java.io.BufferedInputStream(new java.io.FileInputStream(filepath));
// int i;
// while ((i=fileInputStream.read()) != -1) {
// writer.write(i);
// }
// fileInputStream.close();
// writer.close();


ServletOutputStream os = response.getOutputStream();
FileInputStream fis = new java.io.FileInputStream(filepath);
byte[] b = new byte[1024];
int i = 0;
while((i=fis.read(b))>0){
os.write(b,0,i);
}
fis.close();
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
ctx.responseComplete();
return null;
}

jsp页面

 <h:commandButton action="#{contractsInfo.downloadFile}" image="../skins/i2-standard/images/sav_as_actv.gif" />
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值