-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73e2ad2
commit 91b824a
Showing
11 changed files
with
176 additions
and
28 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |