Skip to content

Commit

Permalink
FTP Update but WORK ON commandWriter please
Browse files Browse the repository at this point in the history
  • Loading branch information
DomAtTheShack committed Jun 8, 2023
1 parent f8a166f commit d7e8c3f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 37 deletions.
127 changes: 92 additions & 35 deletions src/FTPClientJ.java
Original file line number Diff line number Diff line change
@@ -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<Boolean, Integer> uploadTask;


public static boolean isConnected(){
Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -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<Boolean, Integer> createUploadTask(File localFile, String remoteFileName) {
return new SwingWorker<Boolean, Integer>() {
@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<Integer> 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;
Expand Down
7 changes: 5 additions & 2 deletions src/FileBrowserGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class FileBrowserGUI extends JFrame {
private JList<String> fileList;
private DefaultListModel<String> 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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit d7e8c3f

Please sign in to comment.