简体   繁体   English

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

[英]FTPS Connection builder with java that uses the same as standard ftp/sftp after connection is open

I have a custom applet that gets called from a scheduler to open an ftp or sftp connection and get or put multiple files (about 1 million files every hour during peak). 我有一个自定义的applet,可以从调度程序中调用它来打开ftp或sftp连接,并获取或放置多个文件(在高峰期每小时大约100万个文件)。 Problem is the code creates scripts that open the connection based on host/port/protocol/user:pass/etc from the database and uses the basic ftp/sftp from Linux (RedHat). 问题是代码创建了脚本,这些脚本根据数据库中的host / port / protocol / user:pass / etc打开连接,并使用Linux(RedHat)中的基本ftp / sftp。 Below is a snippet of my code that does the ftp/sftp connection open part. 以下是执行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);
                    }

Is there anything I can use to open a connection in a simular fasion for ftps and use the standard ftp commands for get and put (as there is custom threading/per file checking and database updating etc already build in and I don't want to have to redo everything completely.) 有什么我可以用来在ftps的模拟界面中打开连接并使用标准ftp命令进行获取和放置的(因为已经内置了自定义线程/每个文件检查和数据库更新等功能,所以我不想必须完全重做。)

Been struggling with this for some time now, 一直为此苦苦挣扎,

Any help appreciated. 任何帮助表示赞赏。 Thank you, 谢谢,

I resorted to rather redoing it with apache.commons.net 3.3 using the FTPClient & FTPSClient. 我诉诸于使用FTPClient和FTPSClient通过apache.commons.net 3.3重做它。

Works like a charm. 奇迹般有效。 I do not know why this was all done with scripts and not using an FTP library from the start. 我不知道为什么一开始都是用脚本而不是使用FTP库完成的。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM