For best results, we recommend setting up an Ubuntu Server virtual machine (VM) as your FTP and web server. The instructions below assume you are using a VM, but they can be adapted for other environments as needed.
This project implements a custom FTP client in Java that automatically uploads an HTML file from your workstation to a remote Ubuntu server without using any pre-existing FTP client library or tool. The purpose of this tool is to provide a deeper understanding of the File Transfer Protocol (FTP) at the socket level, tailored to a typical server setup.
FTP (File Transfer Protocol) is commonly used for transferring files between a client and a server. In this project:
- An FTP daemon (vsftpd) is set up on a remote Ubuntu virtual server.
- An Apache HTTP server serves the uploaded HTML file from the
/home/$USER/web/directory. - A custom-written Java FTP client:
- Monitors a specific HTML file for changes on the local machine.
- Automatically uploads the updated file to the server using raw FTP commands.
- Ensures the file is accessible via the Apache server at
http://your_vip_address/filename.html.
Unlike typical protocols:
- FTP establishes a control connection from the client to the server on port 21.
- For each data transfer, the client and server negotiate a separate data connection. In passive mode (PASV), which this client implements, the server opens a listening socket, and the client connects back to that socket to transfer data. This approach is generally more firewall-friendly for clients.
This project handles both control and data connections manually using Java Socket objects.
- Detects changes to a specified local HTML file using polling.
- Connects to an FTP server on port 21 using raw TCP sockets.
- Sends FTP commands manually (e.g.,
USER,PASS,TYPE,PASV,STOR,QUIT). - Parses and handles server responses based on FTP status codes.
- Implements passive mode (PASV) for data connections.
- Uploads files in binary mode (TYPE I) suitable for HTML.
- Logs connection status, commands sent, and server responses.
- Demonstrates file synchronization with a web server (via Apache serving from
/home/$USER/web/).
-
Install Apache:
sudo apt update sudo apt install apache2
-
Install vsftpd:
sudo apt install vsftpd
-
Configure vsftpd for write access to the Apache directory (
/home/$USER/web/):- Edit
/etc/vsftpd.conf:Ensure the following (and potentially other) lines are set:sudo nano /etc/vsftpd.conf
write_enable=YES local_enable=YES chroot_local_user=YES allow_writeable_chroot=YES - Restart vsftpd:
sudo systemctl restart vsftpd
- Edit
-
Configure Apache to serve from
/home/$USER/web/:- You've already created
/etc/apache2/sites-available/mywebsite.confwith the following content (replace$USERwith your actual username andyour_vm_ip_address):<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /home/$USER/web ServerName your_vm_ip_address <Directory /home/$USER/web> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog /var/log/apache2/error.log CustomLog /var/log/apache2/access.log combined </VirtualHost> - Enable the site and reload Apache:
sudo a2ensite mywebsite.conf sudo a2dissite 000-default.conf sudo systemctl reload apache2
- You've already created
-
Ensure the FTP user (likely your regular VM user
plm) has write permissions to/home/$USER/web/:sudo chown $USER:$USER /home/$USER/web
-
Compile and run the custom Java FTP uploader (
Main.javaandFTPClient.java). -
Provide the following as command-line arguments:
- The path of the local HTML file to monitor (e.g.,
latency.html). - The IP address of your Ubuntu server VM (e.g.,
10.39.164.84). - Optionally, the polling interval in milliseconds (default is 5000 ms).
Example execution:
java Main latency.html 10.39.164.84 5000
- The path of the local HTML file to monitor (e.g.,
-
The program will prompt you to enter your FTP username (
plm) and password. -
The program polls the local HTML file for changes at the specified interval.
-
Upon detecting a change, it establishes an FTP connection to your server and uploads the updated file to the
/home/$USER/web/directory.
-
Navigate to the file in your browser using your VM's IP address and the filename:
http://your_vm_ip_address/your_html_file_name.htmlFor example:
http://10.39.164.84/latency.html -
Edit and save the
latency.htmlfile on your workstation. -
The Java program detects the change and re-uploads the
latency.htmlfile to the/home/$USER/web/directory on your server. -
Refresh the browser to see the updated version of the file.
├── Main.java
├── FTPClient.java
├── latency.html (example watched file)
├── README.md- RFC 959: File Transfer Protocol (FTP)
- Java
java.net.Socketdocumentation - FTP command documentation
vsftpd.confdocumentation- Apache virtual host configuration
As discussed, you might need to adjust file permissions on the server after the upload so that Apache can read the newly uploaded file. The simplest way for this exercise is to manually run the following command on the server after an upload:
sudo chown www-data:www-data /home/$USER/web/your_html_file_name.html
sudo chmod 644 /home/$USER/web/your_html_file_name.html