| | |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | |
| | | import static org.springblade.common.config.FtpConfig.*; |
| | | |
| | |
| | | @Component |
| | | public class Monitor { |
| | | |
| | | @Scheduled(cron = "*/1 * * * * ?") |
| | | // @Scheduled(cron = "*/5 * * * * ?") |
| | | public static boolean isFTPFileExist() { |
| | | |
| | | FTPClient ftp = new FTPClient(); |
| | | String fileName = null; |
| | | try { |
| | | |
| | | ftp.connect(ftpHost, ftpPort); |
| | | |
| | | // 登陆 |
| | |
| | | return false; |
| | | } |
| | | |
| | | System.out.println("ftp 连接成功!"); |
| | | System.out.println("ftp 连接成功!时间: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); |
| | | |
| | | //被动模式,文件上传不成功会有提示 |
| | | ftp.enterLocalActiveMode(); |
| | |
| | | ftp.changeWorkingDirectory(ftpPath); |
| | | FTPFile[] files = ftp.listFiles(); |
| | | if (files.length == 0) { |
| | | return false; |
| | | ftp.disconnect(); |
| | | } else { |
| | | for (FTPFile file : files) { |
| | | fileName = file.getName(); |
| | | fileName = file.getName(); |
| | | InputStream is = ftp.retrieveFileStream(new String(fileName.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING)); |
| | | if (null == is) { |
| | | return false; |
| | | ftp.disconnect(); |
| | | } else { |
| | | String substring1 = fileName.substring(0, 1); |
| | | //把文件下载到本地 |
| | |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | MysqlCenlint.deletess(fileName); |
| | | FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, "anbao/", fileName); |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (ftp != null) { |
| | | //如果ftp 没有关闭,则关闭ftp |
| | | if (ftp.isConnected()) { |
| | | try { |
| | | ftp.disconnect(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException io) { |
| | | io.printStackTrace(); |
| | | } |
| | | } |
| | | } |