使用commons-net-2.0.jar包进行FTP操做
使用FTPClient下载文件时,若是路径中有中文没法下载,将路径
转码为iso-8859-1后,能够下载
还有一种方式,再new FTPClient() 后,能够设置编码,
ftpClient = new FTPClient();
ftpClient.setControlEncoding(GBK); //不能在connect,login以后设置
ftpClient.connect(ip, port);
ftpClient.login(userName, passWord);
但必定要在建立时设置,不能在链接、登陆后再设置,不然不生效
查看源码得知
FTPClient 继承FTP,FTP 继承 SocketClient,
因此ftpClient调用方法connect()时,会调用_connectAction_()方法,若是尚未没置编码,
getControlEncoding()会默认使用ios-8859-1,
因此必需在connect前完成编码设置
FTP在_connectAction_()方法时使用设置的编码
protected void _connectAction_()
throws IOException
{
super._connectAction_();
this._controlInput_ = new BufferedReader(new InputStreamReader(this._socket_.getInputStream(), getControlEncoding()));
this._controlOutput_ = new BufferedWriter(new OutputStreamWriter(this._socket_.getOutputStream(), getControlEncoding()));
__getReply();
if (FTPReply.isPositivePreliminary(this._replyCode))
__getReply();
}
FTP 继承 SocketClient,connect()时调用_connectAction_()
public void connect(InetAddress host, int port)
throws SocketException, IOException
{
this._socket_ = this._socketFactory_.createSocket();
this._socket_.connect(new InetSocketAddress(host, port), this.connectTimeout);
_connectAction_();
}ios