 
- 
   struts2文件添加时间:2013-5-17 点击量: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.javaimport 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();
 %>
 
 所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
 
  
   
 

 
 


