在JeroMQ中,如何发送具有内容、文件类型和其他属性的单消息文件。
在客户中:
生成文件消息并发送到服务器
DataInputStream inStrm = file.getContent();
ZMsg msg = ZMsg.load(inStrm);
msg.send(sender);有任何方法来设置该消息的属性吗?像这样:
msg.setProperties("Content-Type", "application/xml");
msg.setProperties("fileName", "abc.pdf");在服务器中,接收文件:
Poller items = new ZMQ.Poller (2);
items.register(receiver, ZMQ.Poller.POLLIN);
while (true) {
try{
items.poll();
if (items.pollin(0)) {
ZMsg msg = ZMsg.recvMsg(receiver);
//save file to disk
}
}catch(Exception e){
LOG.error("Error while receive file: ", e);
}
}发布于 2016-12-21 16:53:26
还有另一种方法。ZeroMq有多部分消息
在我看来,这是非常有用的。在jeromq/jzmq库中,您可以这样使用它:
将文件中的数据存储在字节数组中。创建一个多部分的ZMsg,将所需的所有头和数据放入其中:
ZMsg outMsg = new ZMsg();
outMsg.add(new ZFrame("application/xml"));
outMsg.add(new ZFrame("abc.pdf"));
outMsg.add(new ZFrame(bytes)); // here is the data from file
outMsg.send(outSocket);从另一个套接字接收ZMsg并从中获取所有数据:
ZMsg inMsg = ZMsg.recvMsg(inSocket);
String contentType = inMsg.pop().toString();
String fileName = inMsg.pop().toString();
byte[] fileData = inMsg.pop().getData();或者,您可以通过在一个字节数组中序列化所有必要的标头,并且只使用两个帧等任何其他方便的方式来完成这一任务。
https://stackoverflow.com/questions/41249955
复制相似问题