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

//import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.SocketException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
 
 
//http://www.java2s.com/Code/Java/Network-Protocol/UsetheFTPClient.htm
 
public class FTPUploadFileDemo 
{
 
    public static void main(String[] args) 
    {
	        String server = "ftp.cluster011.ovh.net";
	        int port = 21;
	        String user = "analogde";
	        String pass = "rainbow";
	        String location = "D:/saved.jpg";
	        InputStream in = null;
	        
	        FTPClient ftpClient = new FTPClient();
	        try 
	        {
	            ftpClient.connect(server, port);
	            ftpClient.login(user, pass);
	            ftpClient.enterLocalPassiveMode();
	            ftpClient.changeWorkingDirectory("/www/ALARME");
	   	     	int reply = ftpClient.getReplyCode();
	   	     	System.out.println("Received Reply from FTP Connection:" + reply);
	   	     	if(FTPReply.isPositiveCompletion(reply))
	   	     	{
	   	     		System.out.println("Connected Success");
	   	     	}
	   	     	
	   	     	
//	            ftpClient.enterLocalPassiveMode();
//	            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

	   	     	
	   	     	
	        	DateFormat FILE_DATE_FORMAT = new SimpleDateFormat("_dd_MM_yyyy_HH_mm_ss"); 
		     	Date now = new Date(); // by default, represents the current time
		     	String fileName = "saved" + FILE_DATE_FORMAT.format(now) + ".jpg";
		     	System.out.println(fileName);
		        File f1 = new File(location);
		     	in = new FileInputStream(f1);
		     	ftpClient.storeFile("fileName.jpg",in);
	   	     	
	   	     	InputStream inputStream = ftpClient.retrieveFileStream("/www/ALARME/" + "saved.jpg");
		        int returnCode = ftpClient.getReplyCode();
		        if (inputStream == null || returnCode == 550)
		        {
		        	System.out.println("Pas sur le serveur");
		        }
		        else
		        {
		        	System.out.println("Present sur le serveur");
		        	FILE_DATE_FORMAT = new SimpleDateFormat("_dd_MM_yyyy_HH_mm_ss"); 
			     	now = new Date(); // by default, represents the current time
			     	fileName = "saved" + FILE_DATE_FORMAT.format(now) + ".jpg";
			     	System.out.println(fileName);
			        f1 = new File(location);
			     	in = new FileInputStream(f1);
			     	ftpClient.storeFile("filedggghgdhg",in);
		        }
	   	     	
	        
	   	     	if (ftpClient.isConnected())
	   	     	{
	   	     		ftpClient.logout();
	   	     		ftpClient.disconnect();
	   	     	}
	        }
	        catch (IOException ex)
	        {
	            ex.printStackTrace();
	        }
	        
	        
	        
	        /*
	        FTP ftpApp = new FTP();
	        
	        try 
	        {
	            String hostname = "ftp.cluster011.ovh.net";
	            port = 21;
	            String username = "analogde";
	            String password = "rainbow";
	            ftpApp.connect(hostname, port, username, password);
	            
	            
	            String filePath = "/www/ALARME/Music.mp4";
	            Boolean exist = ftpApp.checkFileExists(filePath);
	            System.out.println("Is file " + filePath + " exists? " + exist);
	            
	            filePath = "/www/ALARME/zorro.jpg";
	            exist = ftpApp.checkFileExists(filePath);
	            System.out.println("Is file " + filePath + " exists? " + exist);
	            
	        }
	        catch (IOException ex)
	        {
	            ex.printStackTrace();
	        }
	        
	        finally
	        {
	            		try 
	            		{
	            			ftpApp.logout();
	            		} 
	            		catch (IOException ex)
	            		{
	            			ex.printStackTrace();
	            		}
	        }
	        */
	        
    }  
}    
        
 /*
        FTPClient ftpClient = new FTPClient();
        try {
 
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            
            ftpClient.changeWorkingDirectory("/www/ALARME");
            
   	     	int reply = ftpClient.getReplyCode();
   	     	System.out.println("Received Reply from FTP Connection:" + reply);
    
   	     	if(FTPReply.isPositiveCompletion(reply))
   	     	{
   	     		System.out.println("Connected Success");
   	     	}
   	     	
   	     	
   	 
            ftpClient.logout();
            ftpClient.disconnect();
   	 
     		DateFormat FILE_DATE_FORMAT = new SimpleDateFormat("_dd_MM_yyyy_HH_mm_ss"); 
	     	Date now = new Date(); // by default, represents the current time
	     	String fileName = "saved" + FILE_DATE_FORMAT.format(now) + ".jpg";
	     	System.out.println(fileName);
	        File f1 = new File(location);
	     	in = new FileInputStream(f1);
	     	ftpClient.storeFile(fileName,in);
     	
   	     	
   	     	
   	     	// Fichier existe sur le serveur FTP ?
   	     	InputStream inputStream = ftpClient.retrieveFileStream("/www/ALARME/" + "saved.jpg");
	        int returnCode = ftpClient.getReplyCode();
	        if (inputStream == null || returnCode == 550)
	        {
	        	System.out.println("N'existe pas sur le serveur");
	       
	        }
	        
	        else
	        {
	        	System.out.println("Nouveau");

	        }
	        
	        /*
	        if(locked == 1)
	        {	
	        	DateFormat FILE_DATE_FORMAT = new SimpleDateFormat("_dd_MM_yyyy_HH_mm_ss"); 
		     	Date now = new Date(); // by default, represents the current time
		     	String fileName = "saved" + FILE_DATE_FORMAT.format(now) + ".jpg";
		     	System.out.println("Nouveau nom " + fileName);
		        File f1 = new File(location);
		     	in = new FileInputStream(f1);
		     	ftpClient.storeFile(fileName,in);
	        	
	        
	        }
   	     	
   	  */
   	     	
/*   	   
   	        InputStream inputStream = ftpClient.retrieveFileStream("/www/ALARME/aaaaimage.jpg");
   	        int returnCode = ftpClient.getReplyCode();
   	        if (inputStream == null || returnCode == 550)
   	        {
   	        	System.out.println("False");
   	        }
   	        else
   	        {	
   	        	System.out.println("Exist");
   	        }
*/            
            //String remoteDirPath = "/Upload";
            //String localDirPath = "E:/Test/Download/FTP/Test";
 
            
         
 
            //ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
 
            // APPROACH #1: uploads first file using an InputStream
            //File firstLocalFile = new File("D:/Test/Projects.zip");
 
            //String firstRemoteFile = "Projects.zip";
            //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.");
            //}
 
            // 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();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }*/
 



class FTP
{
    private FTPClient ftpClient;
    private int returnCode;
 
    void connect(String hostname, int port, String username, String password) throws SocketException, IOException 
    {
        ftpClient = new FTPClient();
        ftpClient.connect(hostname, port);
        returnCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(returnCode)) 
        {
            throw new IOException("Could not connect");
        }
        
        boolean loggedIn = ftpClient.login(username, password);
        if (!loggedIn) 
        {
            throw new IOException("Could not login");
        }
        
        System.out.println("Connected and logged in.");
    }
    
    
    boolean checkFileExists(String filePath) throws IOException 
    {
    	   ftpClient.changeWorkingDirectory("/www/ALARME");
    	
        InputStream inputStream = ftpClient.retrieveFileStream(filePath);
        returnCode = ftpClient.getReplyCode();
        if (inputStream == null || returnCode == 550) 
        {
            return false;
        }
        return true;
    }
 
    void logout() throws IOException {
        if (ftpClient != null && ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
            System.out.println("Logged out");
        }
    }
 
    
}
 



