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

最近在看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
	}

}


 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值