FTPClient 中文路徑問題

使用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

相關文章
相關標籤/搜索