我在数据库中的某个表有个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)等得到的字符串都是乱码的
不知道哪位做过?
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)等得到的字符串都是乱码的
不知道哪位做过?
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();
都还是乱码
import sun.misc.BASE64Encoder;
得到的是rO0ABXQADnRyZXRyZXd0cmV3dHJl
本来应该是 tretrewtrewtre ,用new string(byte) 得到的是 方框方框t方框方框tretrewtrewtre不知道什么原因。另外,org.apache.commons.codec.binary.Base64不知道是哪个包的?还有 是否方便留下的联系方式,比如qq?
http://commons.apache.org/codec/
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)
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)
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其实就够了。
但我的image字段内容用getBlob取出并转化为byte[]之后,直接Base64.encodeBase64String(sContentBytes);转化为string?
是的,直接把Image得到的byte[]转换为String即可:
String en = Base64.encodeBase64String(sContentBytes);然后要重新得到Image的byte[]就直接:
byte[] img = Base64.decodeBase64(en);
数据库中这个字段的值是: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
不是我的输入值啊
:)
我得到 :囤薅钒斗岸? 是不是 new String(byte,“utf-8”) ?
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);
}就是这样获取的啊。