Java 如何将Writer转换为字节数组

Java 如何将Writer转换为字节数组,java,hibernate,Java,Hibernate,我需要将blob内容写入数据库。我正在为此使用writer对象 即: OutputStreamWriter-writer=getMyWriter 我需要知道如何将这个writer对象转换为字节数组 我需要这个字节数组,因为要创建blob,我将字节数组传递给Hibernate实体。我粘贴了代码以供参考: public void writeBolobSecuredContentToDb() throws Exception { OutputStreamWriter writer = g

我需要将blob内容写入数据库。我正在为此使用writer对象

即: OutputStreamWriter-writer=getMyWriter

我需要知道如何将这个writer对象转换为字节数组

我需要这个字节数组,因为要创建blob,我将字节数组传递给Hibernate实体。我粘贴了代码以供参考:

   public void writeBolobSecuredContentToDb() throws Exception {
    OutputStreamWriter writer = getMyWriter();
    formattedContentToWriter(writer);

   // Writing to Database

     UserPersonal secureData = UserPersonal() 
     userPersonal.fromBytArray("How to convert my writer object to byte array?");
     writer.flush();
     writer.close();
 }

    public OutputStreamWriter getMyWriter() {   
     CipherOutputStream cos = null;
     try {
        cos = getEncrypttedCipherOutputStream();
        return new OutputStreamWriter(cos);
    } catch (Exception e) {
        throw new IllegalStateException("Could not create cipher outputstream.", e);
    }
}


}

//Hibernate entity
public class UserPersonal implements java.io.Serializable
public static Stock fromBytArray(byte[] content) {          
        fileContent.setContent(Hibernate.createBlob(content));
        return (fileContent);
    }

您到底想在数据库中输入什么?从哪里获得要放入数据库的数据?GetEncryptedCipheroutPutStream做什么?您可能想看看从哪里获得数据->formattedContentToWriter字符串内容添加到写入程序I需要将加密内容放入ZIP并在数据库中另存为blob。它通过GetEncryptedCipheroutPutStream发生;您不需要将其转换为字节数组。您需要从Blob中获取一个OutputStream,并将自己的CipherOutputStream、ZipOutputStream、OutputStreamWriter等打包,然后编写数据。你把这件事颠倒过来,前后颠倒。