Added 2 new commands - mkdir and uploadFiles
This commit is contained in:
parent
d07be4253c
commit
cfa4fe1280
@ -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 <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
@ -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) {
|
||||
|
75
src/main/java/org/nanoboot/ftps/commands/MkdirCommand.java
Normal file
75
src/main/java/org/nanoboot/ftps/commands/MkdirCommand.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user