working FTP
This commit is contained in:
@@ -30,10 +30,10 @@ public class FTPFetcher {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ftpClient.connect(ftpServer, ftpPort);
|
ftpClient.connect(ftpServer, ftpPort);
|
||||||
ftpClient.setControlKeepAliveTimeout(300);
|
// ftpClient.setControlKeepAliveTimeout(300);
|
||||||
boolean login = ftpClient.login(ftpUser, ftpPass);
|
boolean login = ftpClient.login(ftpUser, ftpPass);
|
||||||
if (!login) {
|
if (!login) {
|
||||||
System.out.println("FTP login failed.");
|
Main.logger.error("FTP login failed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ public class FTPFetcher {
|
|||||||
|
|
||||||
String[] fileNames = ftpClient.listNames();
|
String[] fileNames = ftpClient.listNames();
|
||||||
if (fileNames == null || fileNames.length == 0) {
|
if (fileNames == null || fileNames.length == 0) {
|
||||||
System.out.println("No files found in the directory.");
|
Main.logger.error("No files found in the directory.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,21 +51,21 @@ public class FTPFetcher {
|
|||||||
try (FileOutputStream fos = new FileOutputStream(localFile)) {
|
try (FileOutputStream fos = new FileOutputStream(localFile)) {
|
||||||
boolean success = ftpClient.retrieveFile(fileName, fos);
|
boolean success = ftpClient.retrieveFile(fileName, fos);
|
||||||
if (success) {
|
if (success) {
|
||||||
System.out.println("File downloaded: " + localFile.getName());
|
Main.logger.info("File downloaded: {}", localFile.getName());
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Failed to download the file: " + fileName);
|
Main.logger.error("Failed to download the file: {}", fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean deleted = ftpClient.deleteFile(fileName);
|
boolean deleted = ftpClient.deleteFile(fileName);
|
||||||
if (deleted) {
|
if (deleted) {
|
||||||
System.out.println("File deleted from FTP server: " + fileName);
|
Main.logger.info("File deleted from FTP server: {}", fileName);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Failed to delete the file from FTP server: " + fileName);
|
Main.logger.error("Failed to delete the file from FTP server: {}", fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
Main.logger.error(e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (ftpClient.isConnected()) {
|
if (ftpClient.isConnected()) {
|
||||||
@@ -73,7 +73,7 @@ public class FTPFetcher {
|
|||||||
ftpClient.disconnect();
|
ftpClient.disconnect();
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
ex.printStackTrace();
|
Main.logger.error(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ public class BrokerConnector {
|
|||||||
}
|
}
|
||||||
return hexString.toString();
|
return hexString.toString();
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
Main.logger.error(e.getMessage());
|
||||||
throw new RuntimeException("SHA-256 algorithm not available", e);
|
throw new RuntimeException("SHA-256 algorithm not available", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,21 +11,21 @@ import java.util.Random;
|
|||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class FTPClientConnector {
|
public class FTPProducer {
|
||||||
String ftpServer = "ftp_server";
|
String ftpServer = "ftp_server";
|
||||||
int ftpPort = 21;
|
int ftpPort = 21;
|
||||||
String ftpUser = "symphony";
|
String ftpUser = "symphony";
|
||||||
String ftpPass = "symphony";
|
String ftpPass = "symphony";
|
||||||
String ftpDir = "/";
|
String ftpDir = "/";
|
||||||
|
|
||||||
FTPClientConnector() {
|
FTPProducer() {
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
uploadRandomJsonToFtp(ftpServer, ftpPort, ftpUser, ftpPass, ftpDir);
|
uploadRandomJsonToFtp(ftpServer, ftpPort, ftpUser, ftpPass, ftpDir);
|
||||||
}
|
}
|
||||||
}, 0, 30000);
|
}, 0, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void uploadRandomJsonToFtp(String ftpServer, int ftpPort, String ftpUser, String ftpPass, String ftpDir) {
|
public static void uploadRandomJsonToFtp(String ftpServer, int ftpPort, String ftpUser, String ftpPass, String ftpDir) {
|
||||||
@@ -40,7 +40,7 @@ public class FTPClientConnector {
|
|||||||
ftpClient.connect(ftpServer, ftpPort);
|
ftpClient.connect(ftpServer, ftpPort);
|
||||||
boolean login = ftpClient.login(ftpUser, ftpPass);
|
boolean login = ftpClient.login(ftpUser, ftpPass);
|
||||||
if (!login) {
|
if (!login) {
|
||||||
System.out.println("FTP login failed.");
|
Main.logger.error("FTP login failed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,11 +49,11 @@ public class FTPClientConnector {
|
|||||||
|
|
||||||
String subDir = "uploads";
|
String subDir = "uploads";
|
||||||
if (!ftpClient.changeWorkingDirectory(subDir)) {
|
if (!ftpClient.changeWorkingDirectory(subDir)) {
|
||||||
System.out.println("Subdirectory does not exist. Creating: " + subDir);
|
Main.logger.info("Subdirectory does not exist. Creating: {}", subDir);
|
||||||
boolean dirCreated = ftpClient.makeDirectory(subDir);
|
boolean dirCreated = ftpClient.makeDirectory(subDir);
|
||||||
if (!dirCreated) {
|
if (!dirCreated) {
|
||||||
System.out.println("Failed to create subdirectory: " + subDir);
|
Main.logger.error("Failed to create subdirectory: {}", subDir);
|
||||||
System.out.println("Server reply: " + ftpClient.getReplyString());
|
Main.logger.error("Server reply: {}", ftpClient.getReplyString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ftpClient.changeWorkingDirectory(subDir);
|
ftpClient.changeWorkingDirectory(subDir);
|
||||||
@@ -62,16 +62,16 @@ public class FTPClientConnector {
|
|||||||
String filename = "file_" + System.currentTimeMillis() + ".json";
|
String filename = "file_" + System.currentTimeMillis() + ".json";
|
||||||
boolean uploaded = ftpClient.storeFile(filename, inputStream);
|
boolean uploaded = ftpClient.storeFile(filename, inputStream);
|
||||||
if (uploaded) {
|
if (uploaded) {
|
||||||
System.out.println("Successfully uploaded: " + filename);
|
Main.logger.info("Successfully uploaded: {}", filename);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Failed to upload: " + filename);
|
Main.logger.error("Failed to upload: {}", filename);
|
||||||
System.out.println("Server reply: " + ftpClient.getReplyString());
|
Main.logger.error("Server reply: {}", ftpClient.getReplyString());
|
||||||
}
|
}
|
||||||
|
|
||||||
ftpClient.logout();
|
ftpClient.logout();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Error during FTP operation: " + e.getMessage());
|
Main.logger.error("Error during FTP operation: {}", e.getMessage());
|
||||||
e.printStackTrace();
|
Main.logger.error(e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
@@ -79,7 +79,7 @@ public class FTPClientConnector {
|
|||||||
ftpClient.disconnect();
|
ftpClient.disconnect();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
Main.logger.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,24 @@
|
|||||||
package io.github.lumijiez;
|
package io.github.lumijiez;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
public static Logger logger = LogManager.getLogger(Main.class);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Thread brokerThread = new Thread(BrokerConnector::connect);
|
Thread brokerThread = new Thread(BrokerConnector::connect);
|
||||||
brokerThread.start();
|
brokerThread.start();
|
||||||
|
|
||||||
Thread ftpThread = new Thread(FTPClientConnector::new);
|
Thread ftpThread = new Thread(FTPProducer::new);
|
||||||
ftpThread.start();
|
ftpThread.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
brokerThread.join();
|
brokerThread.join();
|
||||||
ftpThread.join();
|
ftpThread.join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
Main.logger.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user