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

avec Java Discussion :

pb de connexion à un serveur ftp


Sujet :

avec Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut pb de connexion à un serveur ftp
    Bonjour, j'ai un problème avec mon code pour me connecter au serveur ftp, voici l'erreur:
    java.net.MalformedURLException: unknown protocol: ftp://127.0.0.1/

    Je fais ceci dans un bouton donc une JForm:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    File fichier = new File("C://25.jpg");
     UrlHelper.downloadFile("ftp://127.0.0.1/", fichier, "GARDIEN");
    et voici mon code:
    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
     
    package test;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import sun.net.ftp.FtpClient;
     
    public class UrlHelper {
     
        public static void downloadFile(String adresse) {
     
            downloadFile(adresse, null, null);
        }
     
        public static void downloadFile(String adresse, File dest, String username) {
            BufferedReader reader = null;
            FileOutputStream fos = null;
            InputStream in = null;
            try {
     
                // création de la connection
                String file = "25.jpg";
                URL url = new URL(adresse, username, file);
                URLConnection conn = url.openConnection();
     
     
                System.out.println(adresse);
     
                String FileType = conn.getContentType();
                System.out.println("FileType : " + FileType);
     
                int FileLenght = conn.getContentLength();
                if (FileLenght == -1) {
                    throw new IOException("Fichier non valide.");
                }
     
                // lecture de la réponse
                in = conn.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in));
                if (dest == null) {
                    String FileName = url.getFile();                           
                    FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
                    dest = new File(FileName);
                }
                fos = new FileOutputStream(dest);
                byte[] buff = new byte[1024];
                int l = in.read(buff);
                while (l > 0) {
                    fos.write(buff, 0, l);
                    l = in.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    Merci d'avance.

  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
    "openConnection" de java.net.URL ne gère pas le protocole FTP.
    Tu dois passer par une autre API, ou encore coder la récupération à la main.
    Voici de quoi t'amuser : RFC FTP.

  3. #3
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    Apache commons net contient tout ce qu'il faut pour faire du FTP client (entre autre).

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    j'ai fait un nouveau programme mais je ne sais pas si la gestion des flux, etc. est correct.

    Pouvez-vous m'aider?

    Merci encore.

    mon fichier JForm:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    try {
     
                receiveFileByFTP ftp = new receiveFileByFTP();
                ftp.receiveFileByFTP();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }

    Mon code:
    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package test;
    import java.io.*;
    import sun.net.ftp.FtpClient;
    import sun.net.*;
    /**
     *
     * @author Client
     */
    public class receiveFileByFTP {
     
    public static void receiveFileByFTP() throws IOException
        {
     
        String username = "DRH";
        String password = "admin";
        String host = "10.104.100.81";
        String filename = "25.jpg";
     
        FtpClient ftp = new FtpClient();
        byte[] client;
            ftp.openServer(host, 21);
            ftp.login(username, password);
    	ftp.binary();    
     
     InputStream os = ftp.get(filename); 
     OutputStream fis = new FileOutputStream("C:\\Documents and Settings\\adminlocal\\Mes documents\\Photos_téléchargees_ftp");
     
     
            byte buf[] = new byte[8192];
            int bytesRead = os.read(buf);
            while (bytesRead != -1) {
                fis.write(buf, 0, bytesRead);
                bytesRead = os.read(buf);
            }
     
            fis.close();
            os.close();
    }}

  5. #5
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    Plus simple pour télécharger le fichier :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    		// Récupération du fichier
    		ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    		String nomFichierCible = "C:/Bla/Toto.txt";
    		RandomAccessFile outtxtfile = new RandomAccessFile(nomFichierCible, "rw");
    		try {
    			FileOutputStream fileStream = new FileOutputStream(outtxtfile.getFD());
    			if (ftp.retrieveFile("Toto.txt", fileStream)) {
    				System.out.println("Toto.txt téléchargé avec succès");
    			}
    		} finally {
    			outtxtfile.close();
    		}
    Veille à fermer tes flux dans un bloc "finally".

  6. #6
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    quand je passe par FtpClient, les fonctions retrieveFile() et setFileType() ne sont pas reconnu comment se fait-il ?
    Merci encore.

    j'ai fait ceci exactement:
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package test;
     
    import java.io.FileOutputStream;
    import java.io.RandomAccessFile;
    import sun.net.ftp.FtpClient;
     
    /**
     *
     * @author Jérémy Greslon
     */
    public class DownloadFile {
     
        public static void DownloadFile(){
     
        String username = "DRH";
        String password = "admin";
        String host = "127.0.0.1";
     
     
        FtpClient ftp = new FtpClient();
     
     
        //byte[] client;
            ftp.openServer(host, 21);
            ftp.login(username, password);
    	ftp.binary(); 
     
     
     
                    // Récupération du fichier
    		//ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    		String nomFichierCible = "C:/Bla/Toto.txt";
    		RandomAccessFile outtxtfile = new RandomAccessFile(nomFichierCible, "rw");
    		try {
    			FileOutputStream fileStream = new FileOutputStream(outtxtfile.getFD());
    			if (ftp.retrieveFile("Toto.txt", fileStream)) {
    				System.out.println("Toto.txt téléchargé avec succès");         
    			}
    		} finally {
    			outtxtfile.close();
    		}
        }
    }

  7. #7
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    Parce que tu utilises FtpClient du package sun. Moi j'utilise celui de apache commons net (il faut télécharger la librairie).

    Je n'utilise jamais les packages com.sun.* ou sun.* car il n'y a pas de garanti de le retrouver à une prochaine version de java.

  8. #8
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    j'ai une erreur de ce type:
    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
    Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: test/DownloadFile
            at test.test.jButtonServeurActionPerformed(test.java:61)
            at test.test.access$000(test.java:13)
            at test.test$1.actionPerformed(test.java:35)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6038)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
            at java.awt.Component.processEvent(Component.java:5803)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4410)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4240)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2429)
            at java.awt.Component.dispatchEvent(Component.java:4240)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    Et voici mon code:
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
     
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.SocketException;
    import org.apache.commons.net.ftp.FTPClient;
     
    /**
     *
     * @author Jérémy Greslon
     */
    public class DownloadFile {
     
        public static void DownloadFile() throws SocketException, IOException {
     
            String username = "DRH";
            String password = "admin";
            String host = "127.0.0.1";
            //String filename = "25.jpg";
     
            FTPClient ftp = new FTPClient();
     
            ftp.connect(host);
            ftp.login(username, password);
     
     
            // Récupération du fichier
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            String nomFichierCible = "C:/toto.jpg";
            RandomAccessFile outtxtfile = new RandomAccessFile(nomFichierCible, "rw");
            try {
                FileOutputStream fileStream = new FileOutputStream(outtxtfile.getFD());
                if (ftp.retrieveFile("25.jpg", fileStream)) {
                    System.out.println("Toto.txt téléchargé avec succès");
                }
            } finally {
                outtxtfile.close();
            }
        }
    }
    Merci beaucoup

  9. #9
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    c'est bon ce problème est résolu, c'était juste un problème de liaison avec mon IHM.
    MAis maintenant il me reste un problème, mon serveur ftp me met ceci: 530 Please log in with USER and PASS first.


    Merci à vous

  10. #10
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    jai résolu le problème avec le USER, mais maintenant mon serveur ftp me met File not found, alors que le fichier que je veux télécharger ce trouve bien dans le bon dossier et je fais bien appel à ce dossier.

    La photo que je veux télécharger se trouve ici: C:/Photos_employes
    Et je veux mettre cette photo ici: C:/

    C'est bien ce que j'ai fait dans mon code non ? Merci bien.

    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
     
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.SocketException;
    import org.apache.commons.net.ftp.FTPClient;
     
    /**
     *
     * @author Jérémy Greslon
     */
    public class DownloadFile {
     
        public static void DownloadFile() throws SocketException, IOException {
     
            String username = "GARDIEN";
            String password = "admin";
            String host = "127.0.0.1";
            //String filename = "25.jpg";
     
            FTPClient ftp = new FTPClient();
     
            ftp.connect(host);
            ftp.login(username, password);
     
     
            // Récupération du fichier
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            String nomFichierCible = "C:/25.jpg";
            RandomAccessFile outtxtfile = new RandomAccessFile(nomFichierCible, "rw");
            try {
                FileOutputStream fileStream = new FileOutputStream(outtxtfile.getFD());
                if (ftp.retrieveFile("25.jpg", fileStream)) {
                    System.out.println("Toto.txt téléchargé avec succès");
                }
            } finally {
                outtxtfile.close();
            }
        }
    }

  11. #11
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    Il te le fait sur quelle ligne le FileNotFoundException ?

  12. #12
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    je n'ai plus d'erreur sur le FileNotFoundException, c'est le serveur ftp qui me renvoie une erreur 550 File not found

  13. #13
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    C'est que tu n'es pas dans le bon dossier ou qu'il n'y a pas de fichier "25.jpg" accessible... C'est assez explicite.

  14. #14
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    j'ai pourtant bien le bon dossier et le fichier est bien présent dans ce dossier

  15. #15
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    Citation Envoyé par mimi51340 Voir le message
    j'ai pourtant bien le bon dossier et le fichier est bien présent dans ce dossier
    Si c'était le cas il n'y aurait pas d'erreur. Cherche un peu, le cas échéant utilise un autre client FTP genre Filezilla pour voir ce qu'il se passe (l'avantage de Filezilla c'est qu'il affiche les commandes qu'il fait, tu pourras donc t'en inspirer pour arriver au bon endroit).

  16. #16
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Points : 31
    Points
    31
    Par défaut
    c'est réglé merci pour votre aide mon serveur ftp fonctionne

  17. #17
    Membre expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Points : 3 083
    Points
    3 083
    Par défaut
    ? Merci.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [EdtFTPj] Connexion à un serveur FTP
    Par ensinienne dans le forum API standards et tierces
    Réponses: 9
    Dernier message: 01/03/2013, 14h43
  2. [QtNetwork] Connexion à un serveur FTP
    Par Jiyuu dans le forum PyQt
    Réponses: 1
    Dernier message: 31/08/2011, 01h00
  3. [Débutant] Connexion au serveur FTP
    Par maestroENSI dans le forum C#
    Réponses: 3
    Dernier message: 29/07/2011, 18h24
  4. connexion à mon serveur ftp impossible ?
    Par petitclem dans le forum Distributions
    Réponses: 0
    Dernier message: 28/05/2008, 10h39
  5. [eSVN+Fedora] Connexion à un serveur ftp distant
    Par lun4t1k dans le forum RedHat / CentOS / Fedora
    Réponses: 0
    Dernier message: 17/10/2007, 23h12

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