From 61c4c9d4422f3f1aa63754182c5a382283ec1a56 Mon Sep 17 00:00:00 2001 From: bao <19271189822@163.com> Date: Mon, 5 Jan 2026 09:55:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80redis=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 30db5ad7066baefb51e8cfcbdb4291ddab1910d2) --- src/main/resources/application.yml | 4 +- .../dating/common/aliyun/OssUploadTest.java | 304 ++++++++++++++++++ 2 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/bao/dating/common/aliyun/OssUploadTest.java diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8009016..5cc67fa 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -13,8 +13,8 @@ spring: password: JoyeeServe2025 driver-class-name: com.mysql.cj.jdbc.Driver redis: - host: 127.0.0.1 - port: 6379 + host: 110.42.41.177 + port: 6389 password: "" database: 0 timeout: 10000 diff --git a/src/test/java/com/bao/dating/common/aliyun/OssUploadTest.java b/src/test/java/com/bao/dating/common/aliyun/OssUploadTest.java new file mode 100644 index 0000000..7211c09 --- /dev/null +++ b/src/test/java/com/bao/dating/common/aliyun/OssUploadTest.java @@ -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(); + } +} +