Skip to content

Commit

Permalink
Update FTP and Main
Browse files Browse the repository at this point in the history
  • Loading branch information
DomAtTheShack committed Jun 4, 2023
1 parent 73e2ad2 commit 91b824a
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 28 deletions.
Binary file modified out/production/Dom-s-FTP-Client/FTPClientJ.class
Binary file not shown.
Binary file modified out/production/Dom-s-FTP-Client/FileBrowserGUI$3.class
Binary file not shown.
Binary file modified out/production/Dom-s-FTP-Client/FileBrowserGUI$4.class
Binary file not shown.
Binary file modified out/production/Dom-s-FTP-Client/FileBrowserGUI$5.class
Binary file not shown.
Binary file modified out/production/Dom-s-FTP-Client/FileBrowserGUI$6.class
Binary file not shown.
Binary file modified out/production/Dom-s-FTP-Client/FileBrowserGUI$7.class
Binary file not shown.
Binary file removed out/production/Dom-s-FTP-Client/FileBrowserGUI$8.class
Binary file not shown.
Binary file modified out/production/Dom-s-FTP-Client/FileBrowserGUI.class
Binary file not shown.
175 changes: 156 additions & 19 deletions src/FTPClientJ.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,184 @@
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FTPClientJ {

public static void FTPClient(String sendFile, String RemoteDir, Boolean send) {
public static boolean FTPUpFile(String sendFile, String remoteDir) {
String server = "192.168.5.118";
int port = 21;
String username = "dominichann";
String password = "Hidom@123";
String localFilePath = "C:\\users\\dooli\\Downloads\\rename.bat";
String remoteDirectory = "/home/dominichann/";
String localFilePath = "C:\\Users\\dooli\\Downloads\\FileBrowserGUI.java";
String remoteFilePath = "/home/dominichann/";

FTPClient ftpClient = new FTPClient();
FileInputStream fileInputStream = null;

try {
ftpClient.connect(server, port);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("FTP server refused connection.");
return false;
}

boolean loggedIn = ftpClient.login(username, password);
if (!loggedIn) {
System.out.println("Could not log in to the FTP server.");
return false;
}

ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftpClient.enterLocalPassiveMode();

if (loggedIn) {
System.out.println("Logged in to FTP server.");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
File localFile = new File(localFilePath);
long fileSize = localFile.length();
fileInputStream = new FileInputStream(localFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

File localFile = new File(localFilePath);
FileInputStream inputStream = new FileInputStream(localFile);
byte[] buffer = new byte[1024];
int bytesRead;
long totalBytesUploaded = 0;

boolean uploaded = ftpClient.storeFile(remoteDirectory + localFile.getName(), inputStream);
inputStream.close();
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
ftpClient.storeFile(remoteFilePath, bufferedInputStream);
totalBytesUploaded += bytesRead;

if (uploaded) {
System.out.println("File uploaded successfully.");
} else {
System.out.println("Failed to upload the file.");
int percentComplete = (int) ((totalBytesUploaded * 100) / fileSize);
Main.loadingBar.setLoadingBar(percentComplete);
}
Main.loadingBar.setLoadingBarText("Done!");

bufferedInputStream.close();
fileInputStream.close();
ftpClient.logout();
ftpClient.disconnect();

System.out.println("File upload completed successfully.");
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public static void FTPUpFolder(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "your-username";
String password = "your-password";
String localFolderPath = "path/to/local/folder";
String remoteFolderPath = "/path/to/remote/folder";

FTPClient ftpClient = new FTPClient();

try {
ftpClient.connect(server, port);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("FTP server refused connection.");
return;
}

boolean loggedIn = ftpClient.login(username, password);
if (!loggedIn) {
System.out.println("Could not log in to the FTP server.");
return;
}

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();

long totalBytes = getFolderSize(new File(localFolderPath));
long uploadedBytes = 0;

uploadFolder(ftpClient, localFolderPath, remoteFolderPath, totalBytes, uploadedBytes);

ftpClient.logout();
ftpClient.disconnect();

System.out.println("Folder upload completed successfully.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

private static void uploadFolder(FTPClient ftpClient, String localFolderPath, String remoteFolderPath, long totalBytes, long uploadedBytes) throws IOException {
File localFolder = new File(localFolderPath);
File[] files = localFolder.listFiles();

if (files != null) {
for (File file : files) {
String remoteFilePath = remoteFolderPath + "/" + file.getName();

if (file.isFile()) {
uploadFile(ftpClient, file.getAbsolutePath(), remoteFilePath, totalBytes, uploadedBytes);
} else if (file.isDirectory()) {
ftpClient.makeDirectory(remoteFilePath);
ftpClient.changeWorkingDirectory(remoteFilePath);
uploadFolder(ftpClient, file.getAbsolutePath(), remoteFilePath, totalBytes, uploadedBytes);
ftpClient.changeToParentDirectory();
}
}
}
}

private static void uploadFile(FTPClient ftpClient, String localFilePath, String remoteFilePath, long totalBytes, long uploadedBytes) throws IOException {
File localFile = new File(localFilePath);
FileInputStream fileInputStream = new FileInputStream(localFile);

ftpClient.storeFile(remoteFilePath, fileInputStream);

uploadedBytes += localFile.length();
int percentComplete = (int) ((uploadedBytes * 100) / totalBytes);
System.out.println("Progress: " + percentComplete + "%");

fileInputStream.close();
}

private static long getFolderSize(File folder) {
long size = 0;

if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
size += file.length();
} else if (file.isDirectory()) {
size += getFolderSize(file);
}
}
}
} else {
System.out.println("Failed to log in to FTP server.");
size = folder.length();
}
} catch (IOException e) {
e.printStackTrace();

return size;
}
}
}
18 changes: 10 additions & 8 deletions src/FileBrowserGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public void actionPerformed(ActionEvent e) {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Button 1 clicked");
if(FTPClientJ.FTPUpFile("C:\\Users\\dooli\\Downloads\\FileBrowserGUI.java","/home/dominichann/")) {
JOptionPane.showMessageDialog(null, "Sent!");
}
}
});
buttonsSection.add(button1);
Expand Down Expand Up @@ -173,6 +175,7 @@ private void navigateBack() {
if (backStack.isEmpty()) {
backButton.setEnabled(false);
}

} else {
// Navigate to the parent directory
File parentDirectory = currentDirectory.getParentFile();
Expand Down Expand Up @@ -227,12 +230,11 @@ public static void main(String[] args) {
} catch (Exception e) {
e.printStackTrace();
}

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FileBrowserGUI();
}
});
}
public void setLoadingBar(int x){
loadingBar.setValue(x);
}
public void setLoadingBarText(String str){
loadingBar.setString(str);
}
}
11 changes: 10 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import org.apache.commons.net.ftp.FTP;

import javax.swing.*;

public class Main {
public static FileBrowserGUI loadingBar = new FileBrowserGUI();

public static void main(String[] args){
//FTPClientJ.FTPClient();
// Set native look and feel for better integration with the operating system
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 comments on commit 91b824a

Please sign in to comment.