Selaa lähdekoodia

照片详情内的批量上传

hyy 7 kuukautta sitten
vanhempi
commit
ded5dfa84a

+ 62 - 13
yudao-module-museums/yudao-module-museums-biz/src/main/java/cn/iocoder/yudao/module/museums/service/photos/PhotosServiceImpl.java

@@ -8,9 +8,12 @@ import javax.annotation.Resource;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 
+import java.io.ByteArrayOutputStream;
 import java.sql.Timestamp;
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
 import cn.iocoder.yudao.module.museums.controller.admin.photos.vo.*;
 import cn.iocoder.yudao.module.museums.dal.dataobject.photos.PhotosDO;
@@ -85,30 +88,76 @@ public class PhotosServiceImpl implements PhotosService {
         return photosMapper.selectByGroupId(groupId);
     }
 
+//    @Override
+//    @Transactional(rollbackFor = Exception.class) // 添加事务
+//    public List<Integer> uploadPhotos(Integer groupId, List<MultipartFile> files) throws Exception {
+//        List<Integer> photoIds = new ArrayList<>();
+//
+//        for (MultipartFile file : files) {
+//            // 校验文件类型
+//            if (!file.getOriginalFilename().endsWith(".jpg") && !file.getOriginalFilename().endsWith(".png")&& !file.getOriginalFilename().endsWith(".gif")) {
+//                throw new IllegalArgumentException("上传的文件必须是图片格式 (jpg, png)");
+//            }
+//            // 上传图片并获取URL
+//            String imagePath = fileApi.createFile(file.getBytes());
+//            // 创建照片信息记录
+//            PhotosDO photoRecord = new PhotosDO();
+//            photoRecord.setGroupId(groupId); // 设置照片组ID
+//            photoRecord.setPhotoUrl(imagePath); // 设置照片URL
+//            photosMapper.insert(photoRecord); // 插入照片记录
+//
+//            // 获取并存储新插入的照片 ID
+//            photoIds.add(photoRecord.getId());
+//        }
+//
+//        return photoIds; // 返回所有上传的照片 ID 列表
+//    }
+
     @Override
     @Transactional(rollbackFor = Exception.class) // 添加事务
     public List<Integer> uploadPhotos(Integer groupId, List<MultipartFile> files) throws Exception {
         List<Integer> photoIds = new ArrayList<>();
 
         for (MultipartFile file : files) {
-            // 校验文件类型
-            if (!file.getOriginalFilename().endsWith(".jpg") && !file.getOriginalFilename().endsWith(".png")&& !file.getOriginalFilename().endsWith(".gif")) {
-                throw new IllegalArgumentException("上传的文件必须是图片格式 (jpg, png)");
+            // 校验压缩包类型
+            if (!file.getOriginalFilename().endsWith(".zip")) {
+                throw new IllegalArgumentException("上传的文件必须是压缩包 (zip)");
+            }
+
+            // 解压缩文件并处理图片
+            try (ZipInputStream zis = new ZipInputStream(file.getInputStream())) {
+                ZipEntry zipEntry;
+                while ((zipEntry = zis.getNextEntry()) != null) {
+                    // 检查文件类型,只处理图片
+                    if (zipEntry.getName().endsWith(".jpg") || zipEntry.getName().endsWith(".png") || zipEntry.getName().endsWith(".gif")) {
+                        // 使用 ByteArrayOutputStream 读取文件内容
+                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                        byte[] buffer = new byte[1024];
+                        int length;
+                        while ((length = zis.read(buffer)) != -1) {
+                            bos.write(buffer, 0, length);
+                        }
+                        byte[] imageBytes = bos.toByteArray();
+
+                        // 上传图片并获取 URL
+                        String imagePath = fileApi.createFile(imageBytes);
+
+                        // 创建照片信息记录
+                        PhotosDO photoRecord = new PhotosDO();
+                        photoRecord.setGroupId(groupId); // 设置照片组ID
+                        photoRecord.setPhotoUrl(imagePath); // 设置照片URL
+                        photosMapper.insert(photoRecord); // 插入照片记录
+
+                        // 获取并存储新插入的照片 ID
+                        photoIds.add(photoRecord.getId());
+                    }
+                }
             }
-            // 上传图片并获取URL
-            String imagePath = fileApi.createFile(file.getBytes());
-            // 创建照片信息记录
-            PhotosDO photoRecord = new PhotosDO();
-            photoRecord.setGroupId(groupId); // 设置照片组ID
-            photoRecord.setPhotoUrl(imagePath); // 设置照片URL
-            photosMapper.insert(photoRecord); // 插入照片记录
-
-            // 获取并存储新插入的照片 ID
-            photoIds.add(photoRecord.getId());
         }
 
         return photoIds; // 返回所有上传的照片 ID 列表
     }
 
 
+
 }