Hi!

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 {
            client.connect(this.url);
            client.login(this.login,this.password);
          
            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 {
                    Desktop.getDesktop().open(file);
                }
                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) {
                    fos.close();
                }
                client.disconnect();
                if (file.exists()) {
                   file.deleteOnExit();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  }

Recommended Answers

All 6 Replies

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.

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

No, that implies that the host "109.110.12.236" 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 "109.110.12.236". 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?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.