struts2文件下载

来源:互联网 发布:淘宝联盟能挣钱吗 编辑:程序博客网 时间:2024/06/05 16:57

jsp:访问url即可下载

 <a href='Download.action?fileName=123456.java'>下载</a>

struts.xml

<struts><package name="core" namespace="/" extends="struts-default"><result-types>              <result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"/>          </result-types>   <result type="streamx">                <!-- 运行下载文件类型:指定为二进制 -->                 <param name="contentType">application/octet-stream</param>                  <!-- 对于action中的属性,返回流的属性 -->                <param name="inputName">inputStream</param>                 <!-- 下载头,包括浏览器显示的文件名,应该获取文件名,$是struts取值符号 -->                <param name="contentDisposition">attachment;filename="${downloadFileName}";</param>                <!-- 缓冲区大小设置 -->                <param name="bufferSize">4096</param>             </result>        </action>       </package></struts>

action:

package uploadFileTest;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.UnsupportedEncodingException;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class fileDownload extends ActionSupport {     private String fileName;//   private InputStream inputStream;       public String getDownloadFileName() throws Exception{            String downloadFileName = fileName;            try {             downloadFileName = new String(downloadFileName.getBytes(), "ISO8859-1");            } catch (Exception e) {             e.printStackTrace();            }            return downloadFileName;         }     //下载的流     public InputStream getInputStream() throws Exception{         System.out.println(fileName);         String realPath="";         String path ="G:\\jiang_gra_5\\jQueryTest\\download\\";         realPath=path+fileName;//       InputStream is = new FileInputStream(realPath);         InputStream inputStream=ServletActionContext.getServletContext().getResourceAsStream("/download/123456.java");//用这句话直接输出        if(null==inputStream){         System.out.println("Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name=\"inputName\"> tag specified for this action.检查action中文件下载路径是否正确.");            return null;        }else{//          return ServletActionContext.getServletContext().getResourceAsStream(realPath);            return inputStream;        }     }     @Override        public String execute(){             System.out.println(fileName);               return "success";         }     public String getFileName() throws UnsupportedEncodingException{            fileName = new String(fileName.getBytes(), "ISO-8859-1");            return fileName;     }     public void setFileName(String fileName) {            try {                fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }            this.fileName = fileName;     }}

备注:
异常:
Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the tag specified for this action.检查action中文件下载路径是否正确

检查ServletActionContext.getServletContext().getResourceAsStream(),这句话是对项目相对路径的查找,在服务器文件中检查有没有指定文件,项目相对路径从“apache-tomcat-6.0.39\webapps\项目名称”开始

如果想要获得外部文件 譬如 D盘中的某个文件,那么就要自己创建输入流才可以,如:

File file = new File("XXX");   InputStream is = new FileInputStream(file);   return is;   
0 0