Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Check each 5 minute and ftp out

Is anyone know how to write a code for checking a specific folder in each 5 minute and ftp all file(s) if it cantains to a hard coded host. Thanks for your help.

P.S. Please be informed that I need to ftp in non-passive and ascii mode.
0
leungpingkei
Asked:
leungpingkei
  • 5
  • 3
1 Solution
 
yongsingCommented:
The following is an auto FTP class that ftp files in a given directory to an FTP server every 5 minutes. Example of use:

// Upload files in C:\temp every 5 minutes.
AutoFTP autoFTP = new AutoFTP("hostname", "ftpuserid", 'ftppassword", "C:/temp");


import java.io.*;
import java.util.*;
import java.net.*;
import sun.net.ftp.FtpClient;
import sun.net.TelnetOutputStream;

public class AutoFTP implements Runnable {

 /** The host name of the FTP server. */
 private String host;;

 /** The user ID to login to the FTP server. */
 private String userID;;

 /** The password to login to the FTP server. */
 private String password;;

 /** The directory that contains the files to FTP. */
 private String directory;;

 public AutoFTP(String host, String userID, String password, String directory) {
   this.host = host;
   this.userID = userID;
   this.password = password;
   this.directory = directory;
   Thread thread = new Thread(this);
   thread.start();
 }

 public void run() {
   while (true) {
     try {
       FtpClient ftpClient = new FtpClient();
       ftpClient.openServer(host); // connect to FTP server
       ftpClient.login(userID, password); // login
       File[] files = new File(directory); // get files
       for (int i = 0; i < files.length; i++) {
         // Upload if it is a file.
         if (files[i].isFile()) {
           TelnetOutputStream out = ftpClient.put(files[i].getName());
           FileInputStream in = new FileInputStream(files[i]);
           int c = 0;
           while ((c = in.read()) != -1 ) {
             out.write(c);
           }
           in.close();
           out.close();
         }
       ftpClient.closeServer();
     } catch (Exception exception) {
       exception.printStackTrace();
     }

     // Sleep 5 minutes.
     try {
        Thread.sleep(5*60*1000);
     } catch (InterruptedException e) {}
   }
 }

}
0
 
leungpingkeiAuthor Commented:
To : yongsing

I tried to compile your code and a error return but I don't know what is it ? Please help.

c:\AutoFTP.java:37: incompatible types
found   : java.io.File
required: java.io.File[]
      File[] files = new java.io.File(directory); // get files
                     ^
1 error


0
 
yongsingCommented:
Sorry for the mistake. I have changed the error line:

import java.io.*;
import java.util.*;
import java.net.*;
import sun.net.ftp.FtpClient;
import sun.net.TelnetOutputStream;

public class AutoFTP implements Runnable {

/** The host name of the FTP server. */
private String host;;

/** The user ID to login to the FTP server. */
private String userID;;

/** The password to login to the FTP server. */
private String password;;

/** The directory that contains the files to FTP. */
private String directory;;

public AutoFTP(String host, String userID, String password, String directory) {
  this.host = host;
  this.userID = userID;
  this.password = password;
  this.directory = directory;
  Thread thread = new Thread(this);
  thread.start();
}

public void run() {
  while (true) {
    try {
      FtpClient ftpClient = new FtpClient();
      ftpClient.openServer(host); // connect to FTP server
      ftpClient.login(userID, password); // login
      File[] files = (new File(directory)).listFiles(); // get files
      for (int i = 0; i < files.length; i++) {
        // Upload if it is a file.
        if (files[i].isFile()) {
          TelnetOutputStream out = ftpClient.put(files[i].getName());
          FileInputStream in = new FileInputStream(files[i]);
          int c = 0;
          while ((c = in.read()) != -1 ) {
            out.write(c);
          }
          in.close();
          out.close();
        }
      ftpClient.closeServer();
    } catch (Exception exception) {
      exception.printStackTrace();
    }

    // Sleep 5 minutes.
    try {
       Thread.sleep(5*60*1000);
    } catch (InterruptedException e) {}
  }
}

}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leungpingkeiAuthor Commented:
To : yongsing

Your code is great. Thank for your help. But one thing it's missed, how can I disable the passive mode when put the file out ? Since destination ftp server is not accept any files if using passive mode. Looking forward to your help again.

0
 
yongsingCommented:
I believe that the default is non-passive. So you don't have to do anything. Am I right?
0
 
leungpingkeiAuthor Commented:
Yes. You are right. But I don't know why I am using passive mode as default. So now, before the code finished, I need to disable passive mode when ftp to this site each time. Anyway, you help me a lot, 50 points is your.
0
 
leungpingkeiAuthor Commented:
Yes. You are right. But I don't know why I am using passive mode as default. So now, before the code finished, I need to disable passive mode when ftp to this site each time. Anyway, you help me a lot, 50 points is your.
0
 
leungpingkeiAuthor Commented:
Yes. You are right. But I don't know why I am using passive mode as default. So now, before the code finished, I need to disable passive mode when ftp to this site each time. Anyway, you help me a lot, 50 points is your.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now