org.apache.commons.net.ftp.FTPSClient在list命令后返回425

时间:2016-11-24 07:36:34

标签: java apache ftp ftps apache-commons-net

我正在尝试连接到FTPS服务器。我可以很好地连接FileZilla并列出服务器中的文件,下载等。

enter image description here

我正在尝试使用org.apache.commons.net.ftp包中的FTPSClient通过Java连接到此FTP服务器。这就是我的尝试:

FTPSClient ftp = new FTPSClient();
ftp.connect(HOST, 7021);
ftp.enterLocalPassiveMode();
ftp.login(USERNAME, PASSWORD);
final int list = ftp.list();
System.out.println("List returned: " + list);

我得到的回应是:

List returned: 425

根据Wikipedia,425表示:“无法打开数据连接。”

但是,尝试创建文件夹可以正常工作:

FTPSClient ftp = new FTPSClient();
ftp.connect(HOST, 7021);
ftp.enterLocalPassiveMode();
ftp.login(USERNAME, PASSWORD);
ftp.mkd("/laylaylom");

因此,如果我运行此代码并与FileZilla连接,我将在FTP服务器中看到文件夹“laylaylom”。

我也尝试过:

FTPSClient ftp = new FTPSClient();
ftp.connect(HOST, 7021);
ftp.login(USERNAME, PASSWORD);
final int pasv = ftp.sendCommand("PASV");
System.out.println("pasv is: " + pasv);
final int list = ftp.list();
System.out.println("List returned: " + list);

输出将是:

pasv is: 227
List returned: 522

其中227为:“进入被动模式(h1,h2,h3,h4,p1,p2)。”并且552没有列在维基百科页面中,我不确定它代表什么。

如何让list()工作?

编辑#1

在Martin Prikryl的回答之后我尝试了这个:

FTPSClient ftp = new FTPSClient();
ftp.connect(HOST, 7021);
ftp.enterLocalPassiveMode();
ftp.login(USERNAME, PASSWORD);
final FTPFile[] ftpFiles = ftp.mlistDir("/");
System.out.println(ftpFiles.length);

返回0 ..

编辑#2 WinSCP日志:

