您的位置:首页 > 编程语言 > Java开发

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

2015-07-10 15:18 746 查看
最近在看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
}

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: