| | |
| | | // @Scheduled(cron = "*/30 * * * * ?") |
| | | public boolean readFile() { |
| | | FTPClient ftp = new FTPClient(); |
| | | System.out.println("ftp 创建客户端"); |
| | | try { |
| | | //设置传输超时时间为60秒 |
| | | ftp.setDataTimeout(10000); |
| | |
| | | else { |
| | | for (FTPFile file : files) { |
| | | String fileName = file.getName(); |
| | | InputStream is = ftp.retrieveFileStream(new String(fileName.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING)); |
| | | if (null == is) { |
| | | ftp.logout(); |
| | | return false; |
| | | } else { |
| | | String substring1 = fileName.substring(0, 2); |
| | | if (substring1.equals("nq")) { |
| | | //把文件下载到本地 |
| | | FtpUtil.downloadFtpFile(ftpHost, ftpUserNameIn, ftpPasswordIn, ftpPort, ftpPath, localPath, fileName); |
| | | // |
| | | String s = OutJson.TestJson(fileName); |
| | | //sql语句 |
| | | String sql = OutJson.stringReplace(s); |
| | | String[] split = sql.split(";");//以逗号分割 |
| | | for (String sqls : split) { |
| | | //判断是否是新增,删除,修改 |
| | | String substring = sqls.substring(0, 2); |
| | | //新增 |
| | | if (substring.equals("in")) { |
| | | //运行sql语句 |
| | | MysqlCenlint.inster(sqls); |
| | | } |
| | | //修改 |
| | | else if (substring.equals("up")) { |
| | | MysqlCenlint.update(sqls); |
| | | } |
| | | //删除 |
| | | else { |
| | | MysqlCenlint.delete(sqls); |
| | | } |
| | | System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":读取到文件 = " + fileName); |
| | | if (fileName.contains("nq")) { |
| | | // InputStream is = ftp.retrieveFileStream(new String(fileName.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING)); |
| | | //把文件下载到本地 |
| | | long l = System.currentTimeMillis(); |
| | | FtpUtil.downloadFtpFile(ftpHost, ftpUserNameIn, ftpPasswordIn, ftpPort, ftpPath, localPath, fileName); |
| | | long x = System.currentTimeMillis(); |
| | | System.out.println("下载文件耗时 = " + (x-l)+" 毫秒"); |
| | | // |
| | | String s = OutJson.TestJson(fileName); |
| | | //sql语句 |
| | | String sql = OutJson.stringReplace(s); |
| | | //以逗号分割 |
| | | String[] split = sql.split(";"); |
| | | for (String sqls : split) { |
| | | //判断是否是新增,删除,修改 |
| | | String substring = sqls.substring(0, 2); |
| | | //新增 |
| | | if (substring.equals("in")) { |
| | | //运行sql语句 |
| | | MysqlCenlint.inster(sqls); |
| | | } |
| | | //删除本地服务器文件 |
| | | MysqlCenlint.deletess(fileName); |
| | | //删除 ftp 服务器文件 |
| | | FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserNameIn, ftpPasswordIn, ftpPath, fileName); |
| | | //修改 |
| | | else if (substring.equals("up")) { |
| | | MysqlCenlint.update(sqls); |
| | | } |
| | | //删除 |
| | | else { |
| | | MysqlCenlint.delete(sqls); |
| | | } |
| | | } |
| | | //关闭流 |
| | | is.close(); |
| | | ftp.completePendingCommand(); |
| | | ftp.logout(); |
| | | //删除本地服务器文件 |
| | | MysqlCenlint.deletess(fileName); |
| | | //删除 ftp 服务器文件 |
| | | // ftp.dele(fileName); |
| | | FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserNameIn, ftpPasswordIn, ftpPath, fileName); |
| | | System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":成功删除ftp文件 = " + fileName); |
| | | } |
| | | return true; |
| | | } |
| | | //关闭流 |
| | | ftp.completePendingCommand(); |
| | | ftp.logout(); |
| | | } |
| | | } catch (Exception e) { |
| | | System.out.println("ftp连接失败"); |