解决struts2.0下载中文文件名乱码问题

出自:http://hi.baidu.com/vhook/blog/item/d1791e9a5743b3b2c9eaf4ab.html

struts2多文件动态下载及中文乱码2009-02-17 12:03关于文件上传的例子,网上已经有很多,所以本文就不谈文件上传,重点是文件下载及文件名为中文的时候各个浏览器出现乱码的解决方案。

下面是jsp文件的代码:

<html>
<head>
<title>download</title>
</head>
<body>
<%
//取得服务器"/download/file"目录的物理路径
String path = request.getRealPath("/download/file");
//取得"/download/file"目录的file对象
File file = new File(path);
//取得file目录下所有文件
File[] files = file.listFiles();

for (int i = 0; i < files.length; i++) {

String fname = files[i].getName();

//对文件名进行url编码(UTF-8指明fname原来的编码,UTF-8一般由本地编码GBK代替)
fname =[color=red] java.net.URLEncoder.encode(fname, "UTF-8");[/color]

out.println("<a href=download.action?name=" + fname + ">"
+ files[i].getName() + "</a><br>");
}
%>
</body>
</html>

相应的DownloadAction.java的代码:

package com.test.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = 6329383258366253255L;

private String fileName;

public void setFileName(){
//得到请求下载的文件名
String fname=ServletActionContext.getRequest().getParameter("name");
try {
/*
* 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。
* 这里使用request.setCharacterEncoding解码无效.
* 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
* */
[color=red]fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8"); [/color]

} catch (Exception e) {
e.printStackTrace();
}
this.fileName=fname;
System.out.println(fileName);
}

/*
* @getFileName
* 此方法对应的是struts.xml文件中的:
* <param name="contentDisposition">attachment;filename="${fileName}"</param>
* 这个属性设置的是下载工具下载文件时显示的文件名,
* 要想正确的显示中文文件名,我们需要对fileName再次编码
* 否则中文名文件将出现乱码,或无法下载的情况
* */
public String getFileName() throws UnsupportedEncodingException {

[color=red]fileName=new String(fileName.getBytes(),"ISO-8859-1"); [/color]

return fileName;
}

/*
* @getDownloadFile
* 此方法对应的是struts.xml文件中的:
* <param name="inputName">downloadFile</param>
* 返回下载文件的流,可以参看struts2的源码
* */
public InputStream getDownloadFile() {

this.setFileName();

return ServletActionContext.getServletContext().getResourceAsStream("/download/file/" + fileName);
}

@Override
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml相应的Action配置:

<action name="download" class="com.test.action.DownloadAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
web.xml:

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>download.jsp</welcome-file>
</welcome-file-list>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值