| | |
| | | private static Logger logger = LoggerFactory.getLogger(FtpUtils.class); |
| | | |
| | | private static final String DEFAULT_CHARSET = "UTF-8"; |
| | | private static final int DEFAULT_TIMEOUT = 60 * 1000; |
| | | private static final int DEFAULT_TIMEOUT = 6000 * 1000; |
| | | private static final String DAILY_FILE_PATH = "get"; |
| | | static FTPClient ftpClient; |
| | | private volatile String ftpBasePath; |
| | |
| | | logger.info("username---"+username+"--host---"+host+"---port---"+port+"-----password-----"+password); |
| | | boolean flag = false; |
| | | try { |
| | | ftpClient = new FTPClient(); |
| | | // 连接FTP服务器 |
| | | ftpClient.connect(host, port); |
| | | // 登陆FTP服务器 |
| | | boolean login = ftpClient.login(username, password); |
| | | // 中文支持 |
| | | ftpClient.setControlEncoding(DEFAULT_CHARSET); |
| | | // 设置文件类型为二进制(如果从FTP下载或上传的文件是压缩文件的时候,不进行该设置可能会导致获取的压缩文件解压失败) |
| | | ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); |
| | | ftpClient.enterLocalPassiveMode(); |
| | | if (ftpClient!=null && !FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { |
| | | ftpClient = new FTPClient(); |
| | | // 连接FTP服务器 |
| | | ftpClient.connect(host, port); |
| | | // 登陆FTP服务器 |
| | | boolean login = ftpClient.login(username, password); |
| | | // 中文支持 |
| | | ftpClient.setControlEncoding(DEFAULT_CHARSET); |
| | | // 设置文件类型为二进制(如果从FTP下载或上传的文件是压缩文件的时候,不进行该设置可能会导致获取的压缩文件解压失败) |
| | | ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); |
| | | ftpClient.enterLocalPassiveMode(); |
| | | |
| | | // if (!ftpClient.login(username, password)) { |
| | | // disconnect(); |
| | | // throw new IOException("不能连接到该服务 :" + host); |
| | | // } |
| | | if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { |
| | | logger.error("连接FTP失败,用户名或密码错误。"); |
| | | disconnect(); |
| | | if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { |
| | | logger.error("连接FTP失败,用户名或密码错误。"); |
| | | disconnect(); |
| | | } else { |
| | | logger.info("FTP连接成功!"); |
| | | flag = true; |
| | | } |
| | | } else { |
| | | logger.info("FTP连接成功!"); |
| | | logger.info("当前FTP处于连接状态,无线重复连接!"); |
| | | flag = true; |
| | | } |
| | | } catch (Exception e) { |
| | |
| | | logger.info("FTP文件名修改失败---------->>>>>文件名:"+split2[0]); |
| | | } |
| | | input.close(); |
| | | disconnect(); |
| | | //disconnect(); |
| | | return storeFlag; |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | logger.error("FTP文件上传失败!", e); |
| | | } finally { |
| | | disconnect(); |
| | | //disconnect(); |
| | | } |
| | | return false; |
| | | } |