IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

copie des fichiers d'un ftp à mon "local directory"


Sujet :

Entrée/Sortie Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut copie des fichiers d'un ftp à mon "local directory"
    Salut,
    j'aimerais transferrer des fichiers d'un ftp à mon "local directory". J'ai regardé les codes "http://commons.apache.org/net/", mais ils permettent le transfert des fichiers d'un ftpclientà son serveur ou bien d'un serveur ftp à un autre.
    J'aimerais savoir comment transferer d'un ftp externe à mon "local directory".
    Merci,
    Franckesh

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Citation Envoyé par franckesh007 Voir le message
    Salut,
    j'aimerais transferrer des fichiers d'un ftp à mon "local directory". J'ai regardé les codes "http://commons.apache.org/net/", mais ils permettent le transfert des fichiers d'un ftpclientà son serveur ou bien d'un serveur ftp à un autre.
    J'aimerais savoir comment transferer d'un ftp externe à mon "local directory".
    Merci,
    Franckesh
    Le FTP externe représente le serveur et ton "local directory" représente le client. Les exemples sont pourtant parfaitement clairs sur le site commons.apache.org/net... avec notamment un lien vers ce site. Il te suffit de faire du copier coller

  3. #3
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    merci bien pour la proposition. Pouvez me dire comment je vais exécuter le code getDatafiles en général.?

  4. #4
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Citation Envoyé par franckesh007 Voir le message
    merci bien pour la proposition. Pouvez me dire comment je vais exécuter le code getDatafiles en général.?
    Le plus simple est de commencer par les bases et lire un bon bouquin sur Java

  5. #5
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    Voici ce que j'ai à la fin. Je n'arrive toujours pas à transférer le fichier. Pouvez vous me dire ce qu'il ne va pas dans mon programme. Tout a pourtant l'air ok.

    import java.io.File;
    import java.io.FileOutputStream;
    import java.text.DateFormat;
    //import java.util.Calendar;
    //import java.util.Date;

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




    public class getDataFiles {

    public static void main(String args[])

    {

    try
    {
    String server = "ftp.mydomain.com";
    String username = "my_username";
    String password = "my_password";
    String folder = "/";
    String destinationFolder = null;
    //Calendar start = null;
    //Calendar end = null;

    // Connect and logon to FTP Server
    FTPClient ftp = new FTPClient();
    ftp.connect( server );
    ftp.login( username, password );
    System.out.println("Connected to " + server + ".");
    System.out.print(ftp.getReplyString());

    // List the files in the directory
    ftp.changeWorkingDirectory( folder );
    FTPFile[] files = ftp.listFiles();
    System.out.println( "Number of files in dir: " + files.length );
    DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT );
    for( int i=0; i<files.length; i++ )
    {
    //Date fileDate = files[ i ].getTimestamp().getTime();
    //if( fileDate.compareTo( start.getTime() ) >= 0 &&
    // fileDate.compareTo( end.getTime() ) <= 0 )
    //{
    // Download a file from the FTP Server
    System.out.print( df.format( files[ i ].getTimestamp().getTime() ) );
    System.out.println( "\t" + files[ i ].getName() );
    File file = new File( destinationFolder + File.separator + files[ i ].getName() );
    FileOutputStream fos = new FileOutputStream( file );
    ftp.retrieveFile( files[ i ].getName(), fos );
    fos.close();
    //file.setLastModified( fileDate.getTime() );
    }
    //}

    // Logout from the FTP Server and disconnect
    ftp.logout();
    ftp.disconnect();

    }
    catch( Exception e )
    {
    e.printStackTrace();
    }
    }
    }

  6. #6
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Qu'est-ce qui est affiché ? Quelles sont les erreurs ?

  7. #7
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    Dans le document , ils ont oublié de mentionner qu'on doit ajouter
    jakarta-oro-2.0.8.jar au build path. Donc Probleme résolu.

    Et si je veux que ce soit automatique; que mon programme verifie si un fichier est actualisé dans mon directory et le transfère si oui. J'aimerais donc que les updates dans le directory soient automatiquement copier dans le server ftp.

  8. #8
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    Et comment transferrer des fichiers de mon "local directory" à un Ftp.

  9. #9
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    Voici mon programme pour transferer un fichier de mon directory à un ftp server. Mais j'aimerai tranférer les fichiers de mon directory qui sont actualisés soient automatiquement transférer. Comment procéder s'il vous plait.

    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
    115
    package SendFiles;
     
    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);
        }    
    }

  10. #10
    Futur Membre du Club
    Inscrit en
    Août 2008
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 12
    Points : 5
    Points
    5
    Par défaut
    petite correction: j'aimerais que les fichiers de mon directory qui sont actualisés soient automatiquement transférés vers le Ftp. Comment procéder s'il vous plait.

Discussions similaires

  1. copie des fichiers vers un serveur ftp
    Par junior222 dans le forum Général Java
    Réponses: 1
    Dernier message: 08/03/2014, 13h42
  2. Interdire la copie des fichiers partagés en réseau
    Par Essilife dans le forum Administration
    Réponses: 4
    Dernier message: 16/01/2006, 23h46
  3. comment désactiver le copy des fichiers sur lan
    Par z7e7z dans le forum Développement
    Réponses: 4
    Dernier message: 09/01/2006, 10h10
  4. Réponses: 5
    Dernier message: 05/06/2004, 13h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo