2008-05-19 13:27:03 2467
ÀÚ¹Ù ftp class
È£¼®
import java.io.*;
import com.oroinc.net.ftp.*;
import com.oroinc.net.*;
public class MyFtpClient {
static String server = "xxxxx";
static int port = 21;
static String id = "xxxxx";
static String password = "xxxxx";
FTPClient ftpClient;
public MyFtpClient(String server, int port, String id, String password) {
this.server = server;
this.port = port;
ftpClient = new FTPClient();
}
public static void main(String args[]) {
MyFtpClient ftp = new MyFtpClient(server, port, id, password);
ftp.connect();
ftp.login(ftp.id, ftp.password);
// ·Î±×ÆÄÀÏÀÌ ÀÖ´Â µð·ºÅ丮·Î À̵¿ÇÑ´Ù
ftp.cd("/home/ems/emsprj/project/wos/WosLog/log");
FTPFile[] files = ftp.list();
for (int i = 0; i < files.length ; i++) {
String fileName = files[i].getName();
// ÆÄÀÏ À̸§¿¡¼ È®ÀåÀÚ¸¸ ÃßÃâ
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
long size = files[i].getSize();
// ÆÄÀÏ »çÀÌÁî°¡ 0º¸´Ù Å©°í ·Î±× ÆÄÀϸ¸ °¡Á®¿Â´Ù
if ( (size > 0) && (extension.equalsIgnoreCase("log")) ) {
File file = ftp.get(fileName, fileName);
}
}
ftp.logout();
ftp.disconnect();
System.exit(1);
}
// °èÁ¤°ú ÆÐ½º¿öµå·Î ·Î±×ÀÎ
public boolean login(String user, String password) {
try {
this.connect();
return ftpClient.login(user, password);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return false;
}
// ¼¹ö·ÎºÎÅÍ ·Î±×¾Æ¿ô
private boolean logout() {
try {
return ftpClient.logout();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return false;
}
// ¼¹ö·Î ¿¬°á
public void connect() {
try {
ftpClient.connect(server, port);
int reply;
// ¿¬°á ½ÃµµÈÄ, ¼º°øÇß´ÂÁö ÀÀ´ä ÄÚµå È®ÀÎ
reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("¼¹ö·ÎºÎÅÍ ¿¬°áÀ» °ÅºÎ´çÇß½À´Ï´Ù");
System.exit(1);
}
}
catch (IOException ioe) {
if(ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch(IOException f) {
//
}
}
System.err.println("¼¹ö¿¡ ¿¬°áÇÒ ¼ö ¾ø½À´Ï´Ù");
System.exit(1);
}
}
// FTPÀÇ ls ¸í·É, ¸ðµç ÆÄÀÏ ¸®½ºÆ®¸¦ °¡Á®¿Â´Ù
public FTPFile[] list() {
FTPFile[] files = null;
try {
files = this.ftpClient.listFiles();
return files;
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
// ÆÄÀÏÀ» Àü¼Û ¹Þ´Â´Ù
public File get(String source, String target) {
OutputStream output = null;
try {
File local = new File(source);
output = new FileOutputStream(local);
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
File file = new File(source);
try {
if (ftpClient.retrieveFile(source, output)) {
return file;
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
// ¼¹ö µð·ºÅ丮 À̵¿
public void cd(String path) {
try {
ftpClient.changeWorkingDirectory(path);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
// ¼¹ö·ÎºÎÅÍ ¿¬°áÀ» ´Ý´Â´Ù
private void disconnect() {
try {
ftpClient.disconnect();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}