我正在尝试从 mysql 获取 blob,但使用的是 json。这是我的代码:
public class ClubVO {
private long _id;
private String nom = null;
private byte[] imageClub = null;
public long getId() {
return _id;
}
public void setId( long aId ) {
this._id = aId;
}
public String getNom() {
return nom;
}
public void setNom( String aNom ) {
this.nom = aNom;
}
public byte[] getImage() {
return imageClub;
}
public void setImage( byte[] aImage ) {
this.imageClub = aImage;
}
}
这是我用来检索 json 对象的函数:
public List<ClubVO> consultPlacesByName() throws Exception {
String[] resposta = new WebServiceClient().get( URL_WS );
if ( resposta[0].equals("200") ) {
Gson gson = new Gson();
ArrayList<ClubVO> lListaPlaces = new ArrayList<ClubVO>();
JsonParser parser = new JsonParser();
JsonArray lJasonArray = parser.parse(resposta[1]).getAsJsonArray();
for (int i = 0; i < lJasonArray.size(); i++)
lListaPlaces.add( gson.fromJson( lJasonArray.get(i), ClubVO.class ) );
return lListaPlaces;
} else {
throw new Exception( resposta[1] );
}
}
这是我通过这段代码得到的结果:
{"_id":15,"nom":"Musée d'art Contemporain de Montréal","imageClub":"[B@25450aa2"}
还有一件事,我的专栏是 blob 类型。
最佳答案
您的问题是,您在 json 数据中拥有字节数组的哈希码,而不是真正的图像字节:“[B@25450aa2” 要解决序列化对象时需要使用的问题
new String(Base64.encodeBase64(byteArray))
当你解析json数据时,你需要做相反的事情:
Base64.decodeBase64(jsonString.getBytes())
关于java - 如何使用 json 从 mysql 检索图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20057951/