Thanks to visit codestin.com
Credit goes to github.com

Skip to content

This is an ftp client written in java

PLM-18/FTPSERVER

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FTP AutoUploader

Recommended Environment

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.

Overview

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.

Key FTP Behavior

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.

Features

  • 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/).

Setup Instructions

1. On the Server (Ubuntu VM)

  1. Install Apache:

    sudo apt update
    sudo apt install apache2
  2. Install vsftpd:

    sudo apt install vsftpd
  3. Configure vsftpd for write access to the Apache directory (/home/$USER/web/):

    • Edit /etc/vsftpd.conf:
      sudo nano /etc/vsftpd.conf
      Ensure the following (and potentially other) lines are set:
      write_enable=YES
      local_enable=YES
      chroot_local_user=YES
      allow_writeable_chroot=YES
      
    • Restart vsftpd:
      sudo systemctl restart vsftpd
  4. Configure Apache to serve from /home/$USER/web/:

    • You've already created /etc/apache2/sites-available/mywebsite.conf with the following content (replace $USER with your actual username and your_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
  5. Ensure the FTP user (likely your regular VM user plm) has write permissions to /home/$USER/web/:

    sudo chown $USER:$USER /home/$USER/web

2. On the Client (Workstation)

  1. Compile and run the custom Java FTP uploader (Main.java and FTPClient.java).

  2. 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
  3. The program will prompt you to enter your FTP username (plm) and password.

  4. The program polls the local HTML file for changes at the specified interval.

  5. Upon detecting a change, it establishes an FTP connection to your server and uploads the updated file to the /home/$USER/web/ directory.

Demonstration

  1. 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.html
    

    For example: http://10.39.164.84/latency.html

  2. Edit and save the latency.html file on your workstation.

  3. The Java program detects the change and re-uploads the latency.html file to the /home/$USER/web/ directory on your server.

  4. Refresh the browser to see the updated version of the file.

Project Structure

├── Main.java
├── FTPClient.java
├── latency.html (example watched file)
├── README.md

References

Addressing File Permissions After Upload

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

About

This is an ftp client written in java

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published