Guest User

Untitled

a guest
Mar 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import org.apache.commons.net.ftp.FTP;
  8. import org.apache.commons.net.ftp.FTPClient;
  9. import org.apache.commons.net.ftp.FTPFile;
  10. import groovy.lang.*;
  11. import groovy.util.*;
  12.  
  13. public class FTPUploadFileComparision {
  14.  
  15. static String tmpDate = ""; //variable for store the last modified date
  16.  
  17. public static void main(String[] args) {
  18. String server = "172.17.5.105"; //server ip
  19. int port = 21; //default port
  20. String user = "FTPUser"; //username
  21. String pass = "Santosh@123"; //password
  22.  
  23. FTPClient ftpClient = new FTPClient();
  24. try {
  25. ftpClient.connect(server, port);
  26. ftpClient.login(user, pass);
  27. ftpClient.enterLocalPassiveMode();
  28. FTPFile[] files1 = ftpClient.listFiles("/");
  29. DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  30. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  31. for (FTPFile file : files1) {
  32. String details = file.getName();
  33. if (file.isDirectory()) {
  34. details = "[" + details + "]";
  35. }
  36. details += "tt" + file.getSize();
  37. details += "tt" + dateFormater.format(file.getTimestamp().getTime());
  38. compare(dateFormater.format(file.getTimestamp().getTime()), ftpClient);
  39. }
  40. } catch (IOException ex) {
  41. System.out.println("Error: " + ex.getMessage());
  42. ex.printStackTrace();
  43. } finally {
  44. try {
  45. if (ftpClient.isConnected()) {
  46. ftpClient.logout();
  47. ftpClient.disconnect();
  48. }
  49. } catch (IOException ex) {
  50. ex.printStackTrace();
  51. }
  52. }
  53. }
  54. //method for compare the local file with ftp server file based on the last modified date
  55.  
  56. public static void compare(String val, FTPClient ftpClient) throws IOException {
  57. String status = null;
  58. File f = new File("D:\Autoplus.txt"); //local file path
  59. Long modifiedTime1 = f.lastModified();
  60. DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  61.  
  62. String str = dateFormater.format(modifiedTime1);
  63. if (str.compareTo(val) > 0) { //local file last modified date is grater than ftp server file
  64. //lastmodified date then delte the old file and place the new file//
  65. tmpDate = val;
  66. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  67.  
  68. // uploads first file using an InputStream stream into ftp server if local file last modified date is grater than ftp server file
  69. // File firstLocalFile = new File(f);
  70. InputStream inputStream = new FileInputStream(f);
  71. String firstRemoteFile = "Autoplus.txt";
  72. boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
  73. inputStream.close();
  74. if (done) {
  75. System.out.println("The file is uploaded successfully.");
  76. } else {
  77. System.out.println("failed to upload the file");
  78. }
  79.  
  80. //delete the file in ftp server if local file last modified date is grater then ftp server file
  81. // Set a string with the file you want to delete
  82. FTPFile[] files1 = ftpClient.listFiles("/FTP");
  83. //ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  84. for (FTPFile file : files1) {
  85. String details = file.getName();
  86.  
  87. String filename = "FTP//" + details;
  88.  
  89. boolean exist = ftpClient.deleteFile(filename);
  90.  
  91. if (exist) {
  92. System.out.println("File '" + filename + "' deleted...");
  93. } // Notify user that file doesn't exist
  94. else {
  95. System.out.println("File '" + filename + "' doesn't exist...");
  96. }
  97. }
  98.  
  99. } else if (str.compareTo(val) < 0) {
  100. tmpDate = str;
  101. System.out.println(tmpDate);
  102.  
  103. } else {
  104. status = "ok";
  105. System.out.println("modified at same time");
  106. }
  107.  
  108. }
  109. }
Add Comment
Please, Sign In to add comment