Struts2 文件下载及中文乱码问题的解决方案(下载各种格式文件)

一 struts.xml文件的编辑  
   <struts>  
  <package name="crud-default" extends="convention-default">   
    <action name="download" class="com.myweb.download.DownLoadAction">  
     <!--type 为 stream 应用 StreamResult 处理-->  
    <result name="success" type="stream">   
     <!--默认为 text/plain-->  
     <param name="contentType">application/x-msdownload;charset=ISO8859-1</param>  
     <!-- 默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法,   
比如这里就是 getInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 -->  
     <param name="inputName">inputStream</param>  
     <!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename   
指定下载文件时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名 -->  
     <param name="contentDisposition">attachment;filename="${fileName}"</param>  
     <!-- 输出时缓冲区的大小 -->  
     <param name="bufferSize">4096</param>  
    </result>  
    </action>   
   </package>  
</struts>


package com.used.struts2.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.used.struts2.bean.UserBean;
import com.used.struts2.utils.ConfigMessages;
import com.used.struts2.utils.CreaterCSV;

public class DownLoadAction extends ActionSupport {
	private static final long serialVersionUID = 2013455583651749435L;
	private final static String DOWNLOADFILEPATH=ConfigMessages.getString("reportServerDir");
	private String filename;
	private List<UserBean> UserBeanList;
	
	
	public String reportCSV(){
		UserBeanList =  getUserList();
		 List<UserBean> list = new ArrayList<UserBean>();
		 for (int i = 0; i < UserBeanList.size(); i++) {
			UserBean job = new UserBean();
         	job.setUserId(UserBeanList.get(i).getUserId());
         	job.setUserName(UserBeanList.get(i).getUserName());
         	job.setTel(UserBeanList.get(i).getTel());
            list.add(job);
         }
		  try {
			filename = ConfigMessages.getString("reportOmnitureFileName");
			CreaterCSV.CreateCsvfiles(ConfigMessages.getString("reportServerDir"), filename, UserBean.getList(), list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return SUCCESS;
	}
	
	
	//封装list
	public List<UserBean> getUserList(){
		List<UserBean> userList = new ArrayList<UserBean>();
		
		for(int i=0;i<5;i++){
			UserBean ub = new UserBean();
			ub.setUserId("001"+i);
			ub.setUserName("ADMIN_"+i);
			ub.setTel(123+i);
			userList.add(ub);
		}
		return userList;
	}
	
	
	
	 //从下载文件原始存放路径读取得到文件输出流  
    public InputStream getDownloadFile() {
    		InputStream is = null;
			try {
				is = new FileInputStream(DOWNLOADFILEPATH+filename+".csv");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
    		  return  is; 
    } 
	
    //如果下载文件名为中文,进行字符编码转换  
    public String getDownloadChineseFileName() {  
        String downloadChineseFileName = filename;  
  
        try {  
            downloadChineseFileName = new String(downloadChineseFileName.getBytes(), "ISO8859-1"); 
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        return downloadChineseFileName+".csv";  
    }

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public static String getDownloadfilepath() {
		return DOWNLOADFILEPATH;
	}
	
	
}


package com.used.struts2.utils;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * 读取配置文件
 * @author ADMIN
 *
 */
public class ConfigMessages {
	private static final String BUNDLE_NAME = "configures"; 
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
	
	private ConfigMessages() {
	}
	
	public static String getString(String key) {
		try {
			return RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			System.out.println("get properties file error!");
			return null;
		}
	}
	
}


reportServerDir= E\:/workspace/apache-tomcat-6.0.20/webapps/USED_Project/report/




你可能感兴趣的:(Struts2 文件下载及中文乱码问题的解决方案(下载各种格式文件))