现在的位置: 首页 > 综合 > 正文

ORACLE的BLOB和BASE64

2011年07月17日 ⁄ 综合 ⁄ 共 1089字 ⁄ 字号 评论关闭

最近的项目中,要用到ORACLE的BLOB和BASE64。场景是要把图片以BLOB放到ORACLE里去,
然后在WS里,以BASE64加密后,返回给。NET,。NET再解码还原之。过程小结之:

1 JAVA上传图片到BLOB字段就不说了,比较简单

2 把BLOB用BASE64加密的方法如下:
    public static String ioToBase64() throws IOException {
         String fileName = "d:/sunset.jpg"; //源文件
         String strBase64 = null;
         try {
             InputStream in = new FileInputStream(fileName);
             // in.available()返回文件的字节长度
             byte[] bytes = new byte[in.available()];
             // 将文件中的内容读入到数组中
             in.read(bytes);
             strBase64 = new BASE64Encoder().encode(bytes);      //将字节流数组转换为字符串
             in.close();
         } catch (FileNotFoundException fe) {
             fe.printStackTrace();
         } catch (IOException ioe) {
             ioe.printStackTrace();
         }
         return strBase64;
     }

3 。NET的WEBSERVICE接收这个字符串,解BASE64之,C#里比较简单,并且把东西保存了
   WebReference.HelloWorldImplService h = new WebReference.HelloWorldImplService();
        string str = h.ReturnBase64String();
        byte[] bs = Convert.FromBase64String(str);
        FileStream file = new FileStream("c:/hello.jpg", FileMode.Create);
             file.Write(bs, 0, bs.Length);
             file.Close();

抱歉!评论已关闭.