笔者在最近的项目中遇到动态表格中下载文件的问题,以下是测试的小例子,支持下载中文名的文件.
一 页面test.jsp
1<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
2<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
3<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
4<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
5<html>
6<head>
7<title>文件列示</title>
8</head>
9<body>
10<f:view>
11<h:form>
12 <t:panelGrid>
13
14 <t:panelGroup>
15 <!-- this is table -->
16 <t:dataTable id="data" value="#{fileBean.contentList}" var="result" border="1" width="100%" rows="10">
17
18 <t:column rendered="true">
19 <f:facet name="header">
20 <t:outputText value="编号" />
21 </f:facet>
22 <t:outputText value="#{result.number}" />
23 </t:column>
24
25 <t:column rendered="true">
26 <f:facet name="header">
27 <t:outputText value="名称" />
28 </f:facet>
29 <t:outputText value="#{result.name}" />
30 </t:column>
31
32 <t:column rendered="true">
33 <f:facet name="header">
34 <t:outputText value="大小" />
35 </f:facet>
36 <t:outputText value="#{result.size}" />
37 </t:column>
38
39 <t:column rendered="true">
40 <f:facet name="header">
41 <t:outputText value="操作" />
42 </f:facet>
43 <t:commandButton value="下載" actionListener="#{fileBean.downloadFile}" />
44 </t:column>
45 </t:dataTable>
46
47 </t:panelGroup>
48 </t:panelGrid>
49</h:form>
50</f:view>
51</body>
52</html>
二 MyFile.java
1package com.jakin;
2
3public class MyFile {
4 private String number;
5 private String name;
6 private String size;
7
8 public MyFile(String number, String name, String size) {
9 super();
10 this.number = number;
11 this.name = name;
12 this.size = size;
13 }
14
15 public String getNumber() {
16 return number;
17 }
18
19 public void setNumber(String number) {
20 this.number = number;
21 }
22
23 public String getName() {
24 return name;
25 }
26
27 public void setName(String name) {
28 this.name = name;
29 }
30
31 public String getSize() {
32 return size;
33 }
34
35 public void setSize(String size) {
36 this.size = size;
37 }
38}
39
三,JSF中的bean:FileBean
1package com.jakin;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.List;
8
9import javax.faces.component.UIColumn;
10import javax.faces.component.UIComponent;
11import javax.faces.component.html.HtmlDataTable;
12import javax.faces.context.FacesContext;
13import javax.faces.event.ActionEvent;
14import javax.servlet.ServletOutputStream;
15import javax.servlet.http.HttpServletResponse;
16
17public class FileBean {
18 private List<MyFile> contentList; // 存放文件的集合
19
20 public List<MyFile> getContentList() {
21 contentList = new ArrayList<MyFile>();
22 for (int i = 0; i < 5; i++) {
23 MyFile myFile = new MyFile(i + "", i + ".txt", "100K");
24 contentList.add(myFile);
25 }
26 return contentList;
27 }
28
29 public void setContentList(List<MyFile> contentList) {
30 this.contentList = contentList;
31 }
32
33 public void downloadFile(ActionEvent e) {
34 UIComponent component = (UIComponent) e.getSource();
35 UIColumn column = (UIColumn) component.getParent();
36 HtmlDataTable dataTable = (HtmlDataTable) column.getParent();
37 int listIndex = dataTable.getRowIndex();
38 MyFile myFile = (MyFile) contentList.get(listIndex);
39 String name = myFile.getName();
40 // String name = "轻松搞定XML.pdf";
41
42 try {
43 File file = new File("D:\\project\\UploadFile\\" + name);
44 HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext
45 .getCurrentInstance().getExternalContext().getResponse();
46 ServletOutputStream servletOutputStream = httpServletResponse
47 .getOutputStream();
48
49 name = java.net.URLEncoder.encode(name, "UTF-8");
50 httpServletResponse.setHeader("Content-disposition",
51 "attachment; filename=\"" + name + "\"");
52 httpServletResponse.setContentLength((int) file.length());
53 httpServletResponse
54 .setContentType("application/x-download;charset=UTF-8");
55
56 byte[] b = new byte[1024];
57 int i = 0;
58 FileInputStream fis = new java.io.FileInputStream(file);
59 while ((i = fis.read(b)) > 0) {
60 servletOutputStream.write(b, 0, i);
61 }
62 } catch (IOException ioe) {
63 ioe.printStackTrace();
64 }
65 FacesContext.getCurrentInstance().responseComplete();
66 }
67
68}