我们如何找到通过JMS消息接收的文件的名称,我需要知道文件的名称,例如,如果收到名称example.txt的文本文件,我想得到名称“example”通过javax.jms.Message的文件。
这是代码
public void onMessage(Message message, Session session) throws JMSException {
String msg = null;
if (message instanceof TextMessage || message instanceof BytesMessage) {
if (message instanceof BytesMessage) {
BytesMessage byteMessage = (BytesMessage) message;
StringBuffer buffer = new StringBuffer((int)byteMessage.getBodyLength());
byteMessage.
for (int i = 0; i < (int)byteMessage.getBodyLength(); i++) {
buffer.append((char)byteMessage.readByte());
}
msg = buffer.toString().trim();
//for processing the message i need to know the name of the file.
//how to find the name of the file received, like "example" //if received file is "example.txt"
}
else {
getLogger().debug(this.getClass().getName() + ": Received TextMessage");
msg = ((TextMessage)message).getText();
}
getLogger().debug("Request received by " + this.getClass().getName() + " has been processed");
}
答案 0 :(得分:2)
JMS不知道文件是什么。虽然,在发送消息时,您可以使用字符串属性将元数据附加到消息。放置像#34; originalFilename&#34;这样的东西是很常见的。作为邮件发送时的属性。
msg.setStringProperty("originalFilename",filename);
// .. set the bytes of the file somehow
msgProducer.send(msg);
然后,在您的接收代码中,您可以通过
选择它String filename = message.getStringProperty("originalFilename");