drop alias jcifs_put_binary;
create alias if not exists jcifs_put_binary as $$
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
int jcifs_put_binary(String domain,
String user, String password, String server,
String path, String filename, Blob blob)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}
OutputStream os = null;
InputStream bis = null;
try
{
os = new SmbFileOutputStream(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename);
bis = blob.getBinaryStream();
byte buf[] = new byte[8192];
int rs = -1;
while( (rs = bis.read(buf)) != -1 ){
os.write(buf, 0, rs);
}
}
finally
{
if( os != null )os.close();
if( bis != null )bis.close();
}
return 0;
}
$$
実行例
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
jcifs_get_binary('LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF.JPG')
);
select jcifs_put_binary('LIFERAY1',
'test1', 'test1', 'liferay1',
'share/h2', 'renamed_SF.JPG', c2)
from blobtest
where c1 = 1;
※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。
動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。