

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
 
//http://www.codejava.net/java-se/networking/ftp/java-ftp-list-files-and-directories-example

public class FTPUploadFile 
{
 
    public static void main(String[] args)
    {
        String server = "ftp.cluster011.ovh.net";
        int port = 21;
        String user = "analogde";
        String pass = "rainbow";
 
        FTPClient ftpClient = new FTPClient();

        try 
        {
 		            ftpClient.connect(server, port);
		            ftpClient.login(user, pass);
		            ftpClient.enterLocalPassiveMode();
		            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

		        	String location = "D:/raspberry.jpg";
		            File firstLocalFile = new File(location);
		            String racine = "/www/ALARME/";
		            String file = firstLocalFile.getName();
		            
		            boolean flag_existe = false;
		            // le fichier existe t-il ?	
		            FTPFile[] files1 = ftpClient.listFiles(racine);
		            for (FTPFile aFile: files1) 
	                {
	                    if(aFile.getName().equals(file) )
	                    {
	                    	flag_existe = true;
	                    }
	                }
		            
		            // oui
		            if(flag_existe == true)
			        {
                    	System.out.println("Le fichier " + file + " existe sur le serveur: renomer");
				        String[] parts = file.split("\\.");
				        DateFormat FILE_DATE_FORMAT = new SimpleDateFormat("_dd_MM_yyyy_HH_mm_ss"); 
				     	Date now = new Date(); // by default, represents the current time
				     	String fileName = parts[0] + FILE_DATE_FORMAT.format(now) + "." + parts[1];
				     	System.out.println("Nouveau nom: " + fileName);
				        
				     	File f1 = new File(location);
				     	InputStream	in = new FileInputStream(f1);
				     	boolean done = ftpClient.storeFile(racine + fileName,in);
				     	in.close();
				     	if (done) 
			            {
			                System.out.println(fileName +" is uploaded successfully.");
			            }
			        }
		            // non
			        else
			        {
			        	System.out.println("Nouveau sur le serveur");
			        	File f1 = new File(location);
				     	InputStream	in = new FileInputStream(f1);
				     	boolean done = ftpClient.storeFile(racine + file,in);
			            in.close();
			            if (done) 
			            {
			            	  System.out.println(file +" is uploaded successfully.");
			            }
			            
			        }	
		            
// demande au serveur le renvoie de la date du fichier    
		          //String filePath = racine + file;
			      //String time = ftpClient.getModificationTime(filePath);
			      //System.out.println("Server Reply: " + time);
		            
// renomer un fichier qui existe sur le serveur 
			      //  boolean success = ftpClient.rename(racine + file, racine + fileName);
		          //  if (success) 
		          //  {
		          //  	 System.out.println("Rename successfully.");
		          //  } else
		          //  {
		          //  	 System.out.println("Pb rename.");
		          //  }*/  
		            
		            
		            
/*		            
		            InputStream inputStream = ftpClient.retrieveFileStream("/www/ALARME/" + "rename.jpg");
			        int returnCode = ftpClient.getReplyCode();
			        if (inputStream == null || returnCode == 550)
			        {
			        	System.out.println("Pas sur le serveur");
			        	flag = false;
			        }
			        else
			        {
			        	System.out.println("Existe sur le serveur: renomer");
			        	flag = true;
			        	
			        }	
/*
		            String firstRemoteFile = racine + "pipo.jpg";
		            InputStream inputStream = new FileInputStream(firstLocalFile);
		 
		            System.out.println("Start uploading first file");
		            boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
		            inputStream.close();
		            if (done) {
		                System.out.println("The first file is uploaded successfully.");
		            }
*/		 
			        
/*			    	System.out.println(flag);

		            String filePath = racine + "rename.jpg";
		            String time = ftpClient.getModificationTime(filePath);
		            System.out.println("Server Reply: " + time);
*/		            
		            
		          
		            
//		            SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy HH:mm");
//		            String file = racine + "rename.jpg";
//		            String formattedDate = formatDate.format(file.getTimestamp().getTime());
//		            System.out.println(file.getName() + "     " + file.getSize() + "     " + formattedDate);
		            
		            
/*		            
		            boolean success = ftpClient.rename(racine + "pipo.jpg", racine + "rename.jpg");
		            
		            if (success) 
		            {
		            	 System.out.println("Rename successfully.");
		            } else
		            {
		            	 System.out.println("Pb rename.");
		            }
*/		            
		            
		            // APPROACH #2: uploads second file using an OutputStream
/*
		            File secondLocalFile = new File("E:/Test/Report.doc");
		            String secondRemoteFile = "test/Report.doc";
		            inputStream = new FileInputStream(secondLocalFile);
		 
		            System.out.println("Start uploading second file");
		            OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
		            byte[] bytesIn = new byte[4096];
		            int read = 0;
		 
		            while ((read = inputStream.read(bytesIn)) != -1) {
		                outputStream.write(bytesIn, 0, read);
		            }
		            inputStream.close();
		            outputStream.close();
		 
		            boolean completed = ftpClient.completePendingCommand();
		            if (completed)
		            {
		            	System.out.println("The second file is uploaded successfully.");
		            }
*/		            
 
        } 
        
        catch (IOException ex)
        {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } 
        
        finally
        {
	            try 
	            {
	                if (ftpClient.isConnected()) 
	                {
	                    ftpClient.logout();
	                    System.out.println("Deconnexion");
	                    ftpClient.disconnect();
	                }
	            } 
	            catch (IOException ex)
	            {
	                ex.printStackTrace();
	            }
        }
        
        
    }
    
   
 
}