Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. package ftp;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.text.DateFormat;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.List;
  14. import javax.sql.DataSource;
  15. import org.apache.commons.net.ftp.FTPClient;
  16. import org.apache.commons.net.ftp.FTPReply;
  17. import org.springframework.jdbc.datasource.SimpleDriverDataSource;
  18.  
  19. import com.mysql.jdbc.Driver;
  20. import com.mysql.jdbc.Statement;
  21.  
  22. public class ftpToDB {
  23. private static void showServerReply(FTPClient ftpClient) {
  24. String[] replies = ftpClient.getReplyStrings();
  25. if (replies != null && replies.length > 0) {
  26. for (String aReply : replies) {
  27. System.out.println("SERVER: " + aReply);
  28. }
  29. }
  30. }
  31.  
  32. public static void main(String[] args) {
  33. String server = "ftp.inerps.eu";
  34. int port = 21;
  35. String user = "burgas@inerps.eu";
  36. String pass = "i8w4sv9da0j2d7w0";
  37. FTPClient ftpClient = new FTPClient();
  38. try {
  39. ftpClient.connect(server, port);
  40. showServerReply(ftpClient);
  41. int replyCode = ftpClient.getReplyCode();
  42. if (!FTPReply.isPositiveCompletion(replyCode)) {
  43. System.out.println("Operation failed. Server reply code: " + replyCode);
  44. return;
  45. }
  46. ftpClient.login(user, pass);
  47. // showServerReply(ftpClient);
  48. } catch (IOException ex) {
  49. System.out.println("Oops! Something wrong happened");
  50. ex.printStackTrace();
  51. }
  52. String[] names = null;
  53. List<String> names1 = new ArrayList();
  54. try {
  55. names = ftpClient.listNames();
  56. } catch (IOException e2) {
  57. e2.printStackTrace();
  58. }
  59. for (int i = 0; i < names.length; i++) {
  60. names1.add(names[i]);
  61. }
  62. for (int i = 0; i < names1.size(); i++) {
  63. if (names1.get(i).startsWith(".")) {
  64. names1.remove(i);
  65. i--;
  66. }
  67. }
  68. Connection conn = null;
  69. DataSource ds = null;
  70. ResultSet resultSet = null;
  71. Statement statement = null;
  72. String preLast = null;
  73.  
  74. try {
  75.  
  76. Driver driver = new com.mysql.jdbc.Driver();
  77. String url = "jdbc:mysql://localhost:5555/camera";
  78. String username = "root";
  79. String password = "603400";
  80. ds = new SimpleDriverDataSource(driver, url, username, password);
  81. conn = ds.getConnection();
  82. statement = (Statement) conn.createStatement();
  83. resultSet = statement.executeQuery("SELECT * FROM camera ORDER BY id DESC LIMIT 1");
  84. while (resultSet.next()) {
  85. preLast = resultSet.getString("cameraTime");
  86. }
  87. }catch (SQLException e1) {
  88. e1.printStackTrace();
  89. }
  90. String last = preLast.substring(0, 8);
  91. for (String string : names1) {
  92. DateFormat df = new SimpleDateFormat("yyyyMMdd");
  93. Date lastDate;
  94. try {
  95. lastDate = df.parse(last);
  96. if(lastDate.after(df.parse(string))) {
  97. names1.remove(string);
  98. }
  99. } catch (ParseException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103.  
  104.  
  105. }
  106. String[] times = null;
  107. List<String> times1 = new ArrayList();
  108. List<String> pattern1 = new ArrayList();
  109. String pattern = "/";
  110. for (String string : names1) {
  111. try {
  112. times = ftpClient.listNames("/" + string);
  113. } catch (IOException e) {
  114. // TODO Auto-generated catch block
  115. e.printStackTrace();
  116. }
  117. pattern = pattern.concat(string + "/");
  118. for (int i = 0; i < times.length; i++) {
  119. try {
  120. if (Character.isDigit(times[i].charAt(0))) {
  121. String numb = string + times[i].substring(0, 6);
  122. times1.add(numb);
  123. }
  124. } catch (Exception e) {
  125. // TODO: handle exception
  126. }
  127. }
  128. for (String string1 : times) {
  129. if (Character.isDigit(string1.charAt(0))) {
  130. String path1 = pattern;
  131. path1 = path1.concat(string1);
  132. pattern1.add(path1);
  133. }
  134.  
  135. }
  136. try {
  137. for (int i = 0; i < times1.size(); i++) {
  138. statement = (Statement) conn.createStatement();
  139. String INSERT_SQL = "INSERT INTO camera (cameraTime,pattern)" + "SELECT * FROM (SELECT ?,?) as tmp"
  140. + " WHERE NOT EXISTS (\r\n" + " SELECT pattern FROM camera WHERE pattern = ?"
  141. + ") LIMIT 1;";
  142.  
  143. PreparedStatement ps = conn.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
  144. ps.setString(1, times1.get(i));
  145. ps.setString(2, pattern1.get(i));
  146. ps.setString(3, pattern1.get(i));
  147. ps.executeUpdate();
  148. ps.close();
  149. }
  150. } catch (
  151.  
  152. SQLException e) {
  153. // TODO Auto-generated catch block
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement