FTPS storeFile返回始终为false的Java

时间:2016-12-11 20:23:11

标签: java ftp-client ftps

我试图将文件发送到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");

我正在尝试这两个链接:link1link2

例如,当我尝试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代码才能做到这一点。任何人都可以给我一个线索

1 个答案:

答案 0 :(得分:0)

我已经尝试过在不同网站上提供的几乎任何可能的解决方案都无法使其与Apache FTPS CLIENT一起使用, 不得不使用一个像魅力一样工作的不同类是一个片段:

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