我在数据库中的某个表有个image字段,用来放大文本,如意见信息等。我现在已经用getBlob取出并转化为byte[],同时使用     ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(sContentBytes));
    sContent = ois.readObject();
    ois.close();
将byte[]放在sContent 中,然后我采用 
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ObjectOutputStream oos = new ObjectOutputStream(baos);
     oos.writeObject(sContent);
     oos.flush();
     byte sContentBytes[] = baos.toByteArray();
     oos.close();
     baos.close();
     return sContentBytes;返回byte[],然后我想得到字符串,但我使用new string()得到的字符串是乱码的,以及使用new string(byte[],"utf-8")等(包括iso8859-1、gbk)等得到的字符串都是乱码的
不知道哪位做过?

解决方案 »

  1.   

    可将其进行BASE64编码,用这个:String str = new BASE64Encoder().encode(sContentBytes);
      

  2.   

    谢谢ldh911.我曾经使用过 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(baos);
         oos.writeObject(sContent);
         oos.flush();
         byte sContentBytes[] = baos.toByteArray();
         //byte[] bytes = baos.toByteArray();      oos.close();
         baos.close();
         return encoder.encodeBuffer(sContentBytes).trim();
    都还是乱码
      

  3.   

    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
      

  4.   

    也可以用Apache关于BASE64的开源包:org.apache.commons.codec.binary.Base64public static String encodeBase64String(byte[] binaryData)
      

  5.   

     用return encoder.encodeBuffer(sContentBytes).trim();
    得到的是rO0ABXQADnRyZXRyZXd0cmV3dHJl
    本来应该是 tretrewtrewtre ,用new string(byte) 得到的是 方框方框t方框方框tretrewtrewtre不知道什么原因。另外,org.apache.commons.codec.binary.Base64不知道是哪个包的?还有 是否方便留下的联系方式,比如qq?
      

  6.   

    这个是BASE64编码,适用于所有二进制流,所以不可能看到原文。解码必须要用 BASE64Decoder,不能直接用 new String()包应该是:Commons Codec
    http://commons.apache.org/codec/
      

  7.   

    我如果使用 System.out.println(org.apache.commons.codec.binary.Base64.decodeBase64(byte[])) 会报错:
    23:24:18,433 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
    java.lang.ArrayIndexOutOfBoundsException: -84
            at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
            at org.apache.commons.codec.binary.Base64.discardNonBase64(Base64.java:4
    78)
            at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:374)
      

  8.   

    我的byte[]是这样得来的:
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(baos);
         oos.writeObject(sContent);
         oos.flush();
         byte sContentBytes[] = baos.toByteArray();
         oos.close();
         baos.close();
         return sContentBytes;
    sContent是这样得来的:
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(sContentBytes));
        sContent = ois.readObject();
        ois.close();按道理不会出错了啊 但是
    我如果使用 System.out.println(org.apache.commons.codec.binary.Base64.decodeBase64(byte[])) 会报错:
    23:24:18,433 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
    java.lang.ArrayIndexOutOfBoundsException: -84
      at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
      at org.apache.commons.codec.binary.Base64.discardNonBase64(Base64.java:4
    78)
      at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:374) 
      

  9.   

    给你段代码参考吧:        byte sContentBytes[] = "Hello World, 我是中国人 , それは本です。".getBytes();
            String en = Base64.encodeBase64String(sContentBytes);
            System.out.println("BASE64 Encode: " + en);
            
            String str = new String(Base64.decodeBase64(en));
            System.out.println("BASE64 Decode: " + str);
    另外,既然你是直接写String,就不要用ObjectOutputStream来处理了,直接用Writer其实就够了。
      

  10.   

    谢谢
    但我的image字段内容用getBlob取出并转化为byte[]之后,直接Base64.encodeBase64String(sContentBytes);转化为string?
      

  11.   


    是的,直接把Image得到的byte[]转换为String即可:
    String en = Base64.encodeBase64String(sContentBytes);然后要重新得到Image的byte[]就直接:
    byte[] img = Base64.decodeBase64(en);
      

  12.   

    谢谢ldh911.
    数据库中这个字段的值是:0xACED000574000E7472657472657774726577747265
    而我输入的值应该是 tretrewtrewtre  他保存到数据库变成了0xACED000574000E7472657472657774726577747265现在我想还原为 tretrewtrewtre   ,怎么搞?
    我使用如下代码:      Blob blogObj = rs.getBlob("s_sign_content");
         byte[] s_sign_content = blogObj.getBytes(1L, (int)blogObj.length());
         en = Base64.encodeBase64String(s_sign_content);然后en为 但是我得到的这个字符串::rO0ABXQADnRyZXRyZXd0cmV3dHJl
    不是我的输入值啊 
    :)
      

  13.   

    读取的时候,要用 new String(Base64.decodeBase64(s_sign_content))。保存才使用encodeBase64String。保存用编码encode,读取用解码decode
      

  14.   


    我得到 :囤薅钒斗岸? 是不是 new String(byte,“utf-8”) ?
      

  15.   

    谢谢。rs = stmt.executeQuery(sql);
        while (rs.next())
        {      Blob blogObj = rs.getBlob("s_sign_content");
         byte[] s_sign_content = blogObj.getBytes(1L, (int)blogObj.length());
         //en = Base64.encodeBase64String(s_sign_content);
         en = new String(Base64.decodeBase64(s_sign_content));      System.out.println("en:"+en);
        }就是这样获取的啊。