Mono 基于Jexus发布,创建中文文件名乱码及解决办法
最近发现MVC在Linux的上传中文文件, 使用 ls命令查看文件名显示乱码.
几经周折,经历了:文件名编码转换,指定web的contentype编码等,最终然并卵.
Google上查到这个
http://www.mono-project.com/docs/faq/technical/
内容如下:
MONO_EXTERNAL_ENCODINGS
When I run an application, I get the following error message:
** Message: Bad encoding for '????' Consider using MONO_EXTERNAL_ENCODINGS
This problem arises when you have files in your file system that Mono can not convert into Unicode. Mono uses the UTF-8 encoding for the filenames stored in your file system by default, because it is the universally accepted standard.
The problem typically arises when you transfer files that were stored on a system that used a different encoding. This might happen if you copy a backup from an older system that encoded filename is latin-1 encoding and your current system uses UTF-8 (Example: the old system probably had LANG set to “en_US” and the new system uses “en_US.UTF-8).
It is highly recommended that you fix the encoding of your filenames on your file system using a tool like convmv, a perl utility that lets you rename files from one encoding to another.
Alternatively, you can set the MONO_EXTERNAL_ENCODINGS variable, but this is not recommended. To use this set the MONO_EXTERNAL_ENCODINGS variable to a comma separated list of encodings that the Mono runtime should try to use when guessing the values encoded in a filename.
See the manual page for details on how Mono uses this variable.
The values allowed are those returned by “iconv –list”.
This is a sample:
export MONO_EXTERNAL_ENCODINGS="utf8:latin1"
Notice that in older versions of Mono, the error message had a typo, and said “MONO_EXTERNAL_ENCODING” instead of “MONO_EXTERNAL_ENCODINGS”
The problem with using MONO_EXTERNAL_ENCODINGS is that even if Mono will be able to parse your filenames, Mono will still store the filenames internall as Unicode. If you try to move, overwrite or do any other manipulation in the file Mono will transform the filename from Unicode to your native encoding and it might fail to find the file.
通过export命令查看全局设置,里面并没有 MONO_EXTERNAL_ENCODINGS的配置.
有陷入了僵局.
周末继续折腾,写了控制台程序创建文件发现中文文件名并无问题,说明mono自生并无问题,
重点放在Jexus上, 遂写了个最简单的web上去创建文件,问题重现.
继续搜索资料 鄙视下百度根本查不到.
找到 张善友的一篇文章
http://www.cnblogs.com/shanyou/archive/2012/01/07/2315982.html
让jexus能够读取中文乱码文件名 1、安装GBK等字符集: 步骤一:修改:/var/lib/locales/supported.d/local文件,添加一行,内容是“zh_CN GBK”。(当然,还可以加上“zh_CN GB18030”等); 步骤二:运行命令 sudo locale-gen 2、在jws.start和jws.restart两个文件中,添加一个环境变量:“export MONO_EXTERNAL_ENCODINGS="gbk:gb18030"”
注:以上方法适用于jexus 5.2
这段话提醒了我.
啥 Jexus有MONO_EXTERNAL_ENCODINGS的配置.
遂,组不查找,jwsd,jws 啥找到了
jws 中有句:
export MONO_EXTERNAL_ENCODINGS="gbk:gb18030:gb2312"
注释掉试试.
发现注释了,重启Jexus,中文名正确了.
赶紧试试 修改成加上utf8如下:
export MONO_EXTERNAL_ENCODINGS="utf8:gbk:gb18030:gb2312"
测试也ok.
为何Jexus默认安装添加了这个配置, 等Jexus的作者 宇内流云 解释,估计是处于其他什么考虑.
顺便看到5.2的发布说明:
与5.1相比,Jexus5.2有如下变化:
1、修复了5.1发布以来发现的所有BUG;
2、为FastCGI、反向代理增加了对“多重Cookie”的支持;
3、增加了对响应数据进行“分块传输”的支持;
4、增加了正常读取中文乱码文件名(windows文件复制到linux系统上后中文文件名成为乱码)的支持;
5、为ASP.NET开放了开放HTTP OPTIONS/PUT/DELETE协议,为“RESTful Web API”的正常运行提供了平台基础;
6、对线程池控制、应用程序域域间数据传递方式进行了改进,性能特别是ASP.NET的处理速度得到大幅度提升(静态文件处理速度提高了5%,ASP.NET处理速度提高了40%)。
不知是否与4有关.