mqtt依赖包引入

This commit is contained in:
xy
2026-04-21 19:21:35 +08:00
parent fed766bca4
commit d8b292d447
6 changed files with 105 additions and 99 deletions

View File

@@ -1,74 +1,74 @@
package com.njcn.csdevice.utils;
import org.eclipse.paho.client.mqttv3.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Description:
* Date: 2023/8/2 13:41【需求编号】
*
* @author clam
* @version V1.0.0
*/
public class MqttTest {
private static final String MQTT_BROKER = "tcp://192.168.1.13:1883";
private static final String MQTT_TOPIC = "file/upload";
private static final String FILE_PATH = "C:\\Users\\无名\\Desktop\\111.json"; // Replace with the path to your file
public static void main(String[] args) {
MqttClient mqttClient = null;
try {
// Connect to the MQTT broker
mqttClient = new MqttClient(MQTT_BROKER, MqttClient.generateClientId());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName("t_user");
connOpts.setPassword("njcnpqs".toCharArray());
mqttClient.connect(connOpts);
// Read the file
File file = new File(FILE_PATH);
FileInputStream fis = new FileInputStream(file);
byte[] fileContent = new byte[(int) file.length()];
fis.read(fileContent);
fis.close();
// Create a new MQTT message
MqttMessage message = new MqttMessage(fileContent);
// Set QoS level and retain flag as per your requirement
message.setQos(1);
// message.setRetained(false);
// Record the start time
long startTime = System.currentTimeMillis();
// Publish the message to the MQTT topic
mqttClient.publish(MQTT_TOPIC, message);
// Record the end time
long endTime = System.currentTimeMillis();
System.out.println("File published successfully!");
System.out.println("Time taken: " + (endTime - startTime) + " ms");
} catch (MqttException | IOException e) {
e.printStackTrace();
} finally {
// Disconnect from the MQTT broker
if (mqttClient != null && mqttClient.isConnected()) {
try {
mqttClient.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
}
}
}
//package com.njcn.csdevice.utils;
//
//import org.eclipse.paho.client.mqttv3.*;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.nio.file.Files;
//import java.nio.file.Paths;
//
///**
// * Description:
// * Date: 2023/8/2 13:41【需求编号】
// *
// * @author clam
// * @version V1.0.0
// */
//public class MqttTest {
// private static final String MQTT_BROKER = "tcp://192.168.1.13:1883";
// private static final String MQTT_TOPIC = "file/upload";
// private static final String FILE_PATH = "C:\\Users\\无名\\Desktop\\111.json"; // Replace with the path to your file
//
// public static void main(String[] args) {
// MqttClient mqttClient = null;
// try {
// // Connect to the MQTT broker
// mqttClient = new MqttClient(MQTT_BROKER, MqttClient.generateClientId());
// MqttConnectOptions connOpts = new MqttConnectOptions();
// connOpts.setUserName("t_user");
// connOpts.setPassword("njcnpqs".toCharArray());
//
// mqttClient.connect(connOpts);
//
// // Read the file
// File file = new File(FILE_PATH);
// FileInputStream fis = new FileInputStream(file);
// byte[] fileContent = new byte[(int) file.length()];
// fis.read(fileContent);
// fis.close();
//
// // Create a new MQTT message
// MqttMessage message = new MqttMessage(fileContent);
//
// // Set QoS level and retain flag as per your requirement
// message.setQos(1);
//// message.setRetained(false);
//
// // Record the start time
// long startTime = System.currentTimeMillis();
//
// // Publish the message to the MQTT topic
// mqttClient.publish(MQTT_TOPIC, message);
//
// // Record the end time
// long endTime = System.currentTimeMillis();
//
// System.out.println("File published successfully!");
// System.out.println("Time taken: " + (endTime - startTime) + " ms");
// } catch (MqttException | IOException e) {
// e.printStackTrace();
// } finally {
// // Disconnect from the MQTT broker
// if (mqttClient != null && mqttClient.isConnected()) {
// try {
// mqttClient.disconnect();
// } catch (MqttException e) {
// e.printStackTrace();
// }
// }
// }
// }
//
//}

View File

@@ -21,6 +21,10 @@
</properties>
<dependencies>
<dependency>
<groupId>com.github.tocrhz</groupId>
<artifactId>mqtt-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.njcn</groupId>
<artifactId>common-web</artifactId>