struts2 下载文件不能显示中文名字的问题
2015-07-10 15:18
746 查看
最近在看Struts 2 的知识,遇到一个下载不能显示中文名字的问题,查找了网上的的内容,最终得到了解决。
Struts.xml
action:
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 } }
相关文章推荐
- Java设计模式之访问者模式
- Java基础之IO流
- eclipse好用的快捷键
- Java 播放 .mp3 格式音乐文件
- java-集合下
- spring+shiro+jasig-cas+cxf 单点登录多点注销简单统一权限管理平台
- 银行家算法的java编程实现
- JAVA WEB 获取properties文件,存放路径 及 获取路径
- java按指定编码写入和读取文件内容的类
- struts2(三)---使用EL表达式,显示Action中的数据
- java绑定
- Eclipse注释模板设置详解
- myeclipse10 vss
- Eclipse快捷键 之 代码追踪
- java保留2位小数
- java 复选框的取值与验证(checkbox)
- java IO简介及演变的历史
- spring aop 切入点
- Spring中Quartz的配置
- Java - List遍历、判断、删除元素时的陷阱