. 2016-11-24 12:52:33.947 --------------------------------------------------------------------------
. 2016-11-24 12:52:33.947 WinSCP Version 5.9.2 (Build 6958) (OS 6.1.7601 Service Pack 1 - Windows 7 Home Premium)
. 2016-11-24 12:52:33.947 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2016-11-24 12:52:33.947 Log level: Normal
. 2016-11-24 12:52:33.947 Local account: KORAYTUGAY6639\koraytugay
. 2016-11-24 12:52:33.947 Working directory: C:\Program Files (x86)\WinSCP
. 2016-11-24 12:52:33.947 Process ID: 3220
. 2016-11-24 12:52:33.947 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" 
. 2016-11-24 12:52:33.947 Time zone: Current: GMT+2, Standard: GMT+2 (E. Europe Standard Time), DST: GMT+3 (E. Europe Daylight Time), DST Start: 3/27/2016, DST End: 10/30/2016
. 2016-11-24 12:52:33.947 Login time: Thursday, November 24, 2016 12:52:33 PM
. 2016-11-24 12:52:33.947 --------------------------------------------------------------------------
. 2016-11-24 12:52:33.947 Session name: tuimcardtest@files.innbound.net (Ad-Hoc site)
. 2016-11-24 12:52:33.947 Host name: files.innbound.net (Port: 7021)
. 2016-11-24 12:52:33.947 User name: tuimcardtest (Password: Yes, Key file: No)
. 2016-11-24 12:52:33.947 Transfer Protocol: FTP
. 2016-11-24 12:52:33.947 Ping type: Dummy, Ping interval: 30 sec; Timeout: 15 sec
. 2016-11-24 12:52:33.947 Disable Nagle: No
. 2016-11-24 12:52:33.947 Proxy: None
. 2016-11-24 12:52:33.947 Send buffer: 262144
. 2016-11-24 12:52:33.947 UTF: Auto
. 2016-11-24 12:52:33.947 FTPS: Explicit TLS/SSL [Client certificate: No]
. 2016-11-24 12:52:33.947 FTP: Passive: Yes [Force IP: Auto]; MLSD: Auto [List all: Auto]; HOST: Auto
. 2016-11-24 12:52:33.947 Session reuse: Yes
. 2016-11-24 12:52:33.947 TLS/SSL versions: TLSv1.0-TLSv1.2
. 2016-11-24 12:52:33.947 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2016-11-24 12:52:33.947 Cache directory changes: Yes, Permanent: Yes
. 2016-11-24 12:52:33.947 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2016-11-24 12:52:33.947 Timezone offset: 0h 0m
. 2016-11-24 12:52:33.947 --------------------------------------------------------------------------
. 2016-11-24 12:52:34.009 Connecting to files.innbound.net:7021 ...
. 2016-11-24 12:52:34.009 Connected with files.innbound.net:7021, negotiating TLS connection...
< 2016-11-24 12:52:34.071 220 Welcome to THE FTP service.
> 2016-11-24 12:52:34.071 AUTH TLS
< 2016-11-24 12:52:34.149 234 Proceed with negotiation.
. 2016-11-24 12:52:34.212 Server asks for authentication with a client certificate.
. 2016-11-24 12:52:34.290 Verifying certificate for "Innflow" with fingerprint 54:52:0f:a0:cc:6a:38:80:d5:52:cc:9d:a4:4f:19:4a:d9:50:08:a7 and 18 failures
. 2016-11-24 12:52:34.290 Asking user:
. 2016-11-24 12:52:34.290 **The server's certificate is not known. You have no guarantee that the server is the computer you think it is.**
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Server's certificate details follow:
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Issuer:
. 2016-11-24 12:52:34.290 - Organization: Innflow, innbound.net, patrick.meier@innflow.com
. 2016-11-24 12:52:34.290 - Location: CH, Zurich, Zurich
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Subject:
. 2016-11-24 12:52:34.290 - Organization: Innflow, innbound.net, patrick.meier@innflow.com
. 2016-11-24 12:52:34.290 - Location: CH, Zurich, Zurich
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Valid: 7/6/2010 11:42:44 PM - 7/3/2020 11:42:44 PM
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Fingerprint (SHA-1): 54:52:0f:a0:cc:6a:38:80:d5:52:cc:9d:a4:4f:19:4a:d9:50:08:a7
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Summary: Self-signed certificate. The error occurred at a depth of 1 in the certificate chain.
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Certificate was not issued for this server. You might be connecting to a server that is pretending to be "files.innbound.net".
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 If you trust this certificate, press Yes. To connect without storing certificate, press No. To abandon the connection press Cancel.
. 2016-11-24 12:52:34.290 
. 2016-11-24 12:52:34.290 Continue connecting and store the certificate? ()
. 2016-11-24 12:52:35.725 Using TLSv1, cipher TLSv1/SSLv3: AES128-SHA, 1024 bit RSA, AES128-SHA              SSLv3 Kx=RSA      Au=RSA  Enc=AES(128)  Mac=SHA1
. 2016-11-24 12:52:35.787 TLS connection established. Waiting for welcome message...
> 2016-11-24 12:52:35.787 USER tuimcardtest
< 2016-11-24 12:52:35.803 331 Please specify the password.
> 2016-11-24 12:52:35.803 PASS ************
< 2016-11-24 12:52:35.865 230 Login successful.
> 2016-11-24 12:52:35.865 SYST
< 2016-11-24 12:52:35.928 215 UNIX Type: L8
> 2016-11-24 12:52:35.928 FEAT
< 2016-11-24 12:52:36.006 211-Features:
< 2016-11-24 12:52:36.006  AUTH SSL
< 2016-11-24 12:52:36.006  AUTH TLS
< 2016-11-24 12:52:36.006  EPRT
< 2016-11-24 12:52:36.006  EPSV
< 2016-11-24 12:52:36.068  MDTM
< 2016-11-24 12:52:36.068  PASV
< 2016-11-24 12:52:36.068  PBSZ
< 2016-11-24 12:52:36.068  PROT
< 2016-11-24 12:52:36.068  REST STREAM
< 2016-11-24 12:52:36.068  SIZE
< 2016-11-24 12:52:36.068  TVFS
< 2016-11-24 12:52:36.068  UTF8
< 2016-11-24 12:52:36.068 211 End
> 2016-11-24 12:52:36.068 OPTS UTF8 ON
< 2016-11-24 12:52:36.131 200 Always in UTF8 mode.
> 2016-11-24 12:52:36.131 PBSZ 0
< 2016-11-24 12:52:36.209 200 PBSZ set to 0.
> 2016-11-24 12:52:36.209 PROT P
< 2016-11-24 12:52:36.271 200 PROT now Private.
. 2016-11-24 12:52:36.287 Connected
. 2016-11-24 12:52:36.287 --------------------------------------------------------------------------
. 2016-11-24 12:52:36.287 Using FTP protocol.
. 2016-11-24 12:52:36.287 Doing startup conversation with host.
> 2016-11-24 12:52:36.349 PWD
< 2016-11-24 12:52:36.411 257 "/"
. 2016-11-24 12:52:36.411 Getting current directory name.
. 2016-11-24 12:52:36.599 Retrieving directory listing...
> 2016-11-24 12:52:36.599 TYPE A
< 2016-11-24 12:52:36.661 200 Switching to ASCII mode.
> 2016-11-24 12:52:36.661 PASV
< 2016-11-24 12:52:36.739 227 Entering Passive Mode (46,140,133,66,195,80).
> 2016-11-24 12:52:36.739 LIST -a
. 2016-11-24 12:52:36.739 Connecting to 46.140.133.66:50000 ...
. 2016-11-24 12:52:36.879 Session ID reused
< 2016-11-24 12:52:36.879 150 Here comes the directory listing.
. 2016-11-24 12:52:36.879 Using TLSv1, cipher TLSv1/SSLv3: AES128-SHA, 1024 bit RSA, AES128-SHA              SSLv3 Kx=RSA      Au=RSA  Enc=AES(128)  Mac=SHA1
. 2016-11-24 12:52:36.911 TLS connection established
. 2016-11-24 12:52:37.020 drwxrwsrwx    6 0        1004         4096 Nov 24 08:32 .
. 2016-11-24 12:52:37.020 drwxrwsrwx    6 0        1004         4096 Nov 24 08:32 ..
. 2016-11-24 12:52:37.020 -rw-r--r--    1 1004     1004       162000 Oct 28 15:25 YTF.AR.T700.S.E0089858.D161028.T092339.A001
. 2016-11-24 12:52:37.020 drwxr-sr-x    2 1004     1004         4096 Nov 24 07:29 abc
. 2016-11-24 12:52:37.020 drwxr-sr-x    2 1004     1004         4096 Nov 24 07:14 koray
. 2016-11-24 12:52:37.020 drwxr-sr-x    2 1004     1004         4096 Nov 24 08:32 laylaylom
. 2016-11-24 12:52:37.020 drwxr-sr-x    2 1004     1004         4096 Nov 24 07:15 tugay
< 2016-11-24 12:52:37.160 226 Directory send OK.
. 2016-11-24 12:52:37.160 Directory listing successful
. 2016-11-24 12:52:37.160 Detecting timezone difference...
. 2016-11-24 12:52:37.160 Retrieving file information...
> 2016-11-24 12:52:37.160 PWD
< 2016-11-24 12:52:37.223 257 "/"
> 2016-11-24 12:52:37.223 CWD /YTF.AR.T700.S.E0089858.D161028.T092339.A001
< 2016-11-24 12:52:37.285 550 Failed to change directory.
> 2016-11-24 12:52:37.285 TYPE I
< 2016-11-24 12:52:37.363 200 Switching to Binary mode.
> 2016-11-24 12:52:37.363 SIZE /YTF.AR.T700.S.E0089858.D161028.T092339.A001
< 2016-11-24 12:52:37.425 213 162000
> 2016-11-24 12:52:37.425 MDTM /YTF.AR.T700.S.E0089858.D161028.T092339.A001
< 2016-11-24 12:52:37.488 213 20161028152533
. 2016-11-24 12:52:37.488 Retrieving file information successful
. 2016-11-24 12:52:37.488 Timezone difference of -3 detected using file /YTF.AR.T700.S.E0089858.D161028.T092339.A001 (Listing: 2016-10-28T12:25:00.000Z, UTF: 2016-10-28T15:25:00.000Z)
. 2016-11-24 12:52:37.488 ..;D;0;1899-12-30T02:00:00.000Z;0;"" [0];"" [0];---------;0
. 2016-11-24 12:52:37.488 YTF.AR.T700.S.E0089858.D161028.T092339.A001;-;162000;2016-10-28T15:25:00.000Z;1;"1004" [0];"1004" [0];rw-r--r--;0
. 2016-11-24 12:52:37.488 abc;D;0;2016-11-24T08:29:00.000Z;1;"1004" [0];"1004" [0];rwxr-sr-x;0
. 2016-11-24 12:52:37.488 koray;D;0;2016-11-24T08:14:00.000Z;1;"1004" [0];"1004" [0];rwxr-sr-x;0
. 2016-11-24 12:52:37.488 laylaylom;D;0;2016-11-24T09:32:00.000Z;1;"1004" [0];"1004" [0];rwxr-sr-x;0
. 2016-11-24 12:52:37.488 tugay;D;0;2016-11-24T08:15:00.000Z;1;"1004" [0];"1004" [0];rwxr-sr-x;0
. 2016-11-24 12:52:37.597 Startup conversation with host finished.
. 2016-11-24 13:54:14.249 Dummy directory read to keep session alive.
. 2016-11-24 13:54:14.249 Retrieving directory listing...
> 2016-11-24 13:54:14.249 TYPE A
< 2016-11-24 13:54:14.312 200 Switching to ASCII mode.
> 2016-11-24 13:54:14.312 PASV
< 2016-11-24 13:54:14.390 227 Entering Passive Mode (46,140,133,66,195,81).
> 2016-11-24 13:54:14.390 LIST -a
. 2016-11-24 13:54:14.390 Connecting to 46.140.133.66:50001 ...
< 2016-11-24 13:54:14.514 150 Here comes the directory listing.
. 2016-11-24 13:54:14.514 Session ID reused
. 2016-11-24 13:54:14.530 Using TLSv1, cipher TLSv1/SSLv3: AES128-SHA, 1024 bit RSA, AES128-SHA              SSLv3 Kx=RSA      Au=RSA  Enc=AES(128)  Mac=SHA1
. 2016-11-24 13:54:14.530 TLS connection established
. 2016-11-24 13:54:14.655 drwxrwsrwx    6 0        1004         4096 Nov 24 08:32 .
. 2016-11-24 13:54:14.655 drwxrwsrwx    6 0        1004         4096 Nov 24 08:32 ..
. 2016-11-24 13:54:14.655 -rw-r--r--    1 1004     1004       162000 Oct 28 15:25 YTF.AR.T700.S.E0089858.D161028.T092339.A001
. 2016-11-24 13:54:14.655 drwxr-sr-x    2 1004     1004         4096 Nov 24 07:29 abc
. 2016-11-24 13:54:14.655 drwxr-sr-x    2 1004     1004         4096 Nov 24 07:14 koray
. 2016-11-24 13:54:14.655 drwxr-sr-x    2 1004     1004         4096 Nov 24 08:32 laylaylom
. 2016-11-24 13:54:14.655 drwxr-sr-x    2 1004     1004         4096 Nov 24 07:15 tugay
< 2016-11-24 13:54:14.795 226 Directory send OK.
. 2016-11-24 13:54:14.795 Directory listing successful

1 个答案:

答案 0 :(得分:1)

FTP.list method太低级了。它甚至没有打开数据连接。它只发送LIST命令。你不能单独使用它。正如它的文件说:

  

将FTP LIST命令发送到服务器的便捷方法,   收到回复,并返回回复代码。 记住,它已经结束了   给你管理数据连接。如果你不需要这么低   访问级别,使用FTPClient,它将为您处理所有低级别的详细信息。

相反,使用一些高级方法,如:

通过调用FTPClient.enterLocalPassiveMode()来保持第一种方法。

只需将PASV命令发送到服务器,就无法切换到被动模式。您还必须切换客户端。否则,客户端和服务器将无法相互通信。

enterLocalPassiveMode将客户端和服务器切换到被动模式。

在使用FTPS时,您也需要启用加密数据传输。请注意,默认情况下,Apache Common Net库不会加密数据传输。

ftp.execPBSZ(0);
ftp.execPROT("P").

实际上,如果控制连接已加密,某些服务器(尤其是您的服务器)甚至会要求您加密数据传输。