java - 如何将文件上传到 ftp 服务器?

标签 java ftp apache-commons apache-commons-net

已解决

我正在制作一个必须与 ftp 服务器联系的桌面应用程序,将上传的文件是图像。我正在尝试,但尽管已连接,但无法上传,问题是当我想上传文件时,该方法不起作用并返回 false。代码如下:

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 ;
}

最佳答案

您应该进入本地被动模式。使用以下方法ftp.enterLocalPassiveMode()

根据文档

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()

我还建议您调用ftpClient.setControlKeepAliveTimeout(timeoutInMilliseconds);

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

关于java - 如何将文件上传到 ftp 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460289/

相关文章:

java - ftp 客户端 : org. apache.commons.net.ftp.FTPClient 上的 noClassDefFound 错误

java - Gradle 和 Android : Could not resolve dependencies com. android.support

java - 用Java连接Advantage数据库

windows - 如何在DOS的ftp模式下查看unix中文件的内容?

java - 为什么Hadoop FTPFileSystem.listStatus(Path path)不起作用?

java - Netbeans 无法导入 FileUtils 包?

Java Web 应用程序 : How to implement caching techniques?

java - ORA-01861 : literal does not match format string error isn't always shown in similar cases

php - FTP获取所有文件

java - 验证没有 http 的 URL