项目中用到的,不知道为什么要用LONG型 LONG型存入的为字符串,可以直接存byte数组转成的字符串,但转换规则要一致,因为要拿出来转换为数组 ,不能用getbytes()
我在每一个byte型中间插入一个空格,读的时候 split;
设计时是要存入二进制,byte转成二进制形式的String类型 要补齐8位,但拿出来转回去还是有点麻烦,还是在每8位分隔,取得时候split
不知道有没有更好的方法,暂时先这么做的,改为 long raw 型字符串,可以直接放进bytes[]。
InputStream is = contentFile.getInputStream(); int count = is.available(); byte[] bytes = new byte[count]; int readCount = 0; while(readCount < count) { readCount = is.read(bytes, readCount, count - readCount); } is.close(); //二进制 StringBuffer buf = new StringBuffer(8); //十进制 StringBuffer bufs = new StringBuffer(3); for (int i = 0; i < bytes.length; i++) { buf.append(String.format("%08d", Integer.valueOf(Integer.toBinaryString(bytes[i] & 0xff)))).append(" "); bufs.append(bytes[i]).append(" "); } //二进制 // String[] bbbs = buf.toString().split(" "); // byte[] vvvs = new byte[bbbs.length]; // for (int i = 0; i < bbbs.length; i++) { // vvvs[i] = (byte) (new BigInteger(bbbs[i], 2).intValue() & 0xff); // } //10进制 String[] bbb = bufs.toString().split(" "); byte[] vvv = new byte[bbb.length]; for (int i = 0; i < bbb.length; i++) { vvv[i] =Byte.parseByte(bbb[i]); } File file = new File("G:\\1223.pdf"); OutputStream out = new FileOutputStream(file);
// out.write(vvvs); out.write(vvv); out.flush(); out.close();
2012.11-12 后来字符类型改为LONG RAW ,这样就可以直接将bytes[],直接放进去,取得时候可以直接取出byte数组:
//存
Map<String,Object> uploadMap = new HashMap<String, Object>(); uploadMap.put("ID", "12345678"); uploadMap.put("NAME", "12345678"); uploadMap.put("CONTENT", bytes); DB.add(table_name,uploadMap);
//取
Map<String,Object> downloadMap = new HashMap<String, Object>();
List<Map<String,Object>> lll = new ArrayList<Map<String,Object>>();
String sql = "select * from "+table_name+ " where ID = ? ";
lll = DB.queryForList(sql, "12345678");
if(lll.size() > 0){
downloadMap = lll.get(0);
System.out.println("取到值");
}
byte[] byte2 = (byte[])downloadMap.get("CONTENT");
每天进步一点,只要今天比昨天好,就有希望!!