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

时间:2015-05-21 07:10:50

标签: java redhat ftps

我有一个自定义小程序,从调度程序调用以打开ftp或sftp连接并获取或放置多个文件(在峰值期间每小时大约有100万个文件)。问题是代码创建了基于主机/端口/协议/用户打开连接的脚本:从数据库传递/ 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的simular fasion中打开连接并使用标准的ftp命令来获取和放置(因为有自定义线程/每个文件检查和数据库更新等已经内置并且我不知道我想完全重做一切。)

现在已经挣扎了一段时间,

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

1 个答案:

答案 0 :(得分:0)

我使用FTPClient&amp; amp;而是使用apache.commons.net 3.3重做它。 FTPSClient。

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

干杯!