Apache FTPS Client storeFile从Unix / Linux / Max问题到Windows FTPS服务器

时间:2017-01-02 06:13:28

标签: java apache ssl ftp ftps

我在从Unix / Mac / Linux环境中将文件传输到Windows FTP服务器时遇到问题。

虽然完全相同的java代码适用于Windows PC。  从* Nix / Mac传输只能在ftp会话上使用此命令

set ftps:initial-prot 
set ftp:ssl-force true
set ftp:ssl-protect-data true
set ssl:verify-certificate no

在我的Windows机器上我不需要它们 - 我认为它与系统变量有关。

这是我的java代码

protected FTPClient getClient(DeliveryDetails details) {
    return new FTPSClient(false); // the connection is Explicit
}

public void setClient(FTPClient client, DeliveryDetails details) throws Exception {
    client.setConnectTimeout(10000);
    client.setDefaultTimeout(1000 * 60 * 2);
    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");
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    FTPSClient ftpsClient = (FTPSClient) client;
    ftpsClient.execPBSZ(0);
    ftpsClient.execPROT("P");
    // both with it and without it didnt work ftpsClient.setWantClientAuth(false);
}

public void saveToServer(FTPClient client, File fileName, InputStream stream){
    BufferedInputStream bis = new BufferedInputStream(stream);
    boolean isSaved = client.storeFile(filename, bis);
    client.logout();
}

FTPS Apache类中的这些参数等同于什么?

set ftps:initial-prot 
set ftp:ssl-force true
set ftp:ssl-protect-data true
set ssl:verify-certificate no

1 个答案:

答案 0 :(得分:1)

Windows NT似乎不支持在 FTP.BLOCK_TRANSFER_MODE

中写入数据

轻松修复

    private static final String WINDOWS_NT_SYTEM_TYPE = "Windows_NT";
   ....
   ....
    try {
            String res = client.getSystemType();
            if (res.equals(WINDOWS_NT_SYTEM_TYPE)) {
                client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            } else {
                client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
            }
        }