java - 使用 java 的 FTPS 连接构建器在连接打开后使用与标准 ftp/sftp 相同的功能

标签 java redhat ftps

我有一个自定义小程序,它从调度程序调用来打开 ftp 或 sftp 连接并获取或放置多个文件(高峰期间每小时大约 100 万个文件)。问题是代码创建的脚本根据数据库中的主机/端口/协议(protocol)/用户:pass/etc 打开连接,并使用 Linux (RedHat) 中的基本 ftp/sftp。下面是我的代码片段,用于执行 ftp/sftp 连接打开部分。

if (colDef.getRetrieveFiles().equals("ALL")){
                    if (!(colDef.getConnectionType().equals("SFTP")) && (colDef.getPrefAuth().equals("PASSWORD"))) {
File scriptFile = new File(colDef.getLocalFolder() + "scripts/script999.sh");
                        File cmdFile = new File(colDef.getLocalFolder() + "scripts/script999.cmd");
                        if (scriptFile.exists()) {
                            scriptFile.delete();
                        }
                        if (cmdFile.exists()) {
                            cmdFile.delete();
                        }

                        FileWriter scriptFw = new FileWriter(colDef.getLocalFolder() + "scripts/script999.sh", true);
                        BufferedWriter scriptBw = new BufferedWriter(scriptFw);
                        FileWriter cmdFw = new FileWriter(colDef.getLocalFolder() + "scripts/script999.cmd", true);
                        BufferedWriter cmdBw = new BufferedWriter(cmdFw);

                        if (colDef.getConnectionType().equals("FTP")) {
                            cmdBw.write("open " + colDef.getHost() + " " + colDef.getFtpPort());
                            cmdBw.newLine();
                            cmdBw.write("user " + colDef.getUsername() + " " + colDef.getPassword());

                            cmdBw.newLine();
                            if (!colDef.getSiteCommand().equals("")) {
                                cmdBw.write(colDef.getSiteCommand());
                                cmdBw.newLine();
                            }
                            if (!colDef.getQuoteCommand().equals("")) {
                                cmdBw.write(colDef.getQuoteCommand());
                                cmdBw.newLine();
                            }
                            if (!colDef.getFTPTransferType().equals("")) {
                                cmdBw.write(colDef.getFTPTransferType());
                                cmdBw.newLine();
                            }
                        }
                        cmdBw.write("cd " + colDef.getSourceFolder());
                        cmdBw.newLine();

                        if (colDef.getConnectionType().equals("FTP"))
                        {
                            cmdBw.write("ls -lrt");
                            cmdBw.newLine();
                        }
                        if (colDef.getConnectionType().equals("SFTP"))
                        {
                            cmdBw.write("ls -lrt");
                            cmdBw.newLine();
                        }

                        cmdBw.write("bye");
                        cmdBw.newLine();

                        scriptBw.write("#!/bin/sh");
                        scriptBw.newLine();
                        scriptBw.write("cd " + colDef.getLocalFolder() + "inputDir/999");
                        scriptBw.newLine();
                        if ((colDef.getConnectionType().equals("SFTP")) && (colDef.getPrefAuth().equals("PUBLICKEY"))) {
                            scriptBw.write("sftp -oPort=" + colDef.getFtpPort() + " -b " + colDef.getLocalFolder() + "scripts/script999.cmd " + colDef.getUsername() + "@" + colDef.getHost());
                            //Check SFTP return code
                            scriptBw.newLine();
                            scriptBw.write("if [[ $? -ne 0 ]]; then ");
                            scriptBw.newLine();
                            scriptBw.write("echo \" SFTP Error while accessing remote producer server.\"");
                            scriptBw.newLine();
                            scriptBw.write("exit $?");
                            scriptBw.newLine();
                            scriptBw.write("fi");
                        }
                        if (colDef.getConnectionType().equals("FTP")) {
                            scriptBw.write("ftp -n -i -p < " + colDef.getLocalFolder() + "scripts/script999.cmd");
                        }
                        scriptBw.newLine();
                        scriptBw.close();
                        scriptFw.close();
                        cmdBw.close();
                        cmdFw.close();
                        finalListOfRemoteFilenames = colUtil.executeScript(scriptFile, cmdFile, colDef, 999, allProperties.getVCCollectionProperties().getProperty("DELETE_SCRIPTS").trim());
                    }else{ //End of FTP and SFTP with Public Key - the else applies to SFTP PASSWORD only
                        finalListOfRemoteFilenames = colUtil.getSFTPListOfRemoteFilenames(colDef, allProperties);
                    }

有什么我可以用来以类似方式打开 ftps 的连接,并使用标准 ftp 命令进行 get 和 put(因为已经内置了自定义线程/每个文件检查和数据库更新等,我不想完全重做所有内容。)

我已经为此苦苦挣扎了一段时间了,

任何帮助表示赞赏。谢谢,

最佳答案

我只好使用 FTPClient 和 FTPSClient 在 apache.commons.net 3.3 上重做它。

就像魅力一样。我不知道为什么这一切都是通过脚本完成的,而不是从一开始就使用 FTP 库。

干杯!

关于java - 使用 java 的 FTPS 连接构建器在连接打开后使用与标准 ftp/sftp 相同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366872/

相关文章:

ssl - "SFTP"和 "FTP over SSL"是一回事吗?

php - Symfony2 : Undefined Function "ftp_ssl_connect"

java - 如何使用 Java 中的 MyCanvas 创建动态更新的 GUI?

java - 像素化视频处理

linux - sudo 还是不 sudo?

cassandra - 无法在 Red Hat 7/CentOS 7 上安装 Apache Cassandra 4.0.5

java - 如何捕获继承类型的异常

java - addStyleName() 的共同属性表达在 setStyleName() 甚至 setStylePrimaryName() 之上!

python - 在 RedHat 上部署 Django 项目

c# - 我使用什么命令通过 ftps 连接更改文件权限?