struts2文件下载的实现

http://elf8848.iteye.com/blog/640964

http://elf8848.iteye.com/blog/275823

struts2.xml配置:

1         <action name="ExportDB" class="edu.xjtu.sei.xxxx.action.DBManager.ExportDBAction">
2             <result name="success" type="stream">
3                 <param name="contentType">application/octet-stream</param>
4                 <param name="inputName">inputStream</param>                
5                 <param name="bufferSize">409600</param>
6                 <param name="contentDisposition">attachment;filename="${filename}"</param>
7                 <param name=""></param>
8             </result>    
9         </action > 


ExportDBAction.java

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

import edu.xjtu.sei.xxx.business.dao.HostInfectDAO;
import edu.xjtu.sei.xxx.util.DateUtils;

public class ExportDBAction extends ActionSupport{
    String filename;
    String flag;
    String errormessage;
    String properties;
    
    private HostInfectDAO hostinfectdao;
   public void setHostInfectDAO(HostInfectDAO hostinfectdao) {
        this.hostinfectdao = hostinfectdao;
    }
    
public String getProperties() {
        return properties;
    }
    public void setProperties(String properties) {
        this.properties = properties;
    }
    boolean success;
    public String execute() {

success
=true; return SUCCESS;
}
public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getFlag(){ return flag; } public void setFlag(String flag){ this.flag=flag; } public String getErrormessage() { return errormessage; } public void setErrormessage(String errormessage) { this.errormessage = errormessage; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public InputStream getInputStream() throws IOException { List results=null; StringBuilder export=new StringBuilder(); results=hostinfectdao.getExportDB(flag); export.append("IP,域名,时间"+"\n"); for (Iterator iter = results.iterator(); iter.hasNext();) { Object[] row=(Object[]) iter.next(); String ip=row[0].toString(); String dns=row[1].toString(); String time=DateUtils.formatDateEx((Date)row[3]).toString(); export.append(ip+","+dns+","+time+"\n"); } ByteArrayInputStream stream = new ByteArrayInputStream(export.toString().getBytes("gbk")); return stream; } public static void main(String args[]) throws IOException{ ExportDBAction a=new ExportDBAction(); a.setProperties("1"); a.getInputStream(); } }


请求action的js函数:

function exportTable(filename,flag){
        //window.location.href = "ExportDB.action?filename="+filename+".csv&flag="+flag+"&contentType=application/octet-stream";
     window.location.href = "ExportDB.action?filename="+filename+".csv&flag="+flag;
}

之前IE浏览器下载的时候一直自动打开文件。
那时struts.xml里的<param name="contentDisposition">attachment;filename="${filename}"</param>

是这样写的:<param name="contentDisposition">filename="${filename}"</param>

后来把attachment加上就好了。

参考文章说的很清楚:

要直接下载一个文件,我们需要做两件事,

第一件事是:设定响应的内容类为“application/octet-stream”,大小写无关。

第二件事是:设置HTTP的响应头名字为:Content-Disposition,设定值为:attachment;   filename   =   theFileName。这里的theFileName就是出现在文件下载对话框里的默认文件名,通常和所下载的文件名字相同,但也可以不同。

jsp页面的实现代码:

html代码:

      <%  
      //   得到文件名字和路径  
      String   filename   =   "MengxianhuiDocTest.doc";  
      String   filepath   =   "D:\\";  
       
      //   设置响应头和下载保存的文件名  
      response.setContentType("APPLICATION/OCTET-STREAM");  
      response.setHeader("Content-Disposition",  
      "attachment;   filename=\""   +   filename   +   "\"");  
     
      //   打开指定文件的流信息  
      java.io.FileInputStream   fileInputStream   =  
      new   java.io.FileInputStream(filepath   +   filename);  
         
      //   写出流信息  
      int   i;  
      while ((i=fileInputStream.read())   !=   -1)   {  
        out.write(i);  
      }  
      fileInputStream.close();  
      out.close();  
    %>   
原文地址:https://www.cnblogs.com/peterpanzsy/p/3081258.html