2010年5月31日月曜日

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")

// ファイルのサイズを表示
files = fc.listFiles(".")
for( file in files ){
if( !file.isDirectory() ){
println "${file.name}:${file.size}"
}
}

// ログアウト
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 件のコメント:

コメントを投稿