The Java connection to FTP is successful, but the upload fails, and an error is reported: Connected time out

The Java code succeeds when uploading files to the FTP server locally, but when deploying to the test server, the connection to FTP is successful but the upload fails, and a Connected time out error is reported;

The test server and FTP service are both on Alibaba Cloud; (there are errors in using intranet and extranet connections)

reason:

  The reason for finding information on the Internet may be that the firewall policy blocks the port connection of the java test service, and the FTP server may use passive mode;

  Notice:

1. PORT (active mode) and PASV (passive mode) of FTP

    (1) PORT (active mode)

    PORT is called active mode in Chinese. The working principle is as follows: The FTP client connects to the 21 port of the FTP server, sends the user name and password to log in. After successful login, when the client wants to list the list or read the data, the client randomly opens a port (above 1024) , send the PORT command to the FTP server to tell the server that the client uses the active mode and open the port; after the FTP server receives the PORT active mode command and the port number, it connects the server's port 20 with the client's open port to send data.

    (2) PASV (passive mode)

    PASV is the abbreviation of Passive, Chinese becomes passive mode, working principle: The FTP client connects to the 21 port of the FTP server, sends the user name and password to log in, after successful login, when you want to list the list or read the data, send the PASV command to the FTP server, The server randomly opens a port (above 1024) locally, and then informs the client of the open port, and the client connects to the open port of the server for data transmission.

 For active and passive modes, please refer to: https://www.cnblogs.com/ajianbeyourself/p/7655464.html

and https://www.cnblogs.com/yeyublog/p/6056424.html

That is to say, the data port opened by the server is restricted by the firewall and cannot be connected, so a range of port numbers must be specified in the configuration of the ftp server as data ports, and then these ports can be set in the firewall to be accessible;

Setting method: increase in the ftp configuration file

pasv_min_port=40001
pasv_max_port=41000 
is added in the firewall configuration:
-A INPUT -p tcp --dport 40001:41000 -j ACCEPT
-A OUTPUT -p tcp --dport 40001:41000 -j ACCEPT

configuration information can be found at: ttps://www. cnblogs.com/ahaii/p/5390616.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326832158&siteId=291194637