From d7e8c3fbfec6817d2470f8c157dd92914a93c220 Mon Sep 17 00:00:00 2001 From: DomAtTheShack Date: Wed, 7 Jun 2023 23:02:24 -0400 Subject: [PATCH] FTP Update but WORK ON commandWriter please --- src/FTPClientJ.java | 127 +++++++++++++++++++++++++++++----------- src/FileBrowserGUI.java | 7 ++- 2 files changed, 97 insertions(+), 37 deletions(-) diff --git a/src/FTPClientJ.java b/src/FTPClientJ.java index 050015f..cd37ae7 100644 --- a/src/FTPClientJ.java +++ b/src/FTPClientJ.java @@ -1,11 +1,14 @@ +import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTP; +import javax.swing.*; import java.io.*; public class FTPClientJ { private static FTPClient ftpClient = new FTPClient(); + private static SwingWorker uploadTask; public static boolean isConnected(){ @@ -15,7 +18,7 @@ public static boolean isConnected(){ return false; } } - public static boolean connect(String server,String user, String pass,int port) throws IOException { + public static boolean connect(String server, String user, String pass, int port) throws IOException { try { ftpClient.connect(server, port); int replyCode = ftpClient.getReplyCode(); @@ -28,8 +31,16 @@ public static boolean connect(String server,String user, String pass,int port) t Main.gui.addConsoleText("Could not log in to the FTP server."); return false; } + + // Add protocol command listener to print out the commands and responses + StringWriter stringWriter = new StringWriter(); + PrintWriter commandWriter = new PrintWriter(stringWriter); + + // Add protocol command listener to the custom PrintWriter + ftpClient.addProtocolCommandListener(new PrintCommandListener(commandWriter, true)); + return true; - }catch (Exception e){ + } catch (Exception e) { Main.gui.addConsoleText(e.toString()); return false; } @@ -43,50 +54,96 @@ public static void disconnect() throws IOException { } public static boolean FTPUpFile(String sendFile, String remoteDir ) { - FileInputStream inputStream = null; + FileInputStream inputStream; try { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); - File localFile = new File(sendFile); - inputStream = new FileInputStream(localFile); + File localFile = new File(sendFile); String remoteFileName = localFile.getName(); + ftpClient.changeWorkingDirectory("/dominichann/"); + + // Create and start the upload task + uploadTask = createUploadTask(localFile, remoteFileName); + uploadTask.execute(); + return true; + } catch (Exception ex) { + Main.gui.addConsoleText("Error: " + ex.getMessage()); + return false; + } + } + private static SwingWorker createUploadTask(File localFile, String remoteFileName) { + return new SwingWorker() { + @Override + protected Boolean doInBackground() throws Exception { + FileInputStream inputStream = new FileInputStream(localFile); + long fileSize = localFile.length(); + long uploadedBytes = 0; + int percentCompleted = 0; + byte[] buffer = new byte[4096]; + + final int POLL_INTERVAL = 500; // Polling interval in milliseconds + long lastProgressUpdateTime = System.currentTimeMillis(); + + try (OutputStream outputStream = ftpClient.storeFileStream(remoteFileName)) { + if (outputStream == null) { + Main.gui.addConsoleText("Failed to obtain the output stream for file transfer."); + return false; + } + + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + uploadedBytes += bytesRead; + int newPercentCompleted = (int) ((uploadedBytes * 100) / fileSize); + if (newPercentCompleted > percentCompleted) { + percentCompleted = newPercentCompleted; + publish(percentCompleted); + } - // Upload the file to the server - OutputStream outputStream = ftpClient.storeFileStream(remoteFileName); - byte[] buffer = new byte[4096]; - long fileSize = localFile.length(); - long uploadedBytes = 0; - int bytesRead; - int percentCompleted = 0; - - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - uploadedBytes += bytesRead; - int newPercentCompleted = (int) ((uploadedBytes * 100) / fileSize); - if (newPercentCompleted > percentCompleted) { - percentCompleted = newPercentCompleted; - System.out.println(percentCompleted); - Main.gui.setLoadingBar(percentCompleted); + // Check if enough time has passed since the last progress update + long currentTime = System.currentTimeMillis(); + if (currentTime - lastProgressUpdateTime >= POLL_INTERVAL) { + lastProgressUpdateTime = currentTime; + publish(percentCompleted); + } + } } - } - inputStream.close(); - outputStream.close(); + inputStream.close(); - boolean completed = ftpClient.completePendingCommand(); - if (completed) { - Main.gui.addConsoleText("File uploaded successfully."); - return true; - } else { - Main.gui.addConsoleText("Failed to upload the file."); - return false; + boolean completed = ftpClient.completePendingCommand(); + if (completed) { + Main.gui.addConsoleText("File uploaded successfully."); + return true; + } else { + Main.gui.addConsoleText("Failed to upload the file."); + return false; + } } - } catch (Exception ex) { - Main.gui.addConsoleText("Error: " + ex.getMessage()); - return false; + + @Override + protected void process(java.util.List chunks) { + // Update the GUI loading bar with the latest progress value + int progress = chunks.get(chunks.size() - 1); + Main.gui.setLoadingBar(progress); } - } + + @Override + protected void done() { + try { + boolean success = get(); + if (success) { + Main.gui.addConsoleText("File upload completed."); + } else { + Main.gui.addConsoleText("File upload failed."); + } + } catch (Exception ex) { + Main.gui.addConsoleText("Error: " + ex.getMessage()); + } + } + }; + } public static void FTPUpFolder(String[] args) { String server = "ftp.example.com"; int port = 21; diff --git a/src/FileBrowserGUI.java b/src/FileBrowserGUI.java index 47f8774..2f36e9a 100644 --- a/src/FileBrowserGUI.java +++ b/src/FileBrowserGUI.java @@ -15,12 +15,12 @@ public class FileBrowserGUI extends JFrame { private JList fileList; private DefaultListModel listModel; private JLabel selectedFileLabel; - private JProgressBar loadingBar; + public JProgressBar loadingBar; private JTextField textField1; private JTextField textField2; private JPasswordField textField3; private JTextField textField4; - private JTextArea console; + public JTextArea console; private File currentDirectory; @@ -293,4 +293,7 @@ public void setLoadingBarText(String str) { public void addConsoleText(String text) { console.append(text + "\n"); } + public void appendTextToConsole(String text) { + addConsoleText(text); + } }