关闭

struts2 下载文件不能显示中文名字的问题

235人阅读 评论(0) 收藏 举报
分类:

最近在看Struts 2 的知识,遇到一个下载不能显示中文名字的问题,查找了网上的的内容,最终得到了解决。


Struts.xml

<action name="download" class="com.jia.action.FileDownloadAction">
       <result name="success" type="stream">
          <param name="contentType">${contentType}</param>
          <param name="inputName">targetFile</param>
          <param name="contentDisposition">filename=${downFileName}</param>
          <param name="bufferSize">4096</param> 
       </result>
    
    
    </action>

action:

package com.jia.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileDownloadAction extends ActionSupport {

	private String inputPath;
	private String contentType;
	private String downFileName;
	

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) throws Exception {
        /*
         * 必须转码,否则找不到路径
         */
		this.inputPath = new String(inputPath.getBytes("iso-8859-1"),"utf-8");
//	    this.inputPath=inputPath;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getDownFileName() {
		return downFileName;
	}


	public void setDownFileName(String downFileName) throws Exception {
		
		this.downFileName =  new String(downFileName.getBytes("iso-8859-1"),"utf-8");
	}

	public InputStream getTargetFile() throws Exception{
		//转换格式,否则中文不能显示。
		this.downFileName=new String(downFileName.getBytes(),"iso-8859-1");
		return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.err.println(downFileName);
		return super.execute();
	}

	public FileDownloadAction() {
		// TODO Auto-generated constructor stub
	}

}


 


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:665次
    • 积分:38
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档