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();
}
}
}
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();
}
}
}