|
楼主 |
发表于 2014-11-12 20:18:48
|
显示全部楼层
本帖最后由 Masion 于 2014-11-13 09:01 编辑
public static void picConvertFromBinToBase64() throws Exception{
//将fDriverImg中的二进制批量转换为fDriverPic中的Base64码
//取得司机的二进制图片信息
StringBuffer sbSql = new StringBuffer();
sbSql.append("SELECT fIdentityCard,fName,fDriverImg ");
sbSql.append("from MY_DriverBasic ");
HashMap<String, String> sqlMap = new HashMap<String, String>();
sqlMap.put(SQL.DEFAULT_DB_NAME, sbSql.toString());
sqlMap.put(DatabaseProduct.MSSQL.name(), sbSql.toString());
Table table = SQL.select(sqlMap, null, "/MyProject/TaxiManage/data");
//将二进制图片转换成Base64
Iterator<Row> it = table.iterator();
Row r = null;
String driverPic=null;
byte[] bytes = null;
while (it.hasNext()){
r = it.next();
bytes=blob2ByteArr(r.getBlob("fDriverImg")); //转成字节数组
if (bytes!= null){
driverPic=(new sun.misc.BASE64Encoder()).encode(bytes); //Base64编码
}
try{ //将Base64码传回对应的司机记录
String ksql="update MY_DriverBasic MY_DriverBasic set MY_DriverBasic.fDriverPic='"+ driverPic+"' where "+
"MY_DriverBasic.fIdentityCard='"+r.getValue("fIdentityCard")+"'";
KSQL.executeUpdate(ksql, null, "/MyProject/TaxiManage/data", null);
}
catch(Exception e){
throw new RuntimeException (e.getMessage());
}
};
}
public static byte[] blob2ByteArr(Blob blob) throws Exception {
//从网上某位大神的博客中找的代码,将blob数据转换为byte[]数据
byte[] b = null;
try {
if (blob != null) {
long in = 1;
b = blob.getBytes(in, (int) (blob.length()));
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("fault");
}
return b;
}
通过上面这个程序,3500个司机照片转换大约2分钟左右
|
|