zoukankan      html  css  js  c++  java
  • struts2文件下载 出现Can not find a java.io.InputStream with the name的错误

    成功代码:

    前台界面jsp:     <a style="text-decoration:none;" href="<%=path %>/main/frontNewsAction_downloadFile.action?fileName=<s:property value="fileTitle"/>">下载</a>
    
    
    Action文件:
    private String fileName;//get set方法
    public String getDownloadFileName() { String downFileName = fileName; try { downFileName = new String(downFileName.getBytes(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } return downFileName; } //下载的流 public InputStream getInputStream() throws FileNotFoundException { String name=this.getDownloadFileName(); File file = new File(ServletActionContext.getServletContext().getRealPath("/file")+"/"+name); InputStream inputStream = new FileInputStream(file); return new FileInputStream(file); } // 下载 public String downloadFile() throws Exception { return "downloadFile"; }
    <!-- 文件下载 -->
        		<result name="downloadFile" type="stream">
         			<param name="contentType">
         				application/octet-stream;charset=utf-8
        			</param>
         			<param name="contentDisposition">
          				attachment;filename="${downloadFileName}"
         			</param>
    				<param name="inputName">inputStream</param>
         			<param name="bufferSize">4096</param>
        		</result>
    

    出现错误时是由于在action中的public InputStream getInputStream()方法返回值为:return ServletActionContext.getServletContext().getResourceAsStream(ServletActionContext.getServletContext().getRealPath("/file")+"/"+name);

    造成的异常

    学习内容:

    配置文件:

    <!-- 文件下载,支持中文附件名 -->
       <action name="fileDownload"
        class="com.test.action.filedown.FileDownloadAction">
        <result name="success" type="stream">
         <!-- 动态文件下载的,事先并不知道未来的文件类型,那么我们可以把它的值设置成为:application/octet- stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错; -->
         <param name="contentType">
         application/octet-stream;charset=ISO8859-1
         </param>
         <param name="contentDisposition">

    <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
          对应action类中的方法 getDownloadFileName() 其中特殊的代码就是${downloadFileName},它的效果相当于运行的时候将action对象的属性的取值动态的填充在${}中间的部分,我 们可以认为它等价于+action. getDownloadFileName()。 -->

          attachment;filename="${downloadFileName}"
         </param>
         <param name="inputName">inputStream</param>
         <param name="bufferSize">4096</param>
        </result>
       </action>

    action文件:

    ----------------------------------------------------------
    action中
    ----------------------------------------------------------
    private String fileName;// 初始的通过param指定的文件名属性 set get

    /** 文件名 转换编码 防止中文乱码*/
    public String getDownloadFileName() {
       String fileName=ServletActionContext.getRequest().getParameter("fileName");
       String downFileName = fileName;
       try {
        downFileName = new String(downFileName.getBytes(), "ISO8859-1");
       } catch (Exception e) {
        e.printStackTrace();
       }
       return downFileName;
    }
    //下载的流
    public InputStream getInputStream() {
       String name=this.getDownloadFileName();
    //  String realPath=ServletActionContext.getServletContext().getRealPath("/uploadImages")+ "/"+name; 路径错误
       String realPath="/uploadImages/"+name;
       InputStream in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
       if(null==in){
        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 ServletActionContext.getServletContext().getResourceAsStream(realPath);//出现异常
    }

    @Override
    public String execute() throws Exception {
       return SUCCESS;
    }

  • 相关阅读:
    Linux 命令ln
    基于ifix组态软件研究控制按钮权限
    Linux sar 性能监控命令
    使用Oracle执行计划分析SQL性能
    谈谈解决问题的思路
    MySQL技术内幕:InnoDB存储引擎 pdf下载
    算法笔记 pdf下载
    Python最佳代码实践:性能、内存和可用性!
    利用Python进行数据分析 pdf下载
    主流 NoSQL 数据库常见应用场景详解
  • 原文地址:https://www.cnblogs.com/a757956132/p/4040666.html
Copyright © 2011-2022 走看看