FTPClient是经常用到的ftp第三方开发包。但是其默认的编码方式是基于“ISO-8859-1”的。因此下载FTP服务器文件时,获得的文件名称中如果含有中文,显示会出现乱码。通过浏览FTPClient的源代码,在FTP.java(commons-net-2.0-src)文件中,含有相关的编码设置:
// We have to ensure that the protocol communication is in ASCII // but we use ISO-8859-1 just in case 8-bit characters cross // the wire. /** * The default character encoding used for communicating over an * FTP control connection. The default encoding is an * ASCII-compatible encoding. Some FTP servers expect other * encodings. You can change the encoding used by an FTP instance * with {@link #setControlEncoding setControlEncoding}. public static final String DEFAULT_CONTROL_ENCODING = "ISO-8859-1"; private static final String __modes = "AEILNTCFRPSBC"; |
由于FTPClient继承了FTP类,可以在FTPClient对象中调用setControlEncoding方法,将编码改为:
ftpClient.setControlEncoding("gb2312"); |
此时,应该就是方便的处理中文文件名称了。