1

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). 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). Below is a snippet of my code that does the ftp/sftp connection open part.

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.)

Been struggling with this for some time now,

Any help appreciated. Thank you,

RangerMcSexy
  • 11
  • 1
  • 2

1 Answers1

0

I resorted to rather redoing it with apache.commons.net 3.3 using the FTPClient & FTPSClient.

Works like a charm. I do not know why this was all done with scripts and not using an FTP library from the start.

Cheers!

RangerMcSexy
  • 11
  • 1
  • 2