Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using Java, In this mini-project, you are asked to develop a simple file transfe

ID: 3834324 • Letter: U

Question

using Java,

In this mini-project, you are asked to develop a simple file transfer protocol. The protocol is based on client-server architecture. The client will connect to the server, sending user name and password for authentication (No encryption needed. Sending over plain TCP connection is fine). After passing authentication, the client will be able to download/upload files, list the files in the current directory at the server, and change the current directory to other directory at the server. After the client sends a request, the server should respond with some information to indicate if the request is processed or not. The server should also be able to serve another client after the current client logout.

You are required implement BOTH client and server sides. And your protocol should be able to transfer BOTH ASCII and binary files. You may refer to the FTP protocol for possible design options and typical commands. But no need to strictly follow or fully implement the FTP protocol. You can design your own protocol.

Explanation / Answer

Simple file transfer protocol in java code is given below :

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpCreateDirectory {
public static void main(String[] args) throws IOException {
FTPClient ftpClient = new FTPClient();
boolean result;
try {
// Connect to the localhost
ftpClient.connect("localhost");

// login to ftp server
result = ftpClient.login("admin", "admin");

if (result == true) {
System.out.println("Logged in Successfully !");
} else {
System.out.println("Login Fail !");
return;
}
// Changing the working directory
result = ftpClient.changeWorkingDirectory("/FtpNew");

if (result == true) {
System.out.println("New Directory is created on Ftp Server!");
} else {
System.out.println("Unable to create new directory!");
}
} catch (FTPConnectionClosedException exp) {
exp.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (FTPConnectionClosedException exe) {
System.out.println(exe);
}
}
}
}

output :

Logged in Successfully.
New Directory is created on Ftp Server.

uploading file using simple ftp


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

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FTPFileUpload {
public static void main(String[] args) throws IOException {
FTPClient ftpclient = new FTPClient();
FileInputStream fis = null;
boolean result;
String ftpServerAddress = "localhost";
String userName = "admin";
String password = "admin";

try {
ftpclient.connect(ftpServerAddress);
result = ftpclient.login(userName, password);

if (result == true) {
System.out.println("Logged in Successfully !");
} else {
System.out.println("Login Fail!");
return;
}
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

ftpclient.changeWorkingDirectory("/");

File file = new File("D:/File.doc");
String testName = file.getName();
fis = new FileInputStream(file);

// Upload file to the ftp server
result = ftpclient.storeFile(testName, fis);

if (result == true) {
System.out.println("File is uploaded successfully");
} else {
System.out.println("File uploading failed");
}
ftpclient.logout();
} catch (FTPConnectionClosedException e) {
e.printStackTrace();
} finally {
try {
ftpclient.disconnect();
} catch (FTPConnectionClosedException e) {
System.out.println(e);
}
}
}
}

downloading file using simple ftp

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpFileDownload {
public static void main(String[] args) throws IOException {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
boolean result;
try {
// Connect to the localhost
ftpClient.connect("localhost");

// login to ftp server
result = ftpClient.login("", "");
if (result == true) {
System.out.println("Successfully logged in!");
} else {
System.out.println("Login Fail!");
return;
}
String fileName = "uploadfile.txt";
fos = new FileOutputStream(fileName);

// Download file from the ftp server
result = ftpClient.retrieveFile(fileName, fos);

if (result == true) {
System.out.println("File downloaded successfully !");
} else {
System.out.println("File downloading failed !");
}
ftpClient.logout();
} catch (FTPConnectionClosedException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
ftpClient.disconnect();
} catch (FTPConnectionClosedException e) {
System.err.println(e);
}
}
}
}