5

I'm doing a ftp file download from a server with help from an Apache library (commons.net ver 3.2). The download was fine and I received all the file that I needed and saved them in a folder. The problem that I have is with the timeouts because when the connection is interrupted while I'm downloading I need an error message that shows me that the connection has been lost, but I have found difficulties doing this, I have searched countless forums including this one and I have tried many ways to solve this but no one have result yet! The code that I have is as follows:

public void doSomething(String ip, int port, String user, String pass, String server, String remotePath, String localPath) {
  int tenseconds = 10 * 1000;
  int thirtyseconds = 30 * 3000;
  Socket s4 = new Socket();
  java.net.InetSocketAddress adr = new java.net.InetSocketAddress("213.0.17.234", 21);
  s4.connect(adr, thirtyseconds);
  FTPClient client = new FTPClient();

  org.apache.commons.vfs2.FileSystemOptions fileSystemOptions = null;
  String key = FtpFileSystemConfigBuilder.getInstance().getEntryParser(fileSystemOptions);

  try {

    client.setConnectTimeout(tenseconds);
    client.setDefaultTimeout(thirtyseconds);
    client.connect(ip, port);
    client.setSoTimeout(thirtyseconds);

    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
      throw new FileSystemException("vfs.provider.ftp/connect-rejected.error");
    }
    client.enterLocalPassiveMode();
    boolean login = client.login(user, pass);

    URL url = new URL("ftp://" + user + ":" + pass + "@" + server + remotePath + ";type=i");
    URLConnection urlc = url.openConnection();
    urlc.setConnectTimeout(1000);
    InputStream is = urlc.getInputStream();
    BufferedWriter bw = new BufferedWriter(new FileWriter(localPath));

    int c;
    client.setSoTimeout(tenseconds);

    client.setControlKeepAliveTimeout(10000);

    while ((c = is.read()) != -1) {
      urlc.getConnectTimeout();

      bw.write(c);

    }

    long t2 = System.currentTimeMillis();

    System.out.println(t2);

    JOptionPane.showMessageDialog(null, "se cargo el primer fichero!", "información", JOptionPane.INFORMATION_MESSAGE);

    if (login) {

      FTPFile[] files = client.listFiles();
      for (FTPFile file : files) {

        if (file.getType() == FTPFile.DIRECTORY_TYPE) {

          System.out.println("ftp file: " + file.getName() + ";" + FileUtils.byteCountToDisplaySize(file.getSize()));

        } else if (file.getType() == FTPFile.FILE_TYPE) {
          System.out.println("ftp file: " + file.getName() + ";" + FileUtils.byteCountToDisplaySize(file.getSize()));

        }

      }

      is.close();
      bw.close();

      client.setSoTimeout(tenseconds);
      client.logout();
      client.disconnect();

    }

  } catch (IOException e) {
    StringWriter sw0 = new StringWriter();
    PrintWriter p0 = new PrintWriter(sw0, true);
    e.printStackTrace(p0);

    System.out.println("connection probably lost");
    JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
  }
}

I have tried all the things a could find a have read that the setdefaulttimeout is used to activate all the timeouts, connectiontiomeout is used to wait for connections and getsotimeouts is used when we are downoading a file but it doesn't work I have tried give it 5 seconds so it will not download the file but it doesn't work, I have read that there are some issues whit connectiontimeouts and that we should use socketfactory, so I creaded a socket factory as well and I tried but it didn't work and I have reach a point where I'm a little bit desperade so I´m asking you for help I all so tried client.setControlKeepAliveTimeout(10000); to establish an alive timeout but it didn't work! :(

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
user2144091
  • 21
  • 1
  • 2

0 Answers0