我用struts2写了一个文件下载,都是常规的做法,可是下载到的是实现下载的action,即XXX.action,本来是一张GIF图片,下载后,把后缀改成.gif,即可打开浏览,请问各位高手,帮忙查找一下原因,无敌感谢!

解决方案 »

  1.   


    struts2 下载不用写response,我贴出我的代码吧
      

  2.   

    action: private String fileName ;
     //private String inputPath;// 指定要被下载的文件路径 
    //  public void setInputPath(String value) {    
    //       inputPath = value;  
    //  }    
      
     public void setFileName(String fileName) {    
           System.out.println("filename : "+fileName);
          this.fileName = fileName;    
     }      
     public String download() throws Exception {  
      System.out.println("============================> contenttype : "+request.getContentType());
            return SUCCESS;  
     }  
     public InputStream getInputStream() throws Exception {    
      String savedir = getPath();
      File file = new File(savedir, fileName);
            // 通过 ServletContext,也就是application 来读取数据  
      if(file.exists()){
      
      //System.out.println("file 存在空");
      //return new FileInputStream(file);
      MessageInfo mi = getMessageInfoDetail();
      return ServletActionContext.getServletContext().getResourceAsStream("/"+mi.getAttachPath()+"\\" +fileName);
      }
      //MessageInfo mi = getMessageInfoDetail();
      //ServletActionContext.getServletContext().getResourceAsStream("/"+mi.getAttachPath()+"\\" +fileName);
         
      return null;
      
      }      public String getDownloadFileName() {    
      
            String downFileName = fileName;    
      
            try {    
      
                downFileName = new String(downFileName.getBytes("ISO-8859-1"), "utf8");    
      
            } catch (UnsupportedEncodingException e) {    
      
                e.printStackTrace();    
      
            }    
      
            return downFileName;    
      
     }    
    struts.xml : 
    <package name="download" extends="struts-default" namespace="/framework">
    <action name="messageAticleDownLoad" class="messageArticleAction" method="download">
    <param name="fileName"></param>
    <result name="success" type="stream">
    <param name="contentType">
    image/GIF
    </param>
    <param name="contentDisposition">  
                        attachment;filename="${fileName}"
                    </param>
                    <param name="inputName">inputStream</param>  
                    <param name="bufferSize">4096</param>    
    </result>
    </action>
    </package>jsp页面 :<s:iterator value="listPhoto" id="photo">

       <a href="/framework/messageAticleDownLoad.do?fileName=
           <s:property value='#photo.name'/>" ><s:property value="#photo.oldName" /></a> 
                    <br />  
    </s:iterator>
      

  3.   

    问题已解决。问题在于struts.xml  配置文件,其中
    <param name="contentDisposition">   
      attachment;filename="${fileName}"
    </param>
    写得不规范,应写作:
    <param name="contentDisposition">  
      attachment;filename=${#request.fileName}
    </param>。哈哈。。谢谢关注!