Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import groovy.lang.*;
- import groovy.util.*;
- public class FTPUploadFileComparision {
- static String tmpDate = ""; //variable for store the last modified date
- public static void main(String[] args) {
- String server = "172.17.5.105"; //server ip
- int port = 21; //default port
- String user = "FTPUser"; //username
- String pass = "Santosh@123"; //password
- FTPClient ftpClient = new FTPClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- FTPFile[] files1 = ftpClient.listFiles("/");
- DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- for (FTPFile file : files1) {
- String details = file.getName();
- if (file.isDirectory()) {
- details = "[" + details + "]";
- }
- details += "tt" + file.getSize();
- details += "tt" + dateFormater.format(file.getTimestamp().getTime());
- compare(dateFormater.format(file.getTimestamp().getTime()), ftpClient);
- }
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- ex.printStackTrace();
- } finally {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- //method for compare the local file with ftp server file based on the last modified date
- public static void compare(String val, FTPClient ftpClient) throws IOException {
- String status = null;
- File f = new File("D:\Autoplus.txt"); //local file path
- Long modifiedTime1 = f.lastModified();
- DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str = dateFormater.format(modifiedTime1);
- if (str.compareTo(val) > 0) { //local file last modified date is grater than ftp server file
- //lastmodified date then delte the old file and place the new file//
- tmpDate = val;
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- // uploads first file using an InputStream stream into ftp server if local file last modified date is grater than ftp server file
- // File firstLocalFile = new File(f);
- InputStream inputStream = new FileInputStream(f);
- String firstRemoteFile = "Autoplus.txt";
- boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
- inputStream.close();
- if (done) {
- System.out.println("The file is uploaded successfully.");
- } else {
- System.out.println("failed to upload the file");
- }
- //delete the file in ftp server if local file last modified date is grater then ftp server file
- // Set a string with the file you want to delete
- FTPFile[] files1 = ftpClient.listFiles("/FTP");
- //ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- for (FTPFile file : files1) {
- String details = file.getName();
- String filename = "FTP//" + details;
- boolean exist = ftpClient.deleteFile(filename);
- if (exist) {
- System.out.println("File '" + filename + "' deleted...");
- } // Notify user that file doesn't exist
- else {
- System.out.println("File '" + filename + "' doesn't exist...");
- }
- }
- } else if (str.compareTo(val) < 0) {
- tmpDate = str;
- System.out.println(tmpDate);
- } else {
- status = "ok";
- System.out.println("modified at same time");
- }
- }
- }
Add Comment
Please, Sign In to add comment