ラベル ApacheCommonsNet の投稿を表示しています。 すべての投稿を表示
ラベル ApacheCommonsNet の投稿を表示しています。 すべての投稿を表示

2010年8月31日火曜日

groovyとApache Commons NetでNTPサーバから時刻を取得する

groovyとApache Commons NetでNTPサーバから時刻を取得するには、以下のコードを実行します。

import org.apache.commons.net.ntp.*

ntpclient = new NTPUDPClient()
ntpclient.open()
// NTPサーバより時刻取得
ti = ntpclient.getTime(InetAddress.getByName("ntp.nict.jp"))
ntpclient.close()
println "time:" + new Date(ti.getReturnTime())



動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Net 2.0

2010年6月2日水曜日

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

// ファイルのユーザ(ユーザID)・グループ(グループID)を表示
files = fc.listFiles(".")
for( file in files ){
if( !file.isDirectory() ){
println "${file.name}:${file.user}:${file.group}"
}
}

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


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

2010年6月1日火曜日

groovyとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを取得する

groovyとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを取得するには、以下のコードを実行します。

import java.text.*
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")

// ファイルのタイムスタンプを表示
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
files = fc.listFiles(".")
for( file in files ){
if( !file.isDirectory() ){
println "${file.name}:" + sdf.format(file.getTimestamp().getTime())
}
}

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


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

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

2010年5月30日日曜日

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
}
}

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


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

2010年5月29日土曜日

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.listHelp()

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


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

2010年5月28日金曜日

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.changeWorkingDirectory("/opt") ){
throw new Exception("failed to change working 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

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

2010年5月26日水曜日

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

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


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

2010年5月21日金曜日

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

// ディレクトリの削除
if( !fc.removeDirectory("test") ){
throw new Exception("failed to delete a directory.")
}

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


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

2010年5月16日日曜日

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

// ディレクトリの作成
if( !fc.makeDirectory("test") ){
throw new Exception("failed to create a directory.")
}

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


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

2010年5月11日火曜日

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

// ファイルのリネーム
if( !fc.rename("./SF.JPG", "./renamed.jpg") ){
throw new Exception("failed to rename a file.")
}

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


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

2010年5月6日木曜日

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

// ファイルの削除
if( !fc.deleteFile("./SF.JPG") ){
throw new Exception("failed to delete a file.")
}

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


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

2010年5月1日土曜日

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

// ファイル転送モードをバイナリに
fc.setFileType(FTP.BINARY_FILE_TYPE)

// ファイルのダウンロード
fos = new FileOutputStream("./SF_LOCAL.JPG")
if( !fc.retrieveFile("./SF.JPG", fos) ){
throw new Exception("failed to download a file.")
}

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


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

2010年4月26日月曜日

groovyとApache Commons NetでFTPサーバにファイルをアップロードする

groovyとApache Commons NetでFTPサーバにファイルをアップロードするには、以下のコードを実行します。

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

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

// ファイル転送モードをバイナリに
fc.setFileType(FTP.BINARY_FILE_TYPE)

// ファイルのアップロード
fis = new FileInputStream("./SF.JPG")
if( !fc.storeFile("./SF.JPG", fis) ){
throw new Exception("failed to upload a file.")
}

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


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

2010年4月21日水曜日

groovyとApache Commons NetでFTPサーバ上の隠しファイルも列挙する

groovyとApache Commons NetでFTPサーバ上の隠しファイルも列挙するには、以下のコードを実行します。

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

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

// 隠しファイルも含めたファイルの列挙
fc.setListHiddenFiles(true)
files = fc.listFiles(".")
for( file in files ){
println file.name
}

// ログアウト
fc.logout()
}
finally
{
if( fc.isConnected() ){
fc.disconnect()
}
}


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

2010年4月16日金曜日

groovyとApache Commons NetでFTPサーバのシステムタイプ名を取得する

groovyとApache Commons NetでFTPサーバのシステムタイプ名を取得するには、以下のコードを実行します。

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

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.getSystemName()

// ログアウト
fc.logout()
}
finally
{
if( fc.isConnected() ){
fc.disconnect()
}
}


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

2010年4月11日日曜日

groovyとApache Commons NetでFTPサーバ上のファイルを列挙する

groovyとApache Commons NetでFTPサーバ上のファイルを列挙するには、以下のコードを実行します。

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

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 ){
println file.name
}

// ログアウト
fc.logout()
}
finally
{
if( fc.isConnected() ){
fc.disconnect()
}
}


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