postgresql数据库使用bytea类型字段
具体存储时使用的是postgresql自定义函数(存储过程)
函数如下:
create or replace function savepicture(xpicture varchar,xpicname varchar)
returns int as $$
begin
insert in temp_picture(picture,picturename) values(cast(xpicture as bytea),xpicname);
return 1;
end $$ language plpgsql;
部分代码处理
String picture= null;
byte[] data = null; File file = new File(path); FileInputStream input = null; try { input = new FileInputStream (file); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[(int)file.length()]; int byteread = 0; try { while((byteread = input.read(buf))!=-1){ output.write(buf,byteread); } data = output.toByteArray(); picture= binarys(data); output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e1) { e1.printStackTrace(); }
public static String binarys(byte[] bytes){ StringBuffer sbf = new StringBuffer(""); for (int i = 0; i < bytes.length; i++) { String tmp = Integer.toOctalString(bytes[i] & 0xff); switch (tmp.length()) { case 1: tmp = " 0" +tmp; break; case 2: tmp = " " +tmp; break; case 3: tmp = "" +tmp; break; default: break; } sbf.append(tmp); } return sbf.toString(); }
postgresql存储二进制数据需要转为3位八进制数据存储。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|