Transfer a file from a local machine to an Ubuntu virtual machine using the FTP protocol through a Java program

17 Views Asked by At
package org.example;

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

public class FTPFileUploader {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        String server = "192.168.142.128";
        int port = 21; // Port FTP par défaut
        String user = "youssef";
        String pass = "youssef";
        String localFilePath = "C:/Users/DeLL/Desktop/wewe.txt";
        String remoteDirectory = "/home/youssef/Desktop/you";

        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;

        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();

            File localFile = new File(localFilePath);
            fis = new FileInputStream(localFile);

            String remoteFileName = localFile.getName();
            ftpClient.storeFile(remoteDirectory + "/" + remoteFileName, fis);
            long endTime = System.currentTimeMillis();
            long scpTime = endTime - startTime;
            System.out.println("la durer de transformation de fichier : " + scpTime+" ms");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

java.net.ConnectException: Connection timed out: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:554) at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) at java.base/java.net.Socket.connect(Socket.java:633) at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:866) at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:1053) at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:3816) at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:3846) at org.example.FTPFileUploader.main(FTPFileUploader.java:31)

I have checked network connectivity. I have verified network configuration settings. I have checked the firewalls on my Ubuntu virtual machine.

Why did the file not transfer to the Ubuntu virtual machine?

0

There are 0 best solutions below