JSF2.0 下载

今天郁闷了一把,特此立传! :cry:
先说下需求:下载服务器文件
实现方式:一是下载服务器文件。二是直接在服务器构造文件流输出。
我在这里的具体需求是查询后的结果通过excel下载。我在这里需要把查询后的结果构造成为一个excel文件,同时把它塞进输出流。昨天在测试翻页的时候发现,翻页后,第二页的多选异步调用失效,通过查页面源码发现,翻页操作后,页面丢失了对js文件的引用。通过对commandbutton的操作加上ajax标签,使之刷新范围在form里面,解决了这个问题。
可是,今天突然发现download不好用了。尝试了一天,终于看出了一点点门道(自己能力不是很好,呵呵)。原来,我的download按钮也被包裹在ajax标签内,所以,每次都会提示empty response(chrome下面)。而在glassfish控制台里面也会有
Caused by: java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
这个异常报出!
火狐报的异常是XML数据没有准备好!
一起都是ajax标签包住commandbutton惹出的祸!
忘记把代码写上,代码来自网络,自己整理后,也应该贡献给网络:)

客户端调用这个函数:
public static <T> void download(String fileName,Collection<T> dataset){
ByteArrayOutputStream baos =downLoadWithExcel("test", "download", dataset, "yyyy-mm-dd");
//download.properties是你定义的表头和list元素的field对应关系,我做测试的是:User = user
Name = Name,user和name是两个属性,大写首字符是为了调反射方法的时候不用做首字母大写操作了
downloadAction(fileName,baos);
}
下面对两个函数说明下:

首先,获取excel流:
@title 用来说明生产的文件名
@fileName 由于项目特殊需求,类是由xsd生成的,通过反射拿不到field,这里通过定义properties文件,说明了各个field和要生成的表头之间对应的关系。而且这个field可以通过在类里面的getField方法获得这个字段的值,所以,我们定义了这个以文件。
@collection<T> 你传递进来的集合,想对这个集合生成excel文件,T类型的field在fileName定义的properties文件里都有说明了
@pattern 日期格式,一般就用“yyyy-MM-dd”就行
public static <T> ByteArrayOutputStream downLoadWithExcel(String title, String fileName, Collection<T> dataset, String pattern) {{

ByteArrayOutputStream baos = null;
BufferedOutputStream bos = null;
baos = new ByteArrayOutputStream();
bos = new BufferedOutputStream(baos);

// 声明一个工作薄

HSSFWorkbook workbook = new HSSFWorkbook();

// 生成一个表格

HSSFSheet sheet = workbook.createSheet(title);

// 设置表格默认列宽度为15个字节

sheet.setDefaultColumnWidth((short) 15);

// 生成一个样式

HSSFCellStyle style = workbook.createCellStyle();

// 设置这些样式

style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 生成一个字体

HSSFFont font = workbook.createFont();

font.setColor(HSSFColor.VIOLET.index);

font.setFontHeightInPoints((short) 12);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// 把字体应用到当前的样式

style.setFont(font);

// 生成并设置另一个样式

HSSFCellStyle style2 = workbook.createCellStyle();

style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);

style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

style2.setBorderTop(HSSFCellStyle.BORDER_THIN);

style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 生成另一个字体

HSSFFont font2 = workbook.createFont();

font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

// 把字体应用到当前的样式

style2.setFont(font2);


// 声明一个画图的顶级管理器

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

// 定义注释的大小和位置,详见文档

HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));

// 设置注释内容

comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));

// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.

comment.setAuthor("leno");


//产生表格标题行

HSSFRow row = sheet.createRow(0);
ResourceBundle resource = ResourceBundle.getBundle(fileName);
Set<String> keys = resource.keySet();

int i = 0;
for (String key : keys) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(style);

HSSFRichTextString text = new HSSFRichTextString();

cell.setCellValue(resource.getString(key));
i++;

}


//遍历集合数据,产生数据行

Iterator<T> it = dataset.iterator();

int index = 0;

while (it.hasNext()) {

index++;

row = sheet.createRow(index);

T t = (T) it.next();

//利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值

int j = 0;
for (String key : keys) {

HSSFCell cell = row.createCell(j);

cell.setCellStyle(style2);


String getMethodName = "get"
+ key;

try {

Class tCls = t.getClass();

Method getMethod = tCls.getMethod(getMethodName,
new Class[]{});

Object value = getMethod.invoke(t, new Object[]{});

//判断值的类型后进行强制类型转换

String textValue = null;

if (value instanceof Boolean) {

boolean bValue = (Boolean) value;

textValue = "true";

if (!bValue) {

textValue = "false";

}

} else if (value instanceof Date) {

Date date = (Date) value;

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

textValue = sdf.format(date);

} else {

//其它数据类型都当作字符串简单处理

textValue = value.toString();
System.out.println(textValue);

}

//如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成

if (textValue != null) {

Pattern p = Pattern.compile("^\\d+(\\.\\d+)?$");

Matcher matcher = p.matcher(textValue);

if (matcher.matches()) {

//是数字当作double处理

cell.setCellValue(Double.parseDouble(textValue));

} else {

HSSFRichTextString richString = new HSSFRichTextString(textValue);

HSSFFont font3 = workbook.createFont();

font3.setColor(HSSFColor.BLUE.index);

richString.applyFont(font3);

cell.setCellValue(richString);
}

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {
//清理资源
}
j++;
}

}

try {

workbook.write(bos);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

}

return baos;

}
}
获得生成的excel流后,就可以通过弟啊用downloadAction方法了
public static void downloadAction(String fileName,ByteArrayOutputStream baos) {
try {

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls"); //不是内嵌显示(inline),而是作为附件下载
response.setContentLength(baos.size());
response.setContentType("application/vnd.ms-excel");
//response.reset();这个方法千万不要用,会出错的!
ServletOutputStream sos = response.getOutputStream();
baos.writeTo(sos);

sos.flush();
baos.close();
} catch (IOException ex) {
logger.debug(ex);
}
}
  • 0
    点赞
  • 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、付费专栏及课程。

余额充值