使用struts2实现文件下载

时间:2023-02-26 21:16:01

<action name="downloadAction" class="">
<result type="stream">
<param name="contentDispositon">attachment;filename=${filename}</param><!--attachment表示不打开下载,设置文件的名字-->
<param name="inputName">downloadFile</param><!-- 设置读取的方法(在action中的方法对应的getDownloadFile(){}) -->
</result>
</action>

 public InputStream getDownloadFile(){
try{
if(1==number){
this.filename=new String(this.filename.getBytes("gbk"),"8859_1");
return ServletActionContext.getServletContext().getResourceAsStream("/upload/java.txt");
}else{
return ServletActionContext.getServletContext().getResourceAsStream("/upload/....");
}
}catch(Exception ex){
ex.printStackTrace();
} return null;
}