2015-11-02 58 views
1

我是mqtt的新手。我啓動一個ActiveMQ服務器並創建一個Java Spring使用者來處理請求。我成功地從終端發佈文本消息,並使用spring消費者進行處理。接下來,我需要通過mqtt發送一個圖像文件。我有一些問題:使用ActimeMQ和Spring Consumer發送並接收mqtt的圖像文件

  1. 是否可以使用mqtt發送圖像文件?
  2. 如何使用我的Spring消費者接收文件?

這是耗時MQTT短信我的Java代碼:

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 
+0

你試過了嗎?發生了什麼,如果你做錯了,錯誤等? –

+0

我得到了解決方案 –

回答

0

代碼發送文件:

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(); 
      } 

     }