<--struts2配置文件-->
<action name="exportInformation" class="org.scbit.lsbi.xny.action.InformationAction"
method="exportInformation">
<result name="success" type="stream"><!--type 为 stream 应用 StreamResult 处理-->
<param name="contentType">application/octet-stream</param><!--默认为 text/plain-->
<param name="inputName">inputStream</param>
<!-- 默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法,
比如这里就是 getInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文
件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
这里使用的是动态文件名,${fileName}, 它将通过 Action 的 getFileName() 获得文件名 -->
<param name="bufferSize">4096</param><!-- 输出时缓冲区的大小 -->
</result>
</action>
<--action-->
public String exportInformation(){
if(search==null)
search = new ArticleSearch();
try {
BasicExport export = new BasicExport(ExcelTypeEnum.information, informationService);
File file = export.export(search);
inputStream = new FileInputStream(file);
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
if(isMSIE){
setExcelFileName(URLEncoder.encode(file.getName(),"UTF-8"));
}else{
setExcelFileName(new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SnecException e) {
e.printStackTrace();
}
return SUCCESS;
}
转载于:https://my.oschina.net/dyl226/blog/664323