Java 将base64的字符串存进oracle的bolb字段

  • Post author:
  • Post category:java


package inserBolb;

import sun.misc.BASE64Decoder;

import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;

import org.springframework.jdbc.support.lob.DefaultLobHandler;

import org.springframework.jdbc.support.lob.LobCreator;

import org.springframework.jdbc.support.lob.LobHandler;

public class insertblob {



public static void main(String[] args) {



// TODO Auto-generated method stub



String kk = “data:image/png;base64,iVBORw0KGgoAAAA…”;//base64的字符串省略了后面的编码



String gg[] = kk.split(“,”);



try {



byte [] head = new BASE64Decoder().decodeBuffer(gg[1]);//将base64字符串去掉前面data:image/png:base64,这一段字符串转为byte[]然后存进oracle的blob字段






LobHandler lobHandler = new DefaultLobHandler();



yslFzxbgDao.getJdbcTemplate().execute(“insert into t_user (id,head) values(?,?)”



, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {



protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {



ps.setString(1, UUID.randomUUID().toString());



//   ps.setBytes(2, head);



lobCreator.setBlobAsBytes(ps, 2, head);





}



});



} catch (Exception e) {



// TODO: handle exception



e.printStackTrace();



}






}

}



版权声明:本文为CNZYYH原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。