日期:2014-05-18  浏览次数:20776 次

还是数据库中文乱码问题,试了几种方法还不行,在线等~
我已经统一了编码,   页面上都用UTF-8,   数据库也用UTF-8写入,一切都很好,没问题
MySQL数据库..进去查看中文显示很正常

当我从数据库取出中文文件名调用下载后,下载的文件名乱码了...

是不是应该把文件名转换回gb2312?我试了,好像也不对..

谢谢

------解决方案--------------------
当我从数据库取出中文文件名调用下载后,下载的文件名乱码了...
__________________________________________________________________

下载后你的文件名应该转一下吧:
String fileName = "你的文件名称 ";
fileName = new String(fileName.getBytes( "GB2312 "), "UTF-8 ");
试一下。
------解决方案--------------------
/**
* 下载
*
* @param mapping
* @param request
* @param response
* @return
*/
private ActionForward downloadTranslation(ActionMapping mapping,
HttpServletRequest request, HttpServletResponse response) {

String file = "d:/xxxxxxxxxxx.txt ";
// 开始下载文件
try {
//
File localfile = new File(file); // 用户要下载的文件内容
String filename = localfile.getName();// 用户下载时提示保存的文件名称
//
response.setContentType( "application/x-download;charset=GB2312 ");
response.setHeader( "Content-Disposition ", "attachment;filename= "
+ URLEncoder.encode(filename, "UTF8 "));
response.setContentLength((int) localfile.length());
//
int i = 0;
byte[] bt = new byte[1024];
FileInputStream fis = new FileInputStream(localfile);
ServletOutputStream sos = response.getOutputStream();
while ((i = fis.read(bt)) != -1) {
sos.write(bt, 0, i);
}
sos.flush();
sos.close();
sos = null;
fis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
------解决方案--------------------
使用Filter吧,就不用做大量的编码转换工作了
------解决方案--------------------
Filter好象也过滤不了文件名吧,下载的文件名是在消息头里的,filter只过滤消息体。
------解决方案--------------------
文件名字应该转成UTF-8
------解决方案--------------------
String fileName = "你的文件名称 ";
fileName = new String(fileName.getBytes( "GB2312 "), "UTF-8 ");
fileName = new String(fileName.getBytes( "GB2312 "), "iso-8859-1 ");
fileName = new String(fileName.getBytes( "UTF-8 "), "iso-8859-1 ");
fileName = new String(fileName.getBytes( "UTF-8 "), "gb2312 ");
fileName = new String(fileName.getBytes( "iso-8859-1 "), "UTF-8 ");
fileName = new String(fileName.getBytes( "iso-8859-1 "), "GB2312 ");
自己试试吧,看上面哪种转码能给你转出来

------解决方案--------------------
http://blog.chinaunix.net/u/21684/showart_208239.html
------解决方案--------------------
1.看看是不是你取出来的所有的中文都是乱码?

2。如果别的中文是好的,那就说明,你在下在文件时候用的文件名的编码不正确
看看
response.setContentType( "application/x-download;charset=GB2312 ");
response.setHeader( "Content-Disposition ", "attachment;filename= "
+ URLEncoder.encode(filename, "UTF8 "));
这个地方你写的对不对。
这个写法我是从上面一个兄弟的回答中拷贝下来的。乱码问题,一般就处在这个地方。