修复用户导入文件时能查询却无法导入的bug;新增获取公网ip的方式
This commit is contained in:
parent
704bdf018b
commit
61de3a8c2c
2
pom.xml
2
pom.xml
@ -66,7 +66,7 @@
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.20</version>
|
||||
<version>5.8.26</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@ -1189,7 +1189,7 @@ public class FileService {
|
||||
if(storagePath.startsWith(file.getParentFile().getPath()))
|
||||
fileNode.setPath("");
|
||||
else
|
||||
fileNode.setPath(file.getParentFile().getPath().replace(storagePath, ""));
|
||||
fileNode.setPath(file.getParentFile().getPath().replace(storagePath, "") + "/");
|
||||
response.success(objectMapper.valueToTree(fileNode));
|
||||
} else {
|
||||
ShareFileQueryMessage shareFileQueryMessage = new ShareFileQueryMessage();
|
||||
|
||||
@ -3,7 +3,6 @@ package com.lion.sns.util;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import cn.hutool.system.oshi.CpuInfo;
|
||||
import cn.hutool.system.oshi.OshiUtil;
|
||||
import com.lion.sns.message.StatusMessage;
|
||||
@ -17,14 +16,15 @@ import org.springframework.http.HttpHeaders;
|
||||
import oshi.hardware.*;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Data
|
||||
@ -103,7 +103,16 @@ public class IoUtil {
|
||||
|
||||
public static PairMessage generatePairMessage(){
|
||||
PairMessage pairMessage = new PairMessage();
|
||||
pairMessage.setIp(networkIF.getIPv4addr()[0].split("/")[0]);
|
||||
String ip = networkIF.getIPv4addr()[0].split("/")[0];
|
||||
String privateIPPattern = "(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)";
|
||||
if(Pattern.compile(privateIPPattern).matcher(ip).find())
|
||||
try {
|
||||
ip = getPublicIP();
|
||||
}catch (IOException | URISyntaxException e){
|
||||
log.info("当前机器网卡为内网ip且无法获取公网ip,程序退出");
|
||||
Runtime.getRuntime().exit(1);
|
||||
}
|
||||
pairMessage.setIp(ip);
|
||||
pairMessage.setHostname(NetUtil.getLocalHostName());
|
||||
|
||||
OperatingSystem os = OshiUtil.getOs();
|
||||
@ -117,6 +126,25 @@ public class IoUtil {
|
||||
return pairMessage;
|
||||
}
|
||||
|
||||
public static String getPublicIP() throws IOException, URISyntaxException {
|
||||
// 通过访问一个提供公网IP查询的服务,例如 httpbin.org
|
||||
URL url = new URI("https://httpbin.org/ip").toURL();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
|
||||
// 读取返回的 JSON 数据
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
|
||||
// 从返回的 JSON 数据中提取公网IP
|
||||
String json = response.toString();
|
||||
int startIndex = json.indexOf("\"origin\":") + 10;
|
||||
int endIndex = json.indexOf("\"", startIndex + 1);
|
||||
return json.substring(startIndex + 1, endIndex);
|
||||
}
|
||||
|
||||
public static StatusMessage generateLoadMessage(int id, String path){
|
||||
StatusMessage statusMessage = new StatusMessage();
|
||||
statusMessage.setId(id);
|
||||
|
||||
Binary file not shown.
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user