2010年5月27日木曜日

groovyとApache Commons NetでFTPサーバ上の親ディレクトリへ移動する

groovyとApache Commons NetでFTPサーバ上の親ディレクトリへ移動するには、以下のコードを実行します。

import org.apache.commons.net.ftp.*

fos = null
fc = new FTPClient();
try
{
// 接続
fc.connect("192.168.1.1", 21)
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.")
}
// ログイン
if( fc.login("user", "password") == false ){
throw new Exception("failed to login.")
}
// エンコーディングの設定
fc.setControlEncoding("UTF-8")

// 親ディレクトリへ移動
println fc.printWorkingDirectory()
if( !fc.changeToParentDirectory() ){
throw new Exception("failed to move parent directory.")
}
println fc.printWorkingDirectory()

// ログアウト
fc.logout()
}
finally
{
if( fc.isConnected() ){
fc.disconnect()
}
if( fos != null )fos.close()
}


動作環境
groovy 1.7.1, JDK6 Update19, Apache Commons Net 2.0

0 件のコメント:

コメントを投稿