FTPS storeFile return 始终为 false Java

FTPS storeFile return always false Java

我正在尝试将文件发送到 FTPS 服务器 连接方式:FTPS、ACTIVE、EXPLICIT

setFileType(FTP.BINARY_FILE_TYPE);
setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);

连接后立即检查回复字符串我得到:

234 AUTH command ok. Expecting TLS Negotiation.

来自 here

 234    Specifies that the server accepts the authentication mechanism specified by the client, and the exchange of security data is complete. A higher level nonstandard code created by Microsoft.

尝试使用 storeFile 或 storeUniqeFile 发送文件时出现错误

在我得到存储文件后检查回复字符串:501 Server cannot accept argument.

奇怪的是我能够毫无问题地为这个客户端创建一个目录 makeDirectory("test1");

我正在尝试这两个链接:link1 , link2

例如,当我尝试 to use ftp.enterLocalPassiveMode(); before ftp.storeFile(destinationfile, in); 我遇到超时错误。

有人知道如何解决吗?

public static void main(String[] args) throws Exception {

    FTPSProvider ftps = new FTPSProvider();
    String json =    "connection details";
    DeliveryDetailsFTPS details = gson.fromJson(json, DeliveryDetailsFTPS .class);
    File file = File.createTempFile("test", ".txt");
    FileUtils.write(file, " some test", true);
    try (FileInputStream stream = new FileInputStream(file)) {
        ftps.sendInternal(ftps.getClient(details), details, stream, file.getName());
    }
}




protected void sendInternal(FTPClient client, DeliveryDetailsFTPS  details, InputStream stream, String filename) throws Exception {

    try {
        // release the enc
        DeliveryDetailsFTPS ftpDetails = (DeliveryDetailsFTPS) details;
        setClient(client, ftpDetails);
        boolean isSaved = false;
        try (BufferedInputStream bis = new BufferedInputStream(stream)) {
            isSaved = client.storeFile(filename, bis);
        }
        client.makeDirectory("test1");
        client.logout();
        if (!isSaved) {
            throw new IOException("Unable to upload file to FTP");
        }
    } catch (Exception ex) {
        LOG.debug("Unable to send to FTP", ex);
        throw ex;
    } finally {
        client.disconnect();
    }
}




@Override
protected FTPClient getClient(DeliveryDetails details) {
    return new FTPSClient(isImplicitSSL((DeliveryDetailsFTPS ) details));
}

    protected void setClient(FTPClient client, DeliveryDetailsFTPS details) throws Exception {
        DeliveryDetailsFTPS ftpDetails = (DeliveryDetailsFTPS ) details;
        client.setConnectTimeout(100000);
        client.setDefaultTimeout(10000 * 60 * 2);
         
            client.setControlKeepAliveReplyTimeout(300);
         
              client.setControlKeepAliveTimeout(300);
     
        client.setDataTimeout(15000);
        client.connect(ftpDetails.host, ftpDetails.port);
        client.setBufferSize(1024 * 1024);
        client.login(ftpDetails.username, ftpDetails.getSensitiveData());
        client.setControlEncoding("UTF-8");

        int code = client.getReplyCode();
        if (code == 530) {
            throw new IOException(client.getReplyString());
        }

        // Set binary file transfer
        client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);

        if (ftpDetails.ftpMode == FtpMode.PASSIVE) {
            client.enterLocalPassiveMode();
        }

         client.changeWorkingDirectory(ftpDetails.path);
    }

我已经尝试了this解决方案也没有解决问题:

我能够发送文件的唯一方法是使用 FileZilla,它使用的是 FTPES。 但我需要我的 Java 代码来完成它。谁能给我一个线索

我已经尝试了不同网站上提供的几乎所有可能的解决方案都无法使其与 Apache FTPS 客户端一起工作, 不得不使用一个不同的 class ,它就像一个魅力这里是一个片段:

com.jscape.inet.ftps Link

private Ftps sendWithFtpsJSCAPE(ConnDetails details, InputStream stream, String filename) throws FtpException, IOException {
    Ftps ftp;
    FtpConnectionDetails ftpDetails = FtpConnectionDetails details;

    ftp = new Ftps(ftpDetails.getHost(), ftpDetails.getUsername(), ftpDetails.getPassword());



    if (ftpDetails.getSecurityMode().equals(FtpConnectionDetails.SecurityMode.EXPLICIT)) {
        ftp.setConnectionType(Ftps.AUTH_TLS);
    } else {
        ftp.setConnectionType(Ftps.IMPLICIT_SSL);
    }
    ftp.setPort(ftpDetails.getPort());

    if (!ftpDetails.getFtpMode().equals(FtpMode.ACTIVE)) {
        ftp.setPassive(true);
    }
    ftp.setTimeout(FTPS_JSCAPE_TIME_OUT);
    ftp.connect();
    ftp.setBinary();
    ftp.setDir(ftpDetails.getPath());
    ftp.upload(stream, filename);

    return ftp;
}