package dntech7.basis; import java.io.*; import java.util.*; import dntech7.basis.ClassLogWriter; import com.enterprisedt.net.ftp.*; import com.enterprisedt.util.debug.*; /** *

Title: FtpManager

*

Description: FTP Utility

*

Copyright: Copyright (c) 2006

*

Company: dntech7

* @author dntech7 * @version 1.0 */ public class FtpManager { public FTPClient mClient; private boolean mConnected; private String mIp; private int mPort; private String mPathName; private String mProgressFile; /* constructor */ public FtpManager(String aIp, int aPort){ this(aIp, aPort, null, null); } public FtpManager(String aIp, int aPort, String aPathName){ this(aIp, aPort, aPathName, null); } public FtpManager(String aIp, int aPort, String aPathName, String aProgressFile){ mIp = aIp; mPort = aPort; mPathName = aPathName; mProgressFile = aProgressFile; mConnected = false; } // Á¢¼ÓÇÑ ¼­¹ö¿¡ ·Î±×ÀÎ ÇÑ´Ù. public boolean login(String aId, String aPassword){ // ŸÀӾƿô 10ÃÊ try{ // ¼­¹ö¿¡ Á¢¼ÓÇÑ´Ù. if(mClient==null) mClient = new FTPClient(mIp, mPort, 10000); // ·Î±×ÀÎÀ» ½ÃµµÇÑ´Ù. mClient.login(aId, aPassword); // Binary Mode Setting mClient.setType(FTPTransferType.BINARY); // change directory if(mPathName != null) mClient.chdir(mPathName); mConnected = true; return true; } catch(IOException ie){ ClassLogWriter.trace(this.toString()+":login", ie); return false; } catch(FTPException fe){ ClassLogWriter.trace(this.toString()+":login(ftp)", fe); return false; } } // Á¢¼Ó¿©ºÎ¸¦ È®ÀÎ public boolean Connected(){ return mConnected; } // ÇϳªÀÇ ÆÄÀÏÀ» ¾÷·Îµå ÇÑ´Ù public boolean put(String aLocalPath){ if(!mConnected) return false; try{ String vRemotePath = aLocalPath.substring(0, aLocalPath.lastIndexOf("/")); checkdir(vRemotePath); // append ÇÏÁö ¾Ê´Â´Ù. // µ¿ÀÏÇÑ °æ·Î¿¡ copyÇÑ´Ù. mClient.put(aLocalPath, aLocalPath, false); return true; } catch(IOException ie){ ClassLogWriter.trace(this.toString()+":put", ie); return false; } catch(FTPException fe){ ClassLogWriter.trace(this.toString()+":put(ftp)", fe); return false; } } // ÇϳªÀÇ ÆÄÀÏÀ» ´Ù¿î·Îµå ÇÑ´Ù public boolean get(String aLocalPath, String remoteFile){ if(!mConnected) return false; try{ // append ÇÏÁö ¾Ê´Â´Ù. // µ¿ÀÏÇÑ °æ·Î¿¡ copyÇÑ´Ù. mClient.get(aLocalPath, remoteFile); return true; } catch(IOException ie){ ClassLogWriter.trace(this.toString()+":get", ie); return false; } catch(FTPException fe){ ClassLogWriter.trace(this.toString()+":get(ftp)", fe); return false; } } // µð·ºÅ丮 À̸§À» º¯°æÇÑ´Ù. public boolean chdir(String aDir){ try{ mClient.chdir(aDir); return true; } catch(IOException ie){ ClassLogWriter.trace(this.toString()+":chdir", ie); return false; } catch(FTPException fe){ ClassLogWriter.trace(this.toString()+":chdir(ftp)", fe); return false; } } // FTP Ŭ¶óÀ̾ðÆ®¸¦ ´Ý´Â´Ù public void close(){ try{ mClient.quit(); }catch(Exception e){} } // ¿©·¯ ÆÄÀϵéÀ» ¹Þ¾Æ¼­ °¢°¢ÀÇ µð·ºÅ丮¿¡ ¿Ã¸°´Ù. // ¼º°ø°Ç¼ö¸¦ ¸®ÅÏ public int putfiles(String [] aRemoteDirectories, String [] aLocalFiles) { int vSuccess = 0; RandomAccessFile vRandomFile = null; if(aRemoteDirectories == null || aLocalFiles == null) return 0; try{ // °¹¼ö°¡ °°ÀºÁö üũÇÑ´Ù. if(aRemoteDirectories.length != aLocalFiles.length) return 0; if(mProgressFile != null){ File f = new File(mProgressFile); if(f.exists()) f.delete(); vRandomFile = new RandomAccessFile(mProgressFile, "rw"); vRandomFile.writeBytes("0/"+String.valueOf(aLocalFiles.length)); } for(int i=0; i