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

2010年10月26日火曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のグループ(グループID)を返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のグループ(グループID)を返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_group as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
@CODE
String ftp_get_group(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
String result = null;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// グループ(グループID)の取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = files[0].getGroup();
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_group('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月25日月曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のユーザ(ユーザID)を返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のユーザ(ユーザID)を返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_user as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
@CODE
String ftp_get_user(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
String result = null;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ユーザ(ユーザID)の取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = files[0].getUser();
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_user('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月24日日曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_timestamp as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
@CODE
Timestamp ftp_get_timestamp(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
Timestamp result = null;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// タイムスタンプの取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = new Timestamp(
files[0].getTimestamp().getTime().getTime() +
TimeZone.getDefault().getOffset(new java.util.Date().getTime()));
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_timestamp('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月23日土曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのサイズを返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのサイズを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_filesize as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
long ftp_get_filesize(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
long result = -1;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイルサイズの取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = files[0].getSize();
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_filesize('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月22日金曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のディレクトリを削除する関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のディレクトリを削除する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_delete_dir as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
int ftp_delete_dir(String host, Integer port,
String user, String password, String encoding,
String path, String dir)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( dir == null ){
throw new Exception("directory is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

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

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

return 0;
}
$$


実行例
call ftp_delete_dir('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'example');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月21日木曜日

H2 DatabaseとApache Commons NetでFTPサーバ上にディレクトリを作成する関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上にディレクトリを作成する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_create_dir as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
int ftp_create_dir(String host, Integer port,
String user, String password, String encoding,
String path, String dir)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( dir == null ){
throw new Exception("directory is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ディレクトリを作成
if( !fc.makeDirectory(path + '/' + dir) ){
throw new Exception("failed to create a directory.");
}

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

return 0;
}
$$


実行例
call ftp_create_dir('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'example');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月20日水曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルをリネームする関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルをリネームする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_rename_file as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
int ftp_rename_file(String host, Integer port,
String user, String password, String encoding,
String path, String filename, String renamedFile)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイルのリネーム
if( !fc.rename(path + '/' + filename,
path + '/' + renamedFile) ){
throw new Exception("failed to rename a file.");
}

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

return 0;
}
$$


実行例
call ftp_rename_file('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.',
'uploaded_SF.JPG', 'renamed_SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月19日火曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルを削除する関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルを削除する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_delete_file as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
int ftp_delete_file(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイルの削除
if( !fc.deleteFile(path + '/' + filename) ){
throw new Exception("failed to delete a file.");
}

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

return 0;
}
$$


実行例
call ftp_delete_file('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'uploaded_readme.txt');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月18日月曜日

H2 DatabaseとApache Commons NetでBLOBをファイルとしてFTPサーバにアップロードする関数を作成する

H2 DatabaseとApache Commons NetでBLOBをファイルとしてFTPサーバにアップロードする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_put_binary as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
int ftp_put_binary(String host, Integer port,
String user, String password, String encoding,
String path, String filename, Blob blob)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイル転送モードをbinaryに
fc.setFileType(FTP.BINARY_FILE_TYPE);

// ファイルのアップロード
InputStream bis = null;
try
{
bis = blob.getBinaryStream();
if( !fc.storeFile(path + '/' + filename, bis) ){
throw new Exception("failed to upload a file.");
}
}
finally
{
if( bis != null )bis.close();
}

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

return 0;
}
$$


実行例
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
ftp_get_binary('192.168.1.122', 21,
'ftptest1', 'ftptest1',
'.', 'SF.JPG')
);

select ftp_put_binary('192.168.1.122', 21,
'ftptest1', 'ftptest1',
'UTF-8', '.', 'uploaded_SF.JPG', c2)
from blobtest
where c1 = 1;


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月17日日曜日

H2 DatabaseとApache Commons NetでCLOBをファイルとしてFTPサーバにアップロードする関数を作成する

H2 DatabaseとApache Commons NetでCLOBをファイルとしてFTPサーバにアップロードする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_put_ascii as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
int ftp_put_ascii(String host, Integer port,
String user, String password, String encoding,
String path, String filename, Clob clob)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイル転送モードをasciiに
fc.setFileType(FTP.ASCII_FILE_TYPE);

// ファイルのアップロード
InputStream ais = null;
try
{
ais = clob.getAsciiStream();
if( !fc.storeFile(path + '/' + filename, ais) ){
throw new Exception("failed to upload a file.");
}
}
finally
{
if( ais != null )ais.close();
}

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

return 0;
}
$$


実行例
drop table if exists clobtest;
create table clobtest (c1 numeric(4), c2 clob);
insert into clobtest values (1,
ftp_get_ascii('192.168.1.122', 21,
'ftptest1', 'ftptest1',
'UTF-8', '.', 'readme.txt')
);
select ftp_put_ascii('192.168.1.122', 21,
'ftptest1', 'ftptest1',
'UTF-8', '.', 'uploaded_readme.txt', c2)
from clobtest
where c1 = 1;


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月16日土曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルをBLOBで返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルをBLOBで返す関数を作成するには、以下の手順を実行します。

1.以下のソースファイルをantなどでコンパイルします。
GetFtpBinaryFunction.java
package com.serverarekore.h2;
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;

public class FtpGetBinaryFunction
{
static public InputStream ftp_get_binary(String host, Integer port,
String user, String password,
String path, String filename)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

InputStream ris = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.");
}
// ログイン
if( fc.login(user, password) == false ){
throw new Exception("failed to login.");
}

// ファイル転送モードをasciiに
fc.setFileType(FTP.ASCII_FILE_TYPE);

// ファイルのダウンロード
InputStream is = fc.retrieveFileStream(filename);

final File tf = File.createTempFile("lobtmp", null);
FileOutputStream fos = new FileOutputStream(tf.getAbsolutePath());

byte buf[] = new byte[8192];
int rs = -1;
try
{
while( (rs = is.read(buf)) != -1 ){
fos.write(buf, 0, rs);
}
}
finally
{
fos.close();
is.close();
}

ris = new InputStream(){
private final InputStream fis = new FileInputStream(tf);
private boolean deleted = false;
private final File fn = tf;
public int autoDelete(int sz) throws IOException
{
if(sz < 0 ){
close();
}
return sz;
}
public void close() throws IOException
{
if( !deleted ){
fis.close();
fn.delete();
deleted = true;
}
}
public int read(byte buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf, off, len));
}

public int read(byte buf[]) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf));
}

public int read() throws IOException
{
return deleted?-1:autoDelete(fis.read());
}
};

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

return ris;
}
}


build.xmlのサンプル
<project name="H2Functions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="c:/Program Files/H2/bin/h2.jar"/>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:/share/jars/FtpGetBinary.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.システム環境変数CLASSPATHにコンパイルしてできたFtpGetBinary.jarを追加し、
H2 Databaseのサービスを再起動。

3.H2 Consoleから以下のコマンドを実行して、ファンクション作成。
create alias ftp_get_binary for 
"com.serverarekore.h2.FtpGetBinaryFunction.ftp_get_binary";


4.以下のようなコマンドでFTPサーバ上のファイルをBLOBに格納できます。
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
ftp_get_binary('192.168.1.122', 21,
'ftptest1', 'ftptest1',
'.', 'SF.JPG')
);


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月15日金曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルをCLOBで返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルをCLOBで返す関数を作成するには、以下の手順を実行します。

1.以下のソースファイルをantなどでコンパイルします。
GetFtpAsciiFunction.java
package com.serverarekore.h2;
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;

public class FtpGetAsciiFunction
{
static public Reader ftp_get_ascii(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{

if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

Reader reader = null;
FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイル転送モードをasciiに
fc.setFileType(FTP.ASCII_FILE_TYPE);

// ファイルのダウンロード
InputStreamReader isr = new InputStreamReader(
fc.retrieveFileStream(filename), encoding);

final File tf = File.createTempFile("lobtmp", null);
FileWriter fw = new FileWriter(tf.getAbsolutePath());

char buf[] = new char[8192];
int rs = -1;
try
{
while( (rs = isr.read(buf)) != -1 ){
fw.write(buf, 0, rs);
}
}
finally
{
fw.close();
isr.close();
}

reader = new Reader(){
private final Reader reader = new FileReader(tf);
private boolean deleted = false;
private final File fn = tf;
public int autoDelete(int sz) throws IOException
{
if(sz < 0 ){
close();
}
return sz;
}
public void close() throws IOException
{
if( !deleted ){
reader.close();
fn.delete();
deleted = true;
}
}
public int read(char buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(reader.read(buf, off, len));
}

public int read(char buf[]) throws IOException
{
return deleted?-1:autoDelete(reader.read(buf));
}

public int read() throws IOException
{
return deleted?-1:autoDelete(reader.read());
}
public int read(java.nio.CharBuffer target) throws IOException
{
return deleted?-1:autoDelete(reader.read(target));
}
};

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

return reader;
}
}


build.xmlのサンプル
<project name="H2Functions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="c:/Program Files/H2/bin/h2.jar"/>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:/share/jars/FtpGetAscii.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.システム環境変数CLASSPATHにコンパイルしてできたFtpGetAscii.jarを追加し、
H2 Databaseのサービスを再起動。

3.H2 Consoleから以下のコマンドを実行して、ファンクション作成。
create alias ftp_get_ascii for "com.serverarekore.h2.FtpGetAsciiFunction.ftp_get_ascii";


4.以下のようなコマンドでFTPサーバ上のファイルをCLOBに格納できます。
drop table if exists clobtest;
create table clobtest (c1 numeric(4), c2 clob);
insert into clobtest values (1,
ftp_get_ascii('192.168.1.122', 21,
'ftptest1', 'ftptest1',
'UTF-8', '.', 'readme.txt')
);
select * from clobtest;


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月14日木曜日

H2 DatabaseとApache Commons NetでFTPの指定ディレクトリの隠しファイルも含めてファイルを列挙する関数を作成する

H2 DatabaseとApache Commons NetでFTPの指定ディレクトリの隠しファイルも含めてファイルを列挙する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_list_hidden_files as $$ 
import java.sql.*;
import org.h2.tools.*;
import org.apache.commons.net.ftp.*;
@CODE
ResultSet ftp_list_hidden_files(String host, Integer port,
String user, String password, String encoding,
String path)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 512, 0);

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// 隠しも含めたファイルの列挙
fc.setListHiddenFiles(true);
FTPFile files[] = fc.listFiles(path);
for(int fi=0;fi<files.length;fi++){
rs.addRow(files[fi].getName());
}

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

return rs;
}
$$


