1,在action中定义一个方法。
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getApplytable() throws Exception {
fileName = new String(fileName.getBytes("gb2312"),"ISO8859-1");
String downPath = ServletActionContext.getServletContext().getRealPath("/");
File file = new File(downPath + "download/林业科学数据共享协议书.doc");
InputStream input = new FileInputStream(file);
return input;
}
该方法的的返回值必须为InputStream,fileName为你action中传过来的文件名。
在struts的配置文件中:
<action name="downloadapplytable" class="org.cfsdc.action.IndexAction" >
<result type="stream">
<param name="contentType">application/msword</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">applytable</param>
<param name="bufferSize">4096</param>
</result>
</action>
上面的inputName必须和你action里面的返回值为InputStream的方法的方法名相同。