struts2文件下载

本文介绍了一个关于Struts2文件下载的问题及解决方案。在尝试通过JSP访问URL下载时,出现找不到`inputStream`的异常。解决办法包括检查Struts配置文件中的action设置,确保文件路径正确,并确认ServletActionContext获取到的资源输入流对应的是服务器上实际存在的文件。对于外部文件,需要手动创建输入流来实现下载。
摘要由CSDN通过智能技术生成

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值