postgresql数据库使用bytea类型字段
特别是,使用postgresql自定义函数“存储过程”
创建复制功能savepicture (xpicturevarchar,xpicname varchar ) )。
returns int as $$
比根
insertintemp _ picture (图像,图像名称) values (cast ) xpictureasbytea ),x图像);
返回1;
结束$ $ language plpgsql;
有些代码是无限的蛋包饭
字符串图片=null;
byte [ ]数据=null;
文件文件=new file (路径;
try {
输入=新文件输入流(文件;
byte[]buf=newbyte[(int ) file.length ) ];
Int字节=0;
try {
wile(byteread=input.read ) buf )!=-1 ()
output.write(buf,0,byteread );
}
data=output.toByteArray (;
图片=binarys (数据;
output.close (;
input.close (;
}catch(ioexceptione ) {
e .打印堆栈跟踪(;
}
}catch(filenotfoundexceptione ) {
e .打印堆栈跟踪(;
}catch(ioexceptionE1) {
E1 .打印堆栈跟踪(;
}
publicstaticstringbinarys (byte [ ] bytes ) {
string buffers BF=new string buffer (' ';
for(intI=0; i bytes.length; I ) {
string tmp=integer.tooctalstring (bytes [ I ]0x ff );
开关(tmp.length () ) ) ) ) )。
case 1:
布雷克;
case 2:
布雷克;
case 3:
布雷克;
默认:
布雷克;
}
SBF.append(tmp;
}
return sbf.toString (;
}
postgresql存储二进制文件必须转换为3位八进制数据存储。