From cfa4fe1280993ca538c3ed8986c79396a2548098 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Wed, 30 Aug 2023 17:36:55 +0200 Subject: [PATCH] Added 2 new commands - mkdir and uploadFiles --- src/main/java/org/nanoboot/ftps/Main.java | 4 + .../nanoboot/ftps/commands/MkdirCommand.java | 75 ++++++++++++++++++ .../nanoboot/ftps/commands/UploadCommand.java | 4 +- .../ftps/commands/UploadFilesCommand.java | 78 +++++++++++++++++++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/nanoboot/ftps/commands/MkdirCommand.java create mode 100644 src/main/java/org/nanoboot/ftps/commands/UploadFilesCommand.java diff --git a/src/main/java/org/nanoboot/ftps/Main.java b/src/main/java/org/nanoboot/ftps/Main.java index 95c9caa..fa06260 100755 --- a/src/main/java/org/nanoboot/ftps/Main.java +++ b/src/main/java/org/nanoboot/ftps/Main.java @@ -22,7 +22,9 @@ import org.nanoboot.ftps.commands.HelpCommand; import org.nanoboot.ftps.commands.VersionCommand; import java.util.HashSet; import java.util.Set; +import org.nanoboot.ftps.commands.MkdirCommand; import org.nanoboot.ftps.commands.UploadCommand; +import org.nanoboot.ftps.commands.UploadFilesCommand; /** * @author Robert Vokac @@ -40,6 +42,8 @@ public class Main { commandImplementations.add(new HelpCommand()); commandImplementations.add(new VersionCommand()); commandImplementations.add(new UploadCommand()); + commandImplementations.add(new MkdirCommand()); + commandImplementations.add(new UploadFilesCommand()); Command foundCommand = null; for(Command e:commandImplementations) { diff --git a/src/main/java/org/nanoboot/ftps/commands/MkdirCommand.java b/src/main/java/org/nanoboot/ftps/commands/MkdirCommand.java new file mode 100644 index 0000000..0c7787c --- /dev/null +++ b/src/main/java/org/nanoboot/ftps/commands/MkdirCommand.java @@ -0,0 +1,75 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// ftps: Tool generating documentation. +// Copyright (C) 2023-2023 the original author or authors. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; version 2 +// of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// + +package org.nanoboot.ftps.commands; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPSClient; +import org.nanoboot.ftps.Command; +import org.nanoboot.ftps.FtpsArgs; +import org.nanoboot.ftps.FtpsCredentials; + +/** + * + * @author pc00289 + */ +public class MkdirCommand implements Command { + + public MkdirCommand() { + } + + @Override + public String getName() { + return "mkdir"; + } + + @Override + public void run(FtpsArgs ftpsArgs) { + System.out.println("mkdir"); + if(ftpsArgs.getArgs().size() < 2) { + throw new RuntimeException("Upload argument expects credentials and a directory name as the arguments."); + } + FtpsCredentials config = new FtpsCredentials(ftpsArgs.getArgs().get(0)); + String dirName = ftpsArgs.getArgs().get(1); + + System.out.println("Going to create directory: " + dirName); + FTPSClient ftps = new FTPSClient(); + try { + ftps.connect(config.getHost(), config.getPort()); + ftps.login(config.getUser(), config.getPassword()); + ftps.changeWorkingDirectory(config.getWorkingDir()); + + ftps.enterLocalPassiveMode(); + ftps.setFileType(FTP.BINARY_FILE_TYPE); + boolean mkdirWasSuccessful=ftps.makeDirectory(dirName); + + System.out.println("Creating directory was " + (mkdirWasSuccessful ? "successful" : "unsuccessful")); + + ftps.disconnect(); + } catch (IOException ex) { + ex.printStackTrace(); + System.err.println(ex.getMessage()); + } + + } + +} diff --git a/src/main/java/org/nanoboot/ftps/commands/UploadCommand.java b/src/main/java/org/nanoboot/ftps/commands/UploadCommand.java index afcc288..aa022de 100644 --- a/src/main/java/org/nanoboot/ftps/commands/UploadCommand.java +++ b/src/main/java/org/nanoboot/ftps/commands/UploadCommand.java @@ -46,11 +46,12 @@ public class UploadCommand implements Command { public void run(FtpsArgs ftpsArgs) { System.out.println("upload"); if(ftpsArgs.getArgs().size() < 2) { - throw new RuntimeException("Upload argument expects credentials and afile name as the arguments."); + throw new RuntimeException("Upload argument expects credentials and a file name as the arguments."); } FtpsCredentials config = new FtpsCredentials(ftpsArgs.getArgs().get(0)); String fileName = ftpsArgs.getArgs().get(1); File file = new File(fileName); + System.out.println("Going to upload file: " + file.getAbsolutePath()); FTPSClient ftps = new FTPSClient(); try { ftps.connect(config.getHost(), config.getPort()); @@ -66,6 +67,7 @@ public class UploadCommand implements Command { ftps.disconnect(); } catch (IOException ex) { + ex.printStackTrace(); System.err.println(ex.getMessage()); } diff --git a/src/main/java/org/nanoboot/ftps/commands/UploadFilesCommand.java b/src/main/java/org/nanoboot/ftps/commands/UploadFilesCommand.java new file mode 100644 index 0000000..358aa6d --- /dev/null +++ b/src/main/java/org/nanoboot/ftps/commands/UploadFilesCommand.java @@ -0,0 +1,78 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// ftps: Tool generating documentation. +// Copyright (C) 2023-2023 the original author or authors. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; version 2 +// of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// + +package org.nanoboot.ftps.commands; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPSClient; +import org.nanoboot.ftps.Command; +import org.nanoboot.ftps.FtpsArgs; +import org.nanoboot.ftps.FtpsCredentials; + +/** + * + * @author pc00289 + */ +public class UploadFilesCommand implements Command { + + public UploadFilesCommand() { + } + + @Override + public String getName() { + return "uploadFiles"; + } + + @Override + public void run(FtpsArgs ftpsArgs) { + System.out.println("uploadFiles"); + if(ftpsArgs.getArgs().size() < 2) { + throw new RuntimeException("Upload argument expects credentials and a directory name as the arguments."); + } + FtpsCredentials config = new FtpsCredentials(ftpsArgs.getArgs().get(0)); + String dirName = ftpsArgs.getArgs().get(1); + File dir = new File(dirName); + System.out.println("Going to upload all files in directory: " + dir.getAbsolutePath()); + FTPSClient ftps = new FTPSClient(); + try { + ftps.connect(config.getHost(), config.getPort()); + ftps.login(config.getUser(), config.getPassword()); + ftps.changeWorkingDirectory(config.getWorkingDir()); + ftps.enterLocalPassiveMode(); + ftps.setFileType(FTP.BINARY_FILE_TYPE); + + for (File fileInDir : dir.listFiles()) { + FileInputStream fileInputStream = new FileInputStream(fileInDir); + boolean uploadWasSuccessful = ftps.storeFile(fileInDir.getName(), fileInputStream); + fileInputStream.close(); + System.out.println("Upload of file \"" + fileInDir.getName() + "\" was " + (uploadWasSuccessful ? "successful" : "unsuccessful")); + } + + ftps.disconnect(); + } catch (IOException ex) { + ex.printStackTrace(); + System.err.println(ex.getMessage()); + } + + } + +}