有关内网问题 解决

This commit is contained in:
lizhenhua 2025-11-15 11:24:24 +08:00
parent 1511465eb5
commit 52cc0709aa
3 changed files with 59 additions and 24 deletions

View File

@ -1,6 +1,8 @@
package com.bonus.file.utils;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.ip.IpUtils;
import com.bonus.file.config.MinioConfig;
import com.bonus.file.entity.FileDetails;
import com.bonus.system.api.domain.SysFile;
@ -15,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@ -300,32 +303,36 @@ public class MinioUtil {
return getFileUrl(bucketName, objectName, 604800);
}
public String getFullFileUrl(String fileUrl) throws Exception {
if (StringUtils.isNotEmpty(fileUrl)){
if (fileUrl.startsWith("http://")) {
return fileUrl;
}
// 获取当前访问系统的 IP通过配置或工具方法获取
String currentIp = InetAddress.getLocalHost().getHostAddress();
// 判断访问来源是内网还是外网
boolean isIntranet = currentIp.startsWith("10.");
// 根据来源返回不同的访问地址
String endpoint;
if (isIntranet) {
// 内网访问比如你给的 10.145.136.229
endpoint = minioConfig.getEndpointInter();
} else {
// 外网或局域网访问比如你给的 192.168.20.234
endpoint = minioConfig.getEndpoint();
}
// 拼接完整路径MinIO 地址
return endpoint + "/" + minioConfig.getBucketName() + "/" + fileUrl;
} else {
public String getFullFileUrl(String fileUrl) throws Exception {
// fileUrl 为空直接返回空字符串
if (StringUtils.isBlank(fileUrl)) {
return "";
}
fileUrl = fileUrl.trim();
// 如果已经是完整 URL直接返回
if (fileUrl.startsWith("http://") || fileUrl.startsWith("https://")) {
return fileUrl;
}
// 获取当前访问系统的客户端 IP若依框架工具
HttpServletRequest request = ServletUtils.getRequest();
String clientIp = IpUtils.getIpAddr(request);
// 根据访问来源选择不同的 endpoint
String endpoint;
if (clientIp != null && clientIp.startsWith("10.")) {
// 内网访问
endpoint = minioConfig.getEndpointInter();
} else {
// 局域网或外网访问
endpoint = minioConfig.getEndpoint();
}
// 拼接完整 MinIO URL
return endpoint + "/" + minioConfig.getBucketName() + "/" + fileUrl;
}

View File

@ -25,6 +25,7 @@ import com.bonus.system.mapper.*;
import com.bonus.system.service.ISysConfigService;
import com.bonus.system.service.ISysDeptService;
import com.bonus.system.service.ISysUserService;
import com.bonus.system.utils.FileCommonUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -120,6 +121,7 @@ public class SysUserServiceImpl implements ISysUserService {
List<SysUser> sysUsers = userMapper.selectUserList(user);
for (SysUser sysUser : sysUsers) {
sysUser.setPhotoUrl(remoteFileService.getFullFileUrl(FileCommonUtils.getFullFileUrl(sysUser.getPhotoUrl())));
sysUser.setPhotoUrl(remoteFileService.getFullFileUrl(sysUser.getPhotoUrl()));
}
if(user.getQueryTyoe()==1){

View File

@ -0,0 +1,26 @@
package com.bonus.system.utils;
public class FileCommonUtils {
/**
* 处理图片路径判断前缀并返回完整访问路径
* @param fileUrl 上传路径 uploads/2025/09/28/xxx.png 或完整 URL
* @return 返回拼接好 IP 的完整路径
*/
public static String getFullFileUrl(String fileUrl) {
if (fileUrl == null || fileUrl.trim().isEmpty()) {
return "";
}
fileUrl = fileUrl.trim();
int index = fileUrl.indexOf("uploads/");
if (index != -1) {
// 存在 uploads/截取路径
return fileUrl.substring(index);
} else {
// 不存在 uploads/直接返回原始 URL
return fileUrl;
}
}
}