How can i upload a file into a ftp server?

Kaostias

SOLVED

I'm making a desktop aplication that must be in touch with a ftp server, the files that will upload are images. I'm trying it but I can't upload despite being connected, the problem is when I want to upload the file, the method doesn't work and returns false. the code is the following:

public boolean subeFTP(String archivoLocal, String archivoServer){
    boolean resultado=false;

    FTPClient ClienteFTP=new FTPClient();

    //Se toma el nombre del archivo local
    String aux = archivoLocal;
    aux = aux.substring(aux.lastIndexOf("\\")+1);

    try {
        //Establece el timeout del servidor en 5 minutos para evitar desconexiones
        ClienteFTP.setControlKeepAliveTimeout(300);

        //Conexión al servidor
        ClienteFTP.connect(prop_servidor, prop_puertoServidor);

        //Pone el ftp en modo pasivo para recibir un archivo, permanecerá así mientras no se cambie
        ClienteFTP.enterLocalPassiveMode(); 

        //Login
        resultado=ClienteFTP.login(prop_usuarioFtp, prop_passFtp);


        //Comprobación de la conexión
        int reply = ClienteFTP.getReplyCode(); 
        if (!FTPReply.isPositiveCompletion(reply)) {
            ClienteFTP.logout();
            ClienteFTP.disconnect();
               throw new Exception("El servidor FTP ha rechazado la conexión, error:\n" + ClienteFTP.getReplyString()); 
        }


        //Se cambia el directorio al requerido
        resultado = resultado && ClienteFTP.changeWorkingDirectory(prop_directorioServidor);


        //Tipo de archivo y envío
        resultado = resultado && ClienteFTP.setFileType(FTP.BINARY_FILE_TYPE);
        //resultado = resultado && ClienteFTP.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);


        BufferedInputStream buffIn=new BufferedInputStream(new FileInputStream(archivoLocal));
        resultado = resultado && ClienteFTP.storeFile(archivoServer+aux, buffIn);
        System.out.println(ClienteFTP.getReplyString());

        buffIn.close();
        ClienteFTP.logout();
        ClienteFTP.disconnect();

        resultado=true;

    } catch (SocketException e) {
        System.out.println("Fallo 1");
        e.printStackTrace();
        resultado=false;

    } catch (IOException e) {
        System.out.println("Fallo 2");
        resultado=false;
        e.printStackTrace();
    } catch (Exception e) {
        resultado=false;
        e.printStackTrace();
    }

    return resultado ;
}
Sagar Gandhi

You should enter in local passive mode. use following methd ftp.enterLocalPassiveMode()

According to documentation

Set the current data connection mode to PASSIVE_LOCAL_DATA_CONNECTION_MODE . Use this method only for data transfers between the client and server. This method causes a PASV (or EPSV) command to be issued to the server before the opening of every data connection, telling the server to open a data port to which the client will connect to conduct data transfers. The FTPClient will stay in PASSIVE_LOCAL_DATA_CONNECTION_MODE until the mode is changed by calling some other method such as enterLocalActiveMode()

I would also suggest you to call ftpClient.setControlKeepAliveTimeout(timeoutInMilliseconds);

Set the time to wait between sending control connection keepalive messages when processing file upload or download.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i upload a file into a ftp server?

From Dev

How do I upload a file to an FTP server using a batch script?

From Dev

How to upload file to remote FTP Server in grails

From Dev

How to upload file to FTP Server in grails

From Dev

how to upload a pdf file to ftp server using file upload controller?

From Dev

How can I download the oldest file of an FTP server?

From Dev

How can I download the oldest file of an FTP server?

From Dev

How can I parallelise the upload of a directory by FTP?

From Dev

How can I upload a file to server, without browser, using go?

From Dev

How can I upload more than one file to my FTP site?

From Dev

File does not upload to FTP server

From Dev

File upload on FTP server not working

From Dev

How I download file on FTP server in browser?

From Dev

How to upload a sqlite db file to ftp server from android program

From Dev

How to upload a sqlite db file to ftp server from android program

From Dev

How can I open an ftp server in Chrome?

From Dev

How can I download a file from a FTP server, then automatically delete it from the server once the download completes?

From Dev

How do i upload a file to my ftp at weebly.com?

From Dev

How do i upload text file to my ftp every minute?

From Dev

How can I artificially slow down an FTP upload for testing purposes?

From Dev

How can I suppress the "copying" window during FTP upload?

From Dev

How can I connect and upload to this FTP host on the console?

From Dev

How can I delete a file from an ftp server using a java program that are N weeks old

From Dev

How can I fetch a list of file names from a Remote Server via FTP?

From Dev

How to upload file to remote FTP server using Varien_Io_Ftp in Magento?

From Dev

How to upload file to remote FTP server using Varien_Io_Ftp in Magento?

From Dev

Ajax upload a file from browser to FTP server

From Dev

Upload a file to an FTP server from a string or stream

From Dev

Upload file to FTP Server using Indy

Related Related

  1. 1

    How can i upload a file into a ftp server?

  2. 2

    How do I upload a file to an FTP server using a batch script?

  3. 3

    How to upload file to remote FTP Server in grails

  4. 4

    How to upload file to FTP Server in grails

  5. 5

    how to upload a pdf file to ftp server using file upload controller?

  6. 6

    How can I download the oldest file of an FTP server?

  7. 7

    How can I download the oldest file of an FTP server?

  8. 8

    How can I parallelise the upload of a directory by FTP?

  9. 9

    How can I upload a file to server, without browser, using go?

  10. 10

    How can I upload more than one file to my FTP site?

  11. 11

    File does not upload to FTP server

  12. 12

    File upload on FTP server not working

  13. 13

    How I download file on FTP server in browser?

  14. 14

    How to upload a sqlite db file to ftp server from android program

  15. 15

    How to upload a sqlite db file to ftp server from android program

  16. 16

    How can I open an ftp server in Chrome?

  17. 17

    How can I download a file from a FTP server, then automatically delete it from the server once the download completes?

  18. 18

    How do i upload a file to my ftp at weebly.com?

  19. 19

    How do i upload text file to my ftp every minute?

  20. 20

    How can I artificially slow down an FTP upload for testing purposes?

  21. 21

    How can I suppress the "copying" window during FTP upload?

  22. 22

    How can I connect and upload to this FTP host on the console?

  23. 23

    How can I delete a file from an ftp server using a java program that are N weeks old

  24. 24

    How can I fetch a list of file names from a Remote Server via FTP?

  25. 25

    How to upload file to remote FTP server using Varien_Io_Ftp in Magento?

  26. 26

    How to upload file to remote FTP server using Varien_Io_Ftp in Magento?

  27. 27

    Ajax upload a file from browser to FTP server

  28. 28

    Upload a file to an FTP server from a string or stream

  29. 29

    Upload file to FTP Server using Indy

HotTag

Archive