在广州地铁管理后台中使用了commons-fileupload来上传文件,发现中文路径或文件名都会变成乱码。但是上传部分的代码是已经在我们的QuickOA里面应用了的,而在QuickOA里面完全没有乱码问题。搜索fileupload的代码,发现在 FileUploadBase类里面有setHeaderEncoding方法,根据其说明,当读取上传表单的各部分时会用到该encoding,如果没有指定encoding则使用系统缺省的encoding。在QuickOA中因为页面是使用“GBK”编码,所以在简体中文的系统下面不会有乱码问题;而在广州地铁管理后台中页面是使用“UTF-8”编码,就必须指定编码。考虑到QuickOA以后有可能运行在英文或繁体中文之类的系统中,在 QuickOA中也指定编码。
代码: |
/** * Specifies the character encoding to be used when reading the headers of * individual parts. When not specified, or <code>null</code>, the platform * default encoding is used. * * @param encoding The encoding used to read part headers. */ public void setHeaderEncoding(String encoding) { headerEncoding = encoding; } |