jsp页面:
<a id="HyperLink2" href="trainImport_down.do?downFileName=test_questions.xls&importType=1">题库导入模板</a>
struts2配置文件
<action name="trainImport_*" class="trainImportAction" method="{1}">
<result name="train-import">/manager/train/train-import.jsp</result>
<result name="down" type="stream">
<param name="contentType">text/xml</param>
<param name="contentDisposition">fileName="${downFileName}"</param>
<param name="inputName">inputStream</param>
</result>
</action>
注: 下载的type类型必须是"stream" 。
参数:contentType:下载类型。 contentDisposition:下载成的文件名。inputName:指定被下载文件的入口输入流。
/**获取下载文件输入流*/
private InputStream inputStream ;
/** 下载文件名 */
private String downFileName ;
/**
*
* 方法名: down
* 方法描述: (下载导入模板文件)
* 创建人:lyf
* 修改时间:2014-9-1 下午05:08:54
* 参数 @return
* 参数 @throws FileNotFoundException 参数说明
* 返回类型 String 返回类型
* @throws
*/
public String down(){
try {
String fileName = getRequest().getSession().getServletContext().getRealPath("/") + "html/system/download/" + this.downFileName;
inputStream = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return "train-import";
}
return "down";
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}