rendered paste body/*Packages*/
package connection;
/*Imports*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.*;
public class FtpConnection {
private FTPClient ftp = new FTPClient();
private String ip;
private String username;
private String password;
/**
* Konstruktor
* @param IP-Addresse mit der verbunden werden soll
*/
public FtpConnection(String ip, String username, String password){
this.ip = ip;
this.username= username;
this.password= password;
}
/*FTP Verbindung aufbauen*/
public void connect(){
int reply;
try{
ftp.connect(ip);
System.out.println("Connect to " + ip);
/*Überprüfe korrekte Verbindung*/
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
System.out.println("FTP Server blockt Verbindung");
}
}
catch(Exception ex){
System.out.println("Fehler");
}
}
/*FTP Login*/
public void login(){
try {
ftp.login(username, password);
System.out.println("Login erfolgreich");
} catch (Exception ex) {
System.out.println("Login fehlgeschlagen");
}
}
/*Datei auf den FTP Server Uploaden*/
public void uploadFile(String remotepath, String localpath){
try {
FileInputStream fis = new FileInputStream(localpath);
ftp.storeFile(remotepath, fis);
System.out.println("Datei gespeichert");
}
catch(Exception ex){
System.out.println("Uploadfehler");
}
}
/*Erstellt einen Ordner auf dem FTP Server (Überstruktur muss schon vorhanden sein)*/
public void createDir(String path){
String[] temp = path.split("\\\\");
path = "";
for(int i = 1; i< temp.length; i++){
if(i== temp.length-1){
path = path + temp[i];
}
else{
path = path + temp[i] + "\\";
}
}
try {
ftp.makeDirectory(path);
System.out.println("Erstelle Ordner im Pfad: " + path);
} catch (IOException e) {
System.out.println("Could not create Dir");
}
}
/*Ordner auf FTP Server Uploaden ( WICHTIG: Nur der Inhalt wird hochgeladen, nicht der Ordner selber!)*/
public void uploadDir(String path) {
//Übertragungsmodus auf Binär stellen
try {
this.ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (IOException e) {
System.out.println("Konnte nicht auf Binär wechseln");
}
File dir = new File(path);
File[] files = dir.listFiles();
if (files != null) { // Erforderliche Berechtigungen etc. sind vorhanden
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
createDir(files[i].getPath());
uploadDir(files[i].getPath());
}
else {
uploadFile(createPath(files[i].getPath()), files[i].getPath());
System.out.println("Erstelle Datei im Pfad: " + createPath(files[i].getPath()));
}
}
}
}
/*Erstellt korrekten Pfad für Upload der Dateien*/
public String createPath(String path){
String[] temp = path.split("\\\\");
path = "";
for(int i = 1; i< temp.length; i++){
if(i== temp.length-1){
path = path + temp[i];
}
else{
path = path + temp[i] + "\\";
}
}
return path;
}
/*Datei von FTP Server downloaden*/
public void downloadFile(String filename){
try{
FileOutputStream fos = new FileOutputStream(filename);
ftp.retrieveFile("\\" + filename, fos);
System.out.println("Datei wurde runtergeladen");
}
catch(Exception ex){
System.out.println("Downloadfehler");
}
}
/*Datei von FTP Server löschen*/
public void deleteFile(String filename){
try {
ftp.deleteFile(filename);
System.out.println("Datei gelöscht");
} catch (IOException e) {
System.out.println("Datei konnte nicht gelöscht werden");
}
}
/*FTP Verbindung trennen*/
public void disconnect(){
try {
ftp.disconnect();
System.out.println("Verbindung getrennt");
} catch (Exception ex) {
System.out.println("Fehler!");
}
}
}