在 Grails 中解码 Base64 字符串
Decoding the Base64 string in Grails
我正在开发一个网络 API,我在其中以 JSON 格式从应用程序发送数据,其中包含我需要在服务器上解码的 base64 字符串形式的图像。
下面是我解码字符串的代码
def imageString=object.image
Base64 coder = new Base64()
def decode=coder.decode(imageString)
抛出此错误:
Cannot cast object '[B@6aec0bd1' with class '[B' to class 'javax.ws.rs.core.Response'. Stacktrace follows:
Message: Cannot cast object '[B@6aec0bd1' with class '[B' to class 'javax.ws.rs.core.Response'
有什么可能的解决方案?
你可以试试这个。
def imageString=object.image
Base64 coder = new Base64()
byte[] decoded=coder.decodeBase64(imageString)
String s == new String(decoded)
如果你有一个 base64 编码的字符串,你可以用这个将它转换成一个字节数组:
def imageString
def byteArray = imageString.decodeBase64()
但是,在您的异常中 object.image
似乎已经是字节数组,而不是字符串:java 类型描述符 [B
表示原始字节数组。这表明它可能已经从其他地方的 base64 转换而来。
我正在开发一个网络 API,我在其中以 JSON 格式从应用程序发送数据,其中包含我需要在服务器上解码的 base64 字符串形式的图像。
下面是我解码字符串的代码
def imageString=object.image
Base64 coder = new Base64()
def decode=coder.decode(imageString)
抛出此错误:
Cannot cast object '[B@6aec0bd1' with class '[B' to class 'javax.ws.rs.core.Response'. Stacktrace follows: Message: Cannot cast object '[B@6aec0bd1' with class '[B' to class 'javax.ws.rs.core.Response'
有什么可能的解决方案?
你可以试试这个。
def imageString=object.image
Base64 coder = new Base64()
byte[] decoded=coder.decodeBase64(imageString)
String s == new String(decoded)
如果你有一个 base64 编码的字符串,你可以用这个将它转换成一个字节数组:
def imageString
def byteArray = imageString.decodeBase64()
但是,在您的异常中 object.image
似乎已经是字节数组,而不是字符串:java 类型描述符 [B
表示原始字节数组。这表明它可能已经从其他地方的 base64 转换而来。