JMS onMessage ByteMessage写入PDF文件

时间:2011-02-24 21:40:56

标签: spring jms byte

我有一个接收BytesMessage / JMSBytesMessage的Spring JMS侦听器。 我想将此消息转换为PDF文件并将其写入驱动器。

public void onMessage(Message message) {

BytesMessage bmsg = (BytesMessage) message;

ByteArrayOutputStream bout = new ByteArrayOutputStream();


}

我知道我需要做一些像msg.readBytes这样的事情,但是我把所有东西放在一起都很麻烦......有人可能提供一些提示。

感谢

1 个答案:

答案 0 :(得分:2)

像这样简单的东西应该有效:

public void onMessage(Message message) {
   try {
      BytesMessage bytesMessage = (BytesMessage) message;

      // copy data into a byte[] buffer
      int dataSize = (int) bytesMessage.getBodyLength();
      byte[] buffer = new byte[dataSize];
      bytesMessage.readBytes(buffer, dataSize);

      // now write the buffer to a file
      File outputFile = new File("/path/to/file.pdf");
      FileOutputStream fileOutput = new FileOutputStream(outputFile);
      try {
         fileOutput.write(buffer);
      } finally {
         fileOutput.close();
      }
   } catch (Exception ex) {
      // handle exception
   }
}

只要数据大小不是很大,这应该可以正常工作。

相关问题