Java 使用ActimeMQ和Spring Consumer使用mqtt发送和接收图像文件
我是mqtt的新手。我正在启动一个ActiveMQ服务器并创建一个JavaSpring使用者来处理请求。我正在从终端成功发布一条文本消息,并使用spring consumer进行处理。接下来,我需要通过mqtt发送一个映像文件。我有一些问题:Java 使用ActimeMQ和Spring Consumer使用mqtt发送和接收图像文件,java,spring,activemq,mqtt,spring-jms,Java,Spring,Activemq,Mqtt,Spring Jms,我是mqtt的新手。我正在启动一个ActiveMQ服务器并创建一个JavaSpring使用者来处理请求。我正在从终端成功发布一条文本消息,并使用spring consumer进行处理。接下来,我需要通过mqtt发送一个映像文件。我有一些问题: 是否可以使用mqtt发送图像文件 如何使用我的Spring Consumer接收文件 这是我使用mqtt文本消息的java代码: public void onMessage(Message message) { if (message insta
public void onMessage(Message message) {
if (message instanceof BytesMessage) {
BytesMessage bm = (BytesMessage) message;
byte data[];
data = new byte[(int) bm.getBodyLength()];
bm.readBytes(data);
String msgText = new String(data);
}
}
这是我发送文件的代码:
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
if (message instanceof BytesMessage) {
try {
BytesMessage bm = (BytesMessage) message;
byte data[];
data = new byte[(int) bm.getBodyLength()];
bm.readBytes(data);
FileOutputStream fileOuputStream = new FileOutputStream(
"/tmp/hello.png");
fileOuputStream.write(data);
fileOuputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
发送文件的代码:
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
if (message instanceof BytesMessage) {
try {
BytesMessage bm = (BytesMessage) message;
byte data[];
data = new byte[(int) bm.getBodyLength()];
bm.readBytes(data);
FileOutputStream fileOuputStream = new FileOutputStream(
"/tmp/hello.png");
fileOuputStream.write(data);
fileOuputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
用于接收文件的Spring使用者代码:
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
if (message instanceof BytesMessage) {
try {
BytesMessage bm = (BytesMessage) message;
byte data[];
data = new byte[(int) bm.getBodyLength()];
bm.readBytes(data);
FileOutputStream fileOuputStream = new FileOutputStream(
"/tmp/hello.png");
fileOuputStream.write(data);
fileOuputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
发送文件的代码:
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
if (message instanceof BytesMessage) {
try {
BytesMessage bm = (BytesMessage) message;
byte data[];
data = new byte[(int) bm.getBodyLength()];
bm.readBytes(data);
FileOutputStream fileOuputStream = new FileOutputStream(
"/tmp/hello.png");
fileOuputStream.write(data);
fileOuputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
用于接收文件的Spring使用者代码:
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
mosquitto_pub -d -t test -f /home/abdulmanaf/Pictures/1.png
if (message instanceof BytesMessage) {
try {
BytesMessage bm = (BytesMessage) message;
byte data[];
data = new byte[(int) bm.getBodyLength()];
bm.readBytes(data);
FileOutputStream fileOuputStream = new FileOutputStream(
"/tmp/hello.png");
fileOuputStream.write(data);
fileOuputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
你试过了吗?如果你做了什么,错误等?你试过了吗?如果你犯了错误,会发生什么?