実行例
call ftp_list_hidden_files('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月13日水曜日

H2 DatabaseとApache Commons NetでFTPのシステムタイプ名を取得する関数を作成する

H2 DatabaseとApache Commons NetでFTPのシステムタイプ名を取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_system_name as $$ 
import java.sql.*;
import org.h2.tools.*;
import org.apache.commons.net.ftp.*;
@CODE
String ftp_get_system_name(String host, Integer port,
String user, String password, String encoding)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;

String systemName = null;

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// システム名の取得
systemName = fc.getSystemName();

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

return systemName;
}
$$


実行例
call ftp_get_system_name('192.168.1.122', 21, 
'ftptest1', 'ftptest1', 'UTF-8');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月12日火曜日

H2 DatabaseとApache Commons NetでFTPの指定ディレクトリのファイルを列挙する関数を作成する

H2 DatabaseとApache Commons NetでFTPの指定ディレクトリのファイルを列挙する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_list as $$ 
import java.sql.*;
import org.h2.tools.*;
import org.apache.commons.net.ftp.*;
@CODE
ResultSet ftp_list(String host, Integer port,
String user, String password, String encoding,
String path)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 512, 0);

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
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(encoding);

// ファイルの列挙
FTPFile files[] = fc.listFiles(path);
for(int fi=0;fi<files.length;fi++){
rs.addRow(files[fi].getName());
}

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

return rs;
}
$$


実行例
call ftp_list('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), Apache Commons Net 2.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。