Skip to content

Commit

Permalink
FTP Update but percent needs work
Browse files Browse the repository at this point in the history
  • Loading branch information
DomAtTheShack committed Jun 7, 2023
1 parent 6013f6c commit f8a166f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 53 deletions.
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.
82 changes: 36 additions & 46 deletions src/FTPClientJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
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;
import java.io.*;

public class FTPClientJ {
private static FTPClient ftpClient = new FTPClient();
Expand Down Expand Up @@ -44,59 +41,52 @@ public static void disconnect() throws IOException {
Main.gui.addConsoleText(e.toString());
}
}
public static boolean FTPUpFile(String sendFile, String remoteDir, boolean error ) {

FileInputStream fileInputStream = null;
public static boolean FTPUpFile(String sendFile, String remoteDir ) {

FileInputStream inputStream = null;
try {
if(!ftpClient.isConnected()){
Main.gui.addConsoleText("Not Connected");
return false;
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();

File localFile = new File(sendFile);
long fileSize = localFile.length();
fileInputStream = new FileInputStream(localFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

byte[] buffer = new byte[1024];
int bytesRead;
long totalBytesUploaded = 0;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
ftpClient.storeFile(remoteDir, bufferedInputStream);
Main.gui.addConsoleText(ftpClient.getReplyString());
inputStream = new FileInputStream(localFile);

totalBytesUploaded += bytesRead;
String remoteFileName = localFile.getName();

int percentComplete = (int) ((totalBytesUploaded * 100) / fileSize);
Main.gui.setLoadingBar(percentComplete);
// 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);
}
}
Main.gui.setLoadingBarText("Done!");

bufferedInputStream.close();
fileInputStream.close();
inputStream.close();
outputStream.close();

System.out.println("File upload completed successfully.");
return true;
} catch (Exception e) {
Main.gui.addConsoleText(e.toString());
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
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;
}
}
return false;
}
public static void FTPUpFolder(String[] args) {
String server = "ftp.example.com";
int port = 21;
Expand Down
31 changes: 24 additions & 7 deletions src/FileBrowserGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public FileBrowserGUI() {
JPanel rightPanel = new JPanel(new BorderLayout()); // Panel for the right side components

JPanel buttonsPanel = new JPanel(new GridLayout(1, 3)); // Panel for buttons
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button1 = new JButton("Send File/Folder");
JButton button2 = new JButton("Receive File/Folder");
JButton button3 = new JButton("Button 3");

buttonsPanel.add(button3);
Expand All @@ -117,9 +117,6 @@ public FileBrowserGUI() {
directoryPanel.add(directoryLabel);
directoryPanel.add(ftpDirectoryScrollPane);

JPanel spacePanel = new JPanel(); // Panel for white space
spacePanel.setPreferredSize(new Dimension(10, 50)); // Adjust the preferred height as needed

loadingBar = new JProgressBar();
loadingBar.setStringPainted(true);

Expand Down Expand Up @@ -207,7 +204,27 @@ public void actionPerformed(ActionEvent e) {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FTPClientJ.FTPUpFile("/home/dominic/Downloads/Human Nature.mp3","Human.mp3",false);
setLoadingBar(0);
int selectedFileIndex = fileList.getSelectedIndex();
if(selectedFileIndex==-1){
addConsoleText("No File Selected");
return;
}
File[] files = currentDirectory.listFiles();
String selectedFile = listModel.getElementAt(selectedFileIndex);
File file = new File(currentDirectory, selectedFile);
if(file.isDirectory()&&selectedFileIndex!=0){
//FTPClientJ.FTPUpFolder();
}else if(selectedFileIndex!=0){
FTPClientJ.FTPUpFile(file.getAbsolutePath(), selectedFile);
}else {

}
if(!FTPClientJ.isConnected()){
connectButton.setEnabled(true);
disconnectButton.setEnabled(false);
addConsoleText("Disconnected from "+ textField1.getText());
}
}
});

Expand All @@ -231,7 +248,7 @@ public void actionPerformed(ActionEvent e) {
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// performButton3Action();
System.out.println(FTPClientJ.isConnected());
}
});
}
Expand Down

0 comments on commit f8a166f

Please sign in to comment.