统一redis服务器

(cherry picked from commit 30db5ad706)
This commit is contained in:
bao
2026-01-05 09:55:41 +08:00
committed by KilLze
parent 717c0a0507
commit 61c4c9d442
2 changed files with 306 additions and 2 deletions

View File

@@ -13,8 +13,8 @@ spring:
password: JoyeeServe2025 password: JoyeeServe2025
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
redis: redis:
host: 127.0.0.1 host: 110.42.41.177
port: 6379 port: 6389
password: "" password: ""
database: 0 database: 0
timeout: 10000 timeout: 10000

View File

@@ -0,0 +1,304 @@
package com.bao.dating.common.aliyun;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
/**
* OSS图片上传测试类
* @author KilLze
*/
@SpringBootTest
public class OssUploadTest {
@Autowired
private AliOssUtil aliOssUtil;
/**
* 测试上传本地图片文件
*/
@Test
public void testUploadLocalImage() {
try {
// 本地图片文件路径(请修改为实际存在的图片路径)
String localImagePath = "D:/image/image.jpg"; // 请修改为实际路径
File imageFile = new File(localImagePath);
if (!imageFile.exists()) {
System.out.println("图片文件不存在: " + localImagePath);
System.out.println("请修改localImagePath为实际存在的图片路径");
return;
}
// 读取文件字节
byte[] imageBytes = Files.readAllBytes(Paths.get(localImagePath));
// 生成OSS对象名称使用时间戳和UUID确保唯一性
String objectName = generateObjectName("test", "jpg");
// 上传到OSS
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
System.out.println("========== 本地图片上传测试 ==========");
System.out.println("本地文件路径: " + localImagePath);
System.out.println("文件大小: " + imageBytes.length + " 字节");
System.out.println("OSS对象名称: " + objectName);
System.out.println("文件访问URL: " + fileUrl);
System.out.println("上传结果: 成功");
System.out.println("=====================================");
} catch (IOException e) {
System.out.println("上传失败: " + e.getMessage());
e.printStackTrace();
}
}
/**
* 测试从网络URL下载图片并上传到OSS
*/
@Test
public void testUploadImageFromUrl() {
try {
// 网络图片URL请修改为实际的图片URL
String imageUrl = "https://example.com/image.jpg"; // 请修改为实际URL
System.out.println("========== 网络图片上传测试 ==========");
System.out.println("开始下载图片: " + imageUrl);
// 从URL下载图片
URL url = new URL(imageUrl);
try (InputStream inputStream = url.openStream()) {
byte[] imageBytes = readAllBytes(inputStream);
// 从URL中提取文件扩展名
String extension = getFileExtensionFromUrl(imageUrl);
if (extension.isEmpty()) {
extension = "jpg"; // 默认扩展名
}
// 生成OSS对象名称
String objectName = generateObjectName("download", extension);
// 上传到OSS
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
System.out.println("图片下载成功");
System.out.println("文件大小: " + imageBytes.length + " 字节");
System.out.println("OSS对象名称: " + objectName);
System.out.println("文件访问URL: " + fileUrl);
System.out.println("上传结果: 成功");
}
System.out.println("=====================================");
} catch (Exception e) {
System.out.println("上传失败: " + e.getMessage());
e.printStackTrace();
}
}
/**
* 测试上传不同格式的图片
*/
@Test
public void testUploadDifferentImageFormats() {
String[] imagePaths = {
"D:/test/image1.jpg", // 请修改为实际路径
"D:/test/image2.png", // 请修改为实际路径
"D:/test/image3.gif" // 请修改为实际路径
};
System.out.println("========== 多格式图片上传测试 ==========");
for (String imagePath : imagePaths) {
try {
File imageFile = new File(imagePath);
if (!imageFile.exists()) {
System.out.println("跳过不存在的文件: " + imagePath);
continue;
}
// 读取文件
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
// 获取文件扩展名
String extension = getFileExtension(imagePath);
// 生成OSS对象名称
String objectName = generateObjectName("format-test", extension);
// 上传
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
System.out.println("\n文件: " + imagePath);
System.out.println("格式: " + extension);
System.out.println("大小: " + imageBytes.length + " 字节");
System.out.println("URL: " + fileUrl);
System.out.println("状态: 成功");
} catch (Exception e) {
System.out.println("\n文件: " + imagePath);
System.out.println("状态: 失败 - " + e.getMessage());
}
}
System.out.println("\n=====================================");
}
/**
* 测试上传图片到指定目录
*/
@Test
public void testUploadImageToDirectory() {
try {
// 本地图片文件路径
String localImagePath = "D:/test/image.jpg"; // 请修改为实际路径
File imageFile = new File(localImagePath);
if (!imageFile.exists()) {
System.out.println("图片文件不存在: " + localImagePath);
return;
}
// 读取文件
byte[] imageBytes = Files.readAllBytes(Paths.get(localImagePath));
// 上传到指定目录例如images/2025/12/24/xxx.jpg
String directory = "images/" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
String fileName = UUID.randomUUID().toString() + ".jpg";
String objectName = directory + "/" + fileName;
// 上传
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
System.out.println("========== 目录上传测试 ==========");
System.out.println("目录: " + directory);
System.out.println("文件名: " + fileName);
System.out.println("完整路径: " + objectName);
System.out.println("文件访问URL: " + fileUrl);
System.out.println("上传结果: 成功");
System.out.println("=================================");
} catch (Exception e) {
System.out.println("上传失败: " + e.getMessage());
e.printStackTrace();
}
}
/**
* 测试使用FileInputStream上传
*/
@Test
public void testUploadWithFileInputStream() {
try {
// 本地图片文件路径
String localImagePath = "D:/test/image.jpg"; // 请修改为实际路径
File imageFile = new File(localImagePath);
if (!imageFile.exists()) {
System.out.println("图片文件不存在: " + localImagePath);
return;
}
// 使用FileInputStream读取文件
try (FileInputStream fis = new FileInputStream(imageFile)) {
byte[] imageBytes = new byte[(int) imageFile.length()];
fis.read(imageBytes);
// 生成OSS对象名称
String objectName = generateObjectName("stream", "jpg");
// 上传
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
System.out.println("========== FileInputStream上传测试 ==========");
System.out.println("文件路径: " + localImagePath);
System.out.println("文件大小: " + imageBytes.length + " 字节");
System.out.println("OSS对象名称: " + objectName);
System.out.println("文件访问URL: " + fileUrl);
System.out.println("上传结果: 成功");
System.out.println("===========================================");
}
} catch (Exception e) {
System.out.println("上传失败: " + e.getMessage());
e.printStackTrace();
}
}
/**
* 生成OSS对象名称
* @param prefix 前缀
* @param extension 文件扩展名
* @return 对象名称
*/
private String generateObjectName(String prefix, String extension) {
// 格式: prefix/yyyyMMdd/HHmmss-uuid.extension
String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd/HHmmss"));
String uuid = UUID.randomUUID().toString().substring(0, 8);
return String.format("%s/%s-%s.%s", prefix, dateTime, uuid, extension);
}
/**
* 从文件路径获取扩展名
* @param filePath 文件路径
* @return 扩展名
*/
private String getFileExtension(String filePath) {
int lastDot = filePath.lastIndexOf('.');
if (lastDot > 0 && lastDot < filePath.length() - 1) {
return filePath.substring(lastDot + 1).toLowerCase();
}
return "jpg"; // 默认扩展名
}
/**
* 从URL获取文件扩展名
* @param url URL地址
* @return 扩展名
*/
private String getFileExtensionFromUrl(String url) {
try {
// 移除查询参数
String path = url.split("\\?")[0];
int lastDot = path.lastIndexOf('.');
int lastSlash = path.lastIndexOf('/');
if (lastDot > lastSlash && lastDot < path.length() - 1) {
return path.substring(lastDot + 1).toLowerCase();
}
} catch (Exception e) {
// 忽略异常
}
return "jpg"; // 默认扩展名
}
/**
* 从InputStream读取所有字节兼容方法
* @param inputStream 输入流
* @return 字节数组
* @throws IOException IO异常
*/
private byte[] readAllBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[8192]; // 8KB缓冲区
int nRead;
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
}
}