[drain pit] Ftp normally connects to the server but cannot get the file

Some codes in ftp tool class ftp util are as follows: import org.apache.commons.net.ftp.*; public class FtpUtil { private static Logger log = Logger.g...

Some codes in ftp tool class ftp util are as follows:

import org.apache.commons.net.ftp.*; public class FtpUtil { private static Logger log = Logger.getLogger(FtpUtil.class); private FTPClient ftpClient; private FtpUtil(){} /** * @description Create ftp connection instance */ public static FtpUtil getInstance(String serverUrl, String port, String userName, String password) throws Exception{ FtpUtil ftpUtil = new FtpUtil(); ftpUtil.connectServer(serverUrl, port, userName, password); return ftpUtil; } /** * @description Create connection */ public void connectServer(String serverUrl, String port, String userName, String password) throws Exception { ftpClient = new FTPClient(); log.debug("connection to ftp The server:" + serverUrl); ftpClient.connect(serverUrl, Integer.parseInt(port)); int reply = ftpClient.getReplyCode(); log.debug("The response string is:" + ftpClient.getReplyString()); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); log.info("Connect ftp Server failed"); throw new Exception("Connect ftp Server failed"); } else { ftpClient.login(userName, password); log.debug("The current working directory is:" + ftpClient.printWorkingDirectory()); log.debug("Sign in ftp Server succeeded"); } } /** * Judge whether the file exists */ public Boolean isExistFileName(String pathFileName) throws ParseException { Boolean isExist=false; // Get all file names in the specified directory FTPFile[] ftpFiles = null; try { ftpFiles = ftpClient.listFiles(pathFileName); } catch (IOException e) { log.info("Get the specified directory:"+pathFileName+"Exception in specified file list"); e.printStackTrace(); } if(ftpFiles!=null&&ftpFiles.length>0){ isExist=true; }else{ isExist=false; } return isExist; } }
  • When the Service calls the ftutil.getinstance method, it can successfully connect to the ftp server without any abnormal errors.
  • The Service calls the ftutil.isexistfilename method, and prompts to specify that there is no file with the specified file name, and there is no exception error. (after verification, the file exists and the directory and file name are correct.)

[solution]: before the program calls the login method, that is, before executing "FTPClient ftpClient = null; - ftpClient.login(username, password)", add a line of code: ftpClient.enterLocalPassiveMode(); this is OK.

[analysis]:

  1. In FTP service, when it comes to the connection between the client and the server, the connection will involve the opening of the port;
  2. The opening of the port involves active mode and passive mode. Active mode: the client opens the port to the server; passive mode: the server opens the port to the client. Because many clients are in the firewall, it is difficult to open ports to the server. So there are more passive modes.
  3. If FTP connection problem occurs when intranet, LAN and other environments are involved, it can also be considered whether it is caused by connection mode.

3 December 2019, 23:42 | Views: 7412

Add new comment

For adding a comment, please log in
or create account

0 comments