?
Solved

Check each 5 minute and ftp out

Posted on 2003-03-14
8
Medium Priority
?
178 Views
Last Modified: 2010-03-31
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
Comment
Question by:leungpingkei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 8136456
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
 

Author Comment

by:leungpingkei
ID: 8141609
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
 
LVL 9

Accepted Solution

by:
yongsing earned 200 total points
ID: 8141885
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
Independent Software Vendors: 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!

 

Author Comment

by:leungpingkei
ID: 8151685
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
 
LVL 9

Expert Comment

by:yongsing
ID: 8151793
I believe that the default is non-passive. So you don't have to do anything. Am I right?
0
 

Author Comment

by:leungpingkei
ID: 8151905
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
 

Author Comment

by:leungpingkei
ID: 8151933
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
 

Author Comment

by:leungpingkei
ID: 8151986
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question