Salut,
Voici mon programme pour transferer un fichier de mon directory à un ftp server. j'aimerais que les fichiers de mon directory qui sont actualisés soient automatiquement transférés vers le Ftp. En bref je veux monitorer le transfert des fichiers de mon repertoire au ftp.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 
import org.apache.commons.net.ftp.*;
import java.io.*;
 
 
public class TestCommonsNet {
 
    /** Creates a new instance of TestCommonsNet */
    public TestCommonsNet() {
    }
 
    /**
     * main - Unit test program
     * @param args Command line arguments
     *
     */
    public static void main(String args[]) {
        try {
            String ftpHost = "ftp.myserver.com";
            String ftpUserName = "tima";
            String ftpPassword = "mypassword";
            String ftpRemoteDirectory = "/home/tarcher/tmp/   ";
            String fileToTransmit = "c:\\temp\\test.txt";
 
            //Create a Jakarta Commons Net FTP Client object
            FTPClient ftp = new FTPClient();
 
            //A datatype to store responses from the FTP server
            int reply;
 
            //
            //Connect to the server
            //
            ftp.connect(ftpHost);
 
            //
            // After connection attempt, you should check the reply code to verify
            // success.
            //
            reply = ftp.getReplyCode();    
            if(!FTPReply.isPositiveCompletion(reply)) {
                try {
                    ftp.disconnect();
                } catch (Exception e) {
                    System.err.println("Unable to disconnect from FTP server " +
                                       "after server refused connection. "+e.toString());
                }
                throw new Exception ("FTP server refused connection.");
            }              
            System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());
 
            //
            //Try to login
            //
            if (!ftp.login(ftpUserName, ftpPassword)) {
                throw new Exception ("Unable to login to FTP server " +
                                     "using username "+ftpUserName+" " +
                                     "and password "+ftpPassword);
            }
 
            System.out.println(ftp.getReplyString());
            System.out.println("Remote system is " + ftp.getSystemName());
 
            //
            //Set our file transfer mode to either ASCII or Binary
            //
            //ftp.setFileType(FTP.ASCII_FILE_TYPE);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
 
            //
            //Change the remote directory
            //
            if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {
                System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
                ftp.changeWorkingDirectory(ftpRemoteDirectory);
                reply = ftp.getReplyCode();
 
                if(!FTPReply.isPositiveCompletion(reply)) {
                    throw new Exception ("Unable to change working directory " +
                                         "to:"+ftpRemoteDirectory);
                }
            }
 
            //
            //Get the file that we will transfer and send it.
            //
            File f = new File(fileToTransmit);
            System.out.println("Storing file as remote filename: " + f.getName());
            boolean retValue = ftp.storeFile(f.getName(), new FileInputStream(f));
            if (!retValue) {
              throw new Exception ("Storing of remote file failed. ftp.storeFile()" +
                                   " returned false.");
            }
 
 
 
            //
            //Disconnect from the FTP server
            //
            try {
                //ftp.logout();
                ftp.disconnect();
            } catch (Exception exc) {
                System.err.println("Unable to disconnect from FTP server. " + exc.toString());
            }
 
        } catch (Exception e) {
            System.err.println("Error: "+e.toString());
        }
 
        System.out.println("Process Complete.");
        System.exit(0);
    }    
}