Added 2 new commands - mkdir and uploadFiles

This commit is contained in:
Robert Vokac 2023-08-30 17:36:55 +02:00
parent d07be4253c
commit cfa4fe1280
No known key found for this signature in database
GPG Key ID: 693D30BEE3329055
4 changed files with 160 additions and 1 deletions

View File

@ -22,7 +22,9 @@ import org.nanoboot.ftps.commands.HelpCommand;
import org.nanoboot.ftps.commands.VersionCommand; import org.nanoboot.ftps.commands.VersionCommand;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.nanoboot.ftps.commands.MkdirCommand;
import org.nanoboot.ftps.commands.UploadCommand; import org.nanoboot.ftps.commands.UploadCommand;
import org.nanoboot.ftps.commands.UploadFilesCommand;
/** /**
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a> * @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
@ -40,6 +42,8 @@ public class Main {
commandImplementations.add(new HelpCommand()); commandImplementations.add(new HelpCommand());
commandImplementations.add(new VersionCommand()); commandImplementations.add(new VersionCommand());
commandImplementations.add(new UploadCommand()); commandImplementations.add(new UploadCommand());
commandImplementations.add(new MkdirCommand());
commandImplementations.add(new UploadFilesCommand());
Command foundCommand = null; Command foundCommand = null;
for(Command e:commandImplementations) { for(Command e:commandImplementations) {

View File

@ -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());
}
}
}

View File

@ -46,11 +46,12 @@ public class UploadCommand implements Command {
public void run(FtpsArgs ftpsArgs) { public void run(FtpsArgs ftpsArgs) {
System.out.println("upload"); System.out.println("upload");
if(ftpsArgs.getArgs().size() < 2) { 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)); FtpsCredentials config = new FtpsCredentials(ftpsArgs.getArgs().get(0));
String fileName = ftpsArgs.getArgs().get(1); String fileName = ftpsArgs.getArgs().get(1);
File file = new File(fileName); File file = new File(fileName);
System.out.println("Going to upload file: " + file.getAbsolutePath());
FTPSClient ftps = new FTPSClient(); FTPSClient ftps = new FTPSClient();
try { try {
ftps.connect(config.getHost(), config.getPort()); ftps.connect(config.getHost(), config.getPort());
@ -66,6 +67,7 @@ public class UploadCommand implements Command {
ftps.disconnect(); ftps.disconnect();
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace();
System.err.println(ex.getMessage()); System.err.println(ex.getMessage());
} }

View File

@ -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());
}
}
}