If I run the code shown below from my local machine, where the FTP server is installed, then everything works correctly. However, if I try to run it from some other machine, then the message "Cannot connect to the FTP server!" appears.

Settings of my FTP Server are shown in the attached image.

I have absolutely no idea about this problem. Please, help me with some advise. What could be wrong and what should I do to fix this problem?

private void openFile(String directoryPath, String fileName) {
        FTPClient client = new FTPClient();
        FileOutputStream fos = null;
        File file = new File(fileName);
        try {
            fos = new FileOutputStream(fileName);

            boolean b = client.retrieveFile(directoryPath + fileName, fos);
            if (!b) {
                JOptionPane.showMessageDialog(null, "Cannot access the file " + fileName + "!", "Warning", JOptionPane.WARNING_MESSAGE);
            } else {
                try {
                catch (IOException ioe) {
                    JOptionPane.showMessageDialog(null, "Cannot open the file " + fileName + "!", "Warning", JOptionPane.WARNING_MESSAGE);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Cannot connect to the FTP server!", "Warning", JOptionPane.WARNING_MESSAGE);
        } finally {
            try {
                if (fos != null) {
                if (file.exists()) {
            } catch (IOException e) {

Edited by LianaN: n/a

Attachments ftp_pic1.png 28.04 KB
6 Years
Discussion Span
Last Post by LianaN

Show us the entire IOException stacktrace instead of just the error message. Also, if it works locally, it seems more of a networking issue. Are you able to "ping" to that host? Are you able to "telnet" to that host on the given port(21)?


Attached you could find the "ping" output that was received on the remote machine. So, it seems that port 21 is closed, is it? If this is the point, then how could I open it for a remote access (the most secured way)?

If this output is not enough, then later I could post IOException stacktrace.

Attachments 11.JPG 31.02 KB

> So, it seems that port 21 is closed, is it?

No, that implies that the host "" is unreachable. This seems to be more of a networking issue and nothing to do with Java as such. How is your other host configured? Is it on the same network? If that IP given to you by your hosting provider?


Well, this IP is given to me by a hosting provider. In fact, this is what I could see from here or from "ipconfig". The other remote computer is just connected to the internet. These two computers are not connected inside some local network. Just both are connected to the internet...

But let me notice that I can remotely connect to the MySQL database placed in "". So, it means that FTP server is unreachable, and at the same time MySQL can be reached. How could it be?

Maybe it could help: I'm using FileZilla to setup FTP Server. When I run FileZilla, then the message shown in attachements is opened. Should I change settings there?

Edited by LianaN: n/a

Attachments server.png 5.09 KB

It's also a possibility that "ping" is disabled on that machine which leads to dropped packets hence the timeouts. Another thing you can try out is "telnet". Try the command telnet <host> <port> for your MySQL and FTP ports. If it works for MySQL and not for FTP server, we might have something there. In any case, post the entire IOException stacktrace.


The problem was with the firewall...I turned it off and the program started working. Thank you!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.