文件上传下载功能
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package com.njcn.access.utils;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xy
|
||||
*/
|
||||
@Component
|
||||
public class CRC32Utils {
|
||||
|
||||
// CRC-32/MPEG-2 多项式, x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
|
||||
private static final int POLYNOMIAL = 0x04C11DB7;
|
||||
|
||||
public static int calculateCRC32(byte[] buf, int len, int seed) {
|
||||
if (buf == null || len <= 0) {
|
||||
return seed;
|
||||
}
|
||||
|
||||
int crc = seed;
|
||||
int count = 0;
|
||||
// 对长度进行填充以适应32位整数
|
||||
int rem = len % Integer.BYTES;
|
||||
if (rem > 0) {
|
||||
int n = Integer.BYTES - rem;
|
||||
byte[] newBuf = new byte[len + n];
|
||||
System.arraycopy(buf, 0, newBuf, 0, len);
|
||||
// 填充字节用0xFF
|
||||
for (int i = len; i < len + n; i++) {
|
||||
newBuf[i] = (byte) 0xFF;
|
||||
}
|
||||
buf = newBuf;
|
||||
len += n;
|
||||
}
|
||||
|
||||
int uiCount = len / Integer.BYTES;
|
||||
for (int k = 0; k < uiCount; k++) {
|
||||
int uiTemp = 0;
|
||||
for (int i = 0; i < Integer.BYTES; i++) {
|
||||
uiTemp |= (buf[k * Integer.BYTES + i] & 0xFF) << (8 * (3 - i));
|
||||
}
|
||||
for (int j = 0; j < 32; j++) {
|
||||
// 检查最高位是否为1
|
||||
if ((crc ^ uiTemp) < 0) {
|
||||
crc = 0x04C11DB7 ^ (crc << 1);
|
||||
count ++;
|
||||
} else {
|
||||
crc <<= 1;
|
||||
}
|
||||
uiTemp <<= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.njcn.access.utils;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author xy
|
||||
*/
|
||||
@Component
|
||||
public class ChannelObjectUtil {
|
||||
|
||||
/**
|
||||
* 将list转成对应实体
|
||||
* @param object
|
||||
* @param clazz
|
||||
* @return
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> List<T> objectToList(Object object, Class<T> clazz) {
|
||||
List<T> resultList = new ArrayList<>();
|
||||
if (object instanceof List<?>) {
|
||||
for (Object o : (List<?>) object) {
|
||||
resultList.add(clazz.cast(o));
|
||||
}
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将object转成对应实体
|
||||
* @param object
|
||||
* @param clazz
|
||||
* @return
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> T objectToSingleObject(Object object, Class<T> clazz) {
|
||||
if (clazz.isInstance(object)) {
|
||||
return clazz.cast(object);
|
||||
}
|
||||
// 或者抛出异常,根据您的需求
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.njcn.access.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 类的介绍:
|
||||
*
|
||||
* @author xuyang
|
||||
* @version 1.0.0
|
||||
* @createTime 2023/4/10 16:03
|
||||
*/
|
||||
public class JsonUtil {
|
||||
|
||||
/**
|
||||
* @param jsonString 要保存的JSON串
|
||||
* @param filePath 保存到的文件路径
|
||||
* @param fileName 文件名称
|
||||
* @return
|
||||
*/
|
||||
//保存json 文件
|
||||
public static boolean createJsonFile(String jsonString, String filePath, String fileName) {
|
||||
// 标记文件生成是否成功
|
||||
boolean flag = true;
|
||||
// 拼接文件完整路径
|
||||
String fullPath = filePath + File.separator + fileName + ".json";
|
||||
// 生成json格式文件
|
||||
try {
|
||||
// 保证创建一个新文件
|
||||
File file = new File(fullPath);
|
||||
// 如果父目录不存在,创建父目录
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
// 如果已存在,删除旧文件
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
file.createNewFile();
|
||||
// 格式化json字符串
|
||||
//jsonString = JsonFormatTool.formatJson2(jsonString);
|
||||
// 将格式化后的字符串写入文件
|
||||
Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
|
||||
write.write(jsonString);
|
||||
write.flush();
|
||||
write.close();
|
||||
} catch (Exception e) {
|
||||
flag = false;
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 返回是否成功的标记
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机文件名:当前年月日时分秒+五位随机数
|
||||
* @return
|
||||
*/
|
||||
public static String getRandomFileName() {
|
||||
SimpleDateFormat simpleDateFormat;
|
||||
simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
|
||||
Date date = new Date();
|
||||
String str = simpleDateFormat.format(date);
|
||||
Random random = new Random();
|
||||
// 获取5位随机数
|
||||
int ranNum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
|
||||
// 当前时间
|
||||
return ranNum + str;
|
||||
}
|
||||
|
||||
public static String convertStreamToString(InputStream inputStream){
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = null;
|
||||
try {
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.njcn.access.utils;
|
||||
|
||||
import com.alibaba.nacos.shaded.com.google.common.reflect.TypeToken;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
||||
import com.njcn.access.config.MqttInfo;
|
||||
import com.njcn.access.pojo.dto.mqtt.MqttClientDto;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 类的介绍:
|
||||
*
|
||||
* @author xuyang
|
||||
* @version 1.0.0
|
||||
* @createTime 2023/7/14 14:44
|
||||
*/
|
||||
@Data
|
||||
@Order(100)
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
public class MqttUtil {
|
||||
|
||||
private final MqttInfo mqttInfo;
|
||||
|
||||
public boolean judgeClientOnline(String id) {
|
||||
boolean result = false;
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request request = new Request.Builder()
|
||||
.url(mqttInfo.getUrl() + "/api/v5/clients/" + id)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", Credentials.basic(mqttInfo.getUserName(), mqttInfo.getPassword()))
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
response.body();
|
||||
MqttClientDto mqttClientDto = gson.fromJson(Objects.requireNonNull(response.body()).string(), new TypeToken<MqttClientDto>(){}.getType());
|
||||
result = mqttClientDto.isConnected();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user