【问题标题】:Way to deserialize the byte array in spring java在spring java中反序列化字节数组的方法
【发布时间】:2015-01-15 20:17:51
【问题描述】:

我正在做上传图像的项目,为此我将图像转换为 base64 字符串,现在在我的 DTO 中,我已将字节数组反序列化如下:

class DecodePhoto extends JsonDeserializer<byte[]> {
    @Override
    public byte[] deserialize(JsonParser jsonparser, DeserializationContext arg1)
            throws IOException, JsonProcessingException {

        String data = jsonparser.getText();

        try {
            byte[] decodedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(data);

            return decodedBytes;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}

在图像中调用 ajax 后,出现以下错误:

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64(Ljava/lang/String;)

这里有什么问题?我附上了 jar common-codec-1.9.jar 和 jackson-databind-2.3.2.jar

但在调试模式下(类文件编辑器)显示如下

JAR 文件 jackson-databind-2.3.2.jar 没有源附件。

请帮我反序列化字节数组?

【问题讨论】:

  • this 可以帮到你

标签: spring bytearray deserialization image-uploading json-deserialization


【解决方案1】:

我已经使用.decodeBase64(byte[]) 而不是.decodeBase64(String) 解码了字节数组。

原因:因为.decodeBase64(final String base64String)这个方法用在common-codec-1.4版本,而我用的是common-codec-1.9版本,所以我用.decodeBase64(byte[])。

class DecodePhoto extends JsonDeserializer<byte[]> {

    @Override
    public byte[] deserialize(JsonParser jsonparser, DeserializationContext arg1)
            throws IOException, JsonProcessingException {

        String data = jsonparser.getText();

        try {

            byte[] decodedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(**data.getBytes()**);  // here is the major change

            return decodedBytes;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-09
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    相关资源
    最近更新 更多