在spring中对http进行序列化和反序列化会导致StreamCorruptedException

时间:2015-12-17 15:41:37

标签: java spring servlets serialization objectinputstream

我们在java中使用spring framwork。我们必须从1台服务器发送http post req到anther,msg应该是base 64并加密。

base 64的代码和解码工作正常,以及加密,比传输字符串和一切都很好,现在我们正在发送一个对象。

public class AuthenticationDTO implements Serializable{

    private byte[] publicKey;
    private byte[] privateKey;
    private byte[] cert;
    private String secret;

    public AuthenticationDTO() {}

    public AuthenticationDTO(byte[] publicKey, byte[] privateKey, String secret) {
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        this.secret = secret;
    }

    public String getSecret() {
        return this.secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public byte[] getPublicKey() {
        return publicKey;
    }

    public byte[] getPrivateKey() {
        return privateKey;
    }

    public void setPublicKey(byte[] publicKey) {
        this.publicKey = publicKey;
    }

    public void setPrivateKey(byte[] privateKey) {
        this.privateKey = privateKey;
    }

    public byte[] getcert() {
        return cert;
    }

    public void setcert(byte[] cert) {
        this.cert = cert;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AuthenticationDTO that = (AuthenticationDTO) o;

        if (!Arrays.equals(publicKey, that.publicKey)) return false;
        if (!Arrays.equals(privateKey, that.privateKey)) return false;
        if (!Arrays.equals(cert, that.cert)) return false;
        return !(secret != null ? !secret.equals(that.secret) : that.secret != null);

    }

    @Override
    public int hashCode() {
        int result = publicKey != null ? Arrays.hashCode(publicKey) : 0;
        result = 31 * result + (privateKey != null ? Arrays.hashCode(privateKey) : 0);
        result = 31 * result + (cert != null ? Arrays.hashCode(cert) : 0);
        result = 31 * result + (secret != null ? secret.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "AuthenticationDTO{" +
                "publicKey=" + Arrays.toString(publicKey) +
                ", privateKey=" + Arrays.toString(privateKey) +
                ", cert=" + Arrays.toString(cert) +
                ", secret='" + secret + '\'' +
                '}';
    }
}

这就是我将对象序列化的方式:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(authenticationDTO);
byte[] bytes = bos.toByteArray();

这就是我反序列化对象的方式:

ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream in = new ObjectInputStream(bis);
AuthenticationDTO authenticationDTO = null;
authenticationDTO = (AuthenticationDTO) in.readObject();

并获得例外:

java.io.StreamCorruptedException: invalid stream header: 57FAC6C7
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806) ~[na:1.8.0_60]
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299) ~[na:1.8.0_60]

任何想法?

0 个答案:

没有答案