feat(修改): ♻️ 工具类修改

This commit is contained in:
bunny 2024-06-13 09:35:19 +08:00
parent e526eefddf
commit 64d9d1c682
3 changed files with 36 additions and 58 deletions

View File

@ -1,57 +0,0 @@
package cn.bunny.common.service.utils;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.pojo.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import java.net.Inet6Address;
import java.net.InetAddress;
public class RequestUtil {
public static String getHttpIpAddress(HttpServletRequest request) {
String ipv6Address = request.getRemoteAddr();
try {
InetAddress inetAddress = InetAddress.getByName(ipv6Address);
if (inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress()) {
System.out.println("IPv4 Address: " + inetAddress.getHostAddress());
} else {
InetAddress ipv4Address = Inet6Address.getByAddress(null, inetAddress.getAddress(), 0);
System.out.println("IPv4 Address: " + ipv4Address.getHostAddress());
}
} catch (Exception e) {
throw new BunnyException(ResultCodeEnum.SERVICE_ERROR);
}
String ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_FORWARDED");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("REMOTE_ADDR");
}
return ipAddress;
}
}

View File

@ -6,7 +6,7 @@ import lombok.Data;
public class FileMessageConstant {
public static final String DOWNLOAD_BUCKET_EXCEPTION = "下载文件失败";
public static final String FILE_UPLOAD_EXCEPTION = "文件上传失败";
public static final String BUCKET_EXISTS_EXCEPTION = "查询文化部对象失败";
public static final String BUCKET_EXISTS_EXCEPTION = "查询文对象失败";
public static final String DELETE_BUCKET_EXCEPTION = "删除文件对象失败";
public static final String FILE_IS_EMPTY = "文件信息为空";
public static final String FILE_IS_NOT_EXITS = "文件信息为空";

View File

@ -1,6 +1,7 @@
package cn.bunny.module.minio.utils;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.module.minio.properties.MinioProperties;
import cn.bunny.pojo.result.constant.FileMessageConstant;
import io.minio.*;
import io.minio.messages.*;
@ -19,9 +20,43 @@ import java.util.Map;
@Component
@Slf4j
public class MinioUtil {
@Autowired
private MinioProperties properties;
@Autowired
private MinioClient minioClient;
/**
* 获取Minio全路径名
*
* @param objectName 对象名称
* @return 全路径
*/
public String getFullPath(String objectName) {
String url = properties.getEndpointUrl();
String bucketName = properties.getBucketName();
return url + "/" + bucketName + objectName;
}
/**
* 获取文件并返回字节数组
*
* @param bucketName 桶名称
* @param objectName 对象名称
* @return 文件流对象
*/
public byte[] getBucketObjectByte(String bucketName, String objectName) {
try {
GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
log.info("获取文件 ------ 成功");
return getObjectResponse.readAllBytes();
} catch (Exception exception) {
log.error("获取文件 ------ 失败消息:{}", exception.getLocalizedMessage());
exception.getStackTrace();
}
throw new BunnyException(FileMessageConstant.GET_BUCKET_EXCEPTION);
}
/**
* 判断桶是否存在
*