Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561

    [java]problema con un ftp client

    Ho trovato questa classe su Internet:

    codice:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    
    public class FTPClient 
    {
      private BufferedReader reader=null;
      private BufferedReader writer=null;
      private Socket socket=null;
      private static boolean DEBUG = false;
      public FTPClient()
      {
      }
      /**
             * Connects to an FTP server and logs in with the supplied username
             * and password.
             */
        public synchronized void connect(String host, int port, String user, String pass) throws IOException {
                if (socket != null) {
                    throw new IOException("Socket is already connected. Disconnect first.");
                }else
                     socket = new Socket(host, port);
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                
                String response = readLine();
                if (!response.startsWith("220 ")) {
                    throw new IOException("FTPClient received an unknown response when connecting to the FTP server: " + response);
                }
                
                sendLine("USER " + user);
                
                response = readLine();
                if (!response.startsWith("331 ")) {
                    throw new IOException("FTPClient received an unknown response after sending the user: " + response);
                }
                
                sendLine("PASS " + pass);
                
                response = readLine();
                if (!response.startsWith("230 ")) {
                    throw new IOException("FTPClient was unable to log in with the supplied password: " + response);
                }
                
                // Now logged in.
        }
        
         private void sendLine(String line) throws IOException {
             if (socket == null) {
                 throw new IOException("FTPClient is not connected.");
             }
             try {
               *  writer.write(line + "\r\n");
               * writer.flush();
                 if (DEBUG) {
                     System.out.println("> " + line);
                 }
             }
             catch (IOException e) {
                 socket = null;
                 throw e;
             }
         }
         
         private String readLine() throws IOException {
             String line = reader.readLine();
             if (DEBUG) {
                 System.out.println("< " + line);
             }
             return line;
         }
       
         public synchronized boolean bin() throws IOException {
            sendLine("TYPE I");
            String response = readLine();
            return (response.startsWith("200 "));
         }
        
         public synchronized boolean ascii() throws IOException {
            sendLine("TYPE A");
            String response = readLine();
            return (response.startsWith("200 "));
         } 
         
    }
    Nella funzione sendLine() però le due istruzioni * non vengono riconosciute.
    Qualcuno può aiutarmi?


    tulipan

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [java]problema con un ftp client

    Originariamente inviato da tulipan
    Ho trovato questa classe su Internet:

    codice:
    public class FTPClient 
    {
      private BufferedReader reader=null;
      private BufferedReader writer=null;
      private Socket socket=null;
      private static boolean DEBUG = false;
    Evidentemente writer non deve certo essere un BufferedReader!

    Comunque non so cosa devi fare di preciso, ma questa classe è molto di "basso" livello, in quanto ti permette solo di inviare/ricevere comandi/risposte al/dal server FTP. Però tutta la gestione della interpretazione delle risposte, dei comandi, del trasferimento dei dati ecc..., sarebbe tutto a tuo carico.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    non ho trovato di meglio.....potresti aiutarmi a trovarne una migliore?


    tulipan

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da tulipan
    non ho trovato di meglio.....potresti aiutarmi a trovarne una migliore?
    C'è ad esempio la libreria Apache Commons Net. Non l'ho mai usata ma guardando il javadoc, mi sembra molto valida e completa.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    ok per poter usare questa libreria arrivati qui http://commons.apache.org/net/download.html cosa devo scaricare?


    tulipan

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da tulipan
    ok per poter usare questa libreria arrivati qui http://commons.apache.org/net/download.html cosa devo scaricare?
    Scarica il package dei "binari", ad esempio lo zip commons-net-1.4.1.zip (c'è anche un tar.gz ma io in genere preferisco gli zip).

    Unzippa da qualche parte e poi fai in modo che in "classpath" ci sia il file commons-net-1.4.1.jar.

    (la questione del classpath è sempre la solita ... dipende da cosa usi per compilare, se direttamente il tool javac a mano o un ambiente di sviluppo)
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    io uso JDeveloper come ambiente di sviluppo


    tulipan

  8. #8
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    Con la libreria che mi hai consigliato ho scritto questa classe:
    codice:
    public class FTP{ 
      public static void sendFile(String server, String username,String password,String fileFromTransfer)
      {
        try
        {
          // Connect and logon to FTP Server
          FTPClient ftp = new FTPClient();
          ftp.connect( server,21 );
          ftp.login( username, password );
          System.out.println("Connected to " + server + ".");
          System.out.print(ftp.getReplyString());
          File file = new File("D:\\cv.doc");
          FileInputStream fis = new FileInputStream( file ); 
          boolean b=ftp.storeFile("file", fis );
          System.out.println(b);
          fis.close();
              
          // Logout from the FTP Server and disconnect
          ftp.logout();
          ftp.disconnect();
    
        }
        catch( Exception e )
        {
          e.printStackTrace();
        }
      }
      public static void main(String[] args)
      {
        FTP f=new FTP();
        f.sendFile("localhost","messiah","messiah123","");
        
      }
    }
    però ottengo il seguente output:

    Connected to localhost.

    230 Logged on

    false


    perchè?

    tulipan

  9. #9
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    problema risolto.....non avevo il permesso di scrittura.....grazie lo stesso


    tulipan

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.