jsp生成的下载文档在unix下文件名的中文问题
应用中使用db2存储了用户上传的文件(word、jpg、zip等格式),一个字段存储了文件名,用户需要下载时取出这两个字段生成相应文件,在windows平台上正常,但最近移植到unix平台下时文件名不正常显示(文件内容正常),文件名也不是乱码,只是全是英文,也没有相应后缀名。怀疑是编码问题,但不知怎么改,请大家帮我看看。显示代码如下
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%
String path = request.getContextPath();
String filename=(String)request.getAttribute("fileName");
filename=new String(filename.getBytes(),"iso-8859-1");
//取文件类型
String filter=filename.substring(filename.lastIndexOf("."));
response.setContentType("APPLICATION/OCTET-STREAM");
if(filter.equals(".txt"))
{
response.setContentType("text/plain");
}
else if(filter.equals(".doc")||filter.equals(".dot"))
{
response.setContentType("application/msword;charset=GB2312");
}
else
{
response.setContentType("image/jpeg;charset=GB2312");
}
response.reset();
response.setHeader("Content-Disposition","attachment;filename="+filename);
response.setHeader("Connection","close");
byte[] fileByte=(byte[])request.getAttribute("fileByte");
//response.reset();
response.getOutputStream().write(fileByte);
response.getOutputStream().close();
%>
问题点数:20、回复次数:1Top
1 楼koma_wind()回复于 2006-03-04 00:30:57 得分 0
做一个编码处理看看
/**
* 编码
* @param s
* @return
*/
public static String getUtf8Str(String s){
String ret=null;
try {
ret=java.net.URLEncoder.encode(s, "utf-8");
} catch (UnsupportedEncodingException ex) {
}
return ret;
}
/**
* 解码
* @param s
* @return
*/
public static String getStrUtf8(String s){
String ret=null;
try {
ret=java.net.URLDecoder.decode(s, "utf-8");
} catch (UnsupportedEncodingException ex) {
}
return ret;
}Top




