`
kowen
  • 浏览: 113585 次
  • 性别: Icon_minigender_1
  • 来自: 东营
社区版块
存档分类
最新评论

struts 下载时 中文文件名乱码问题的解决

    博客分类:
  • ssh
阅读更多
使用struts下载文件时,在ie中文件名称总是显示乱码,以下是解决方法,在firefox和ie下都正常
struts配置:
 		<action name="download" class="pbc.ljf.lilv.action.ReportAction"
			method="download">
			<result name="success" type="stream">
				<param name="contentType">
					application/vnd.ms-excel
				</param><!--下载文件的类型-->
				<param name="inputName">reportFile</param>
				<param name="contentDisposition">
					attachment;filename="${chineseFileName}.xls"
				</param><!--下载文件的名称,注意这里需要定义attachment;  否则就会默认inline 表示浏览器会尝试打开-->
				<param name="bufferSize">4096</param><!--下载缓冲区大小-->
			</result>
		</action>

action类中代码:
	/**
	 * 文件名称转换为中文
	 * @return
	 */
	public String getChineseFileName(){
		String chineseName = "noname";
		try {
			chineseName = new String(fullName.getBytes("gb2312"), "iso8859-1");
		} catch (UnsupportedEncodingException e) {
			log.error("转换中文失败",e);
		}
		return chineseName;
	}


配置文件中的${chineseFileName} 会调用action类中getChineseFileName()获取中文名称
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics