Explorar el Código

增加 100m图片计算错误异常

wuweihao hace 4 años
padre
commit
66ee17eecb

+ 3 - 0
720yun_fd_manage/gis_common/src/main/java/com/gis/common/constant/MsgCode.java

@@ -9,4 +9,7 @@ public class MsgCode {
 
     public final static Integer e3001 = 3001;
     public final static String m3001 = "场景已使用";
+
+    public final static Integer e5003 = 5003;
+    public final static String m5003 = "不支持此图片";
 }

+ 24 - 42
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -460,43 +460,33 @@ public class FileUtils {
 
     /**
      * 使用java 生成缩略图
-     * @param inputFilePath
      * @param ossBasePath
      * @param ossDomain
      * @return
      */
-    public  String compressImgAndUploadOss2(String inputFilePath, String ossBasePath, String ossDomain){
-        String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
-
-        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
-        String suffix = StringUtils.substringAfterLast(inputFilePath, ".");
-        String fileName ="/thumb_"+ time  + "."  +suffix;
-
-        // 保存图片位置
-        String saveCompressImgPath = serverBasePath + fileName;
-        String ossUrl = null;
-        try {
-            Img.from(new File(inputFilePath)).scale(600, 300).write(new File(saveCompressImgPath));
-            log.info("图片压缩成功: " + saveCompressImgPath);
-
-            if (FileUtil.isFile(saveCompressImgPath)) {
-                // 上传oss
-                String ossPath = ossBasePath + fileName;
-                log.info("ossPath: " + ossPath);
-//                asyncTask.uploadOss(saveCompressImgPath, ossPath);
-                aliyunOssUtil.upload(saveCompressImgPath, ossPath);
-                ossUrl = ossDomain + ossPath;
-                log.info("图片上传成功: " + ossUrl);
-            } else {
-                log.error("缩略图不存在: " + saveCompressImgPath);
-            }
-
-        } catch (Exception e) {
-            log.error("图片格式有误,不支持此图片");
-            e.printStackTrace();
-        }
-        return ossUrl;
-    }
+//    public  String compressImgAndUploadOss2(MultipartFile file, String ossBasePath, String ossDomain, Integer width, Integer height, String imgName) throws Exception {
+//        // 保存图片位置
+//        String saveCompressImgPath = ossBasePath + imgName;
+//        String ossUrl = null;
+//
+////            Img.from(new File(file.getInputStream())).scale(width, height).write(new File(saveCompressImgPath));
+//            Img.from(new File("")).scale(width, height).write(new File(saveCompressImgPath));
+//            log.info("图片压缩成功: " + saveCompressImgPath);
+//
+//            if (FileUtil.isFile(saveCompressImgPath)) {
+//                // 上传oss
+//                String ossPath = ossBasePath + imgName;
+//                log.info("ossPath: " + ossPath);
+//                aliyunOssUtil.upload(saveCompressImgPath, ossPath);
+//                ossUrl = ossDomain + ossPath;
+//                log.info("图片上传成功: " + ossUrl);
+//            } else {
+//                log.error("缩略图不存在: " + saveCompressImgPath);
+//            }
+//
+//
+//        return ossUrl;
+//    }
 
 
     /**
@@ -512,14 +502,10 @@ public class FileUtils {
     public  String compressImgAndUploadOss2(String inputFilePath, String ossBasePath, String ossDomain, Integer width, Integer height, String imgName){
         String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
 
-//        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
-//        String suffix = StringUtils.substringAfterLast(inputFilePath, ".");
-//        String fileName ="/thumb_"+ time  + "."  +suffix;
-
         // 保存图片位置
         String saveCompressImgPath = serverBasePath + imgName;
         String ossUrl = null;
-        try {
+
             Img.from(new File(inputFilePath)).scale(width, height).write(new File(saveCompressImgPath));
             log.info("图片压缩成功: " + saveCompressImgPath);
 
@@ -534,10 +520,6 @@ public class FileUtils {
                 log.error("缩略图不存在: " + saveCompressImgPath);
             }
 
-        } catch (Exception e) {
-            log.error("图片格式有误,不支持此图片");
-            e.printStackTrace();
-        }
         return ossUrl;
     }
 

+ 2 - 1
720yun_fd_manage/gis_service/src/main/java/com/gis/service/FodderService.java

@@ -7,6 +7,7 @@ import com.gis.domain.dto.PageDto;
 import com.gis.domain.entity.FodderEntity;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.List;
 
 
@@ -17,7 +18,7 @@ public interface FodderService extends IBaseService<FodderEntity, Long> {
 
 
 
-    Result upload(MultipartFile file, String type);
+    Result upload(MultipartFile file, String type) ;
 
     List<FodderEntity> findByType(String userId, String type);
 

+ 35 - 31
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/FodderServiceImpl.java

@@ -5,6 +5,8 @@ import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import com.gis.common.config.RabbitConfig;
 import com.gis.common.constant.ConfigConstant;
+import com.gis.common.constant.MsgCode;
+import com.gis.common.exception.BaseRuntimeException;
 import com.gis.common.task.AsyncTask;
 import com.gis.common.util.*;
 import com.gis.domain.dto.*;
@@ -69,7 +71,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
 
     @Override
-    public Result upload(MultipartFile file, String type) {
+    public Result upload(MultipartFile file, String type)  {
         // 检查非法文件上传
         boolean checkFile = FileUtils.checkFile(file);
         if (!checkFile) {
@@ -96,18 +98,22 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
         String ossPreviewIcon = null;
 
         FodderEntity entity = new FodderEntity();
+        try {
         if (type.equals("pano")) {
             String sceneCode = RandomUtils.getSceneCode("fd720_");
             newName = sceneCode+"." + suffix;
             savePath =savePath + sceneCode + "/" + newName;
 
-            try {
+
+
                 FileUtil.writeFromStream(file.getInputStream(), savePath);
-                log.info("pano serverPath: " + savePath);
+
+            log.info("pano serverPath: " + savePath);
                 log.info("全景图片写入完成");
 
                 // 压缩图片并上传oss
-                iconPath = fileUtils.compressImgAndUploadOss2(savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain);
+                iconPath = fileUtils.compressImgAndUploadOss2(
+                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
 
                 // 全景图的预览图
                 ossPreviewIcon = fileUtils.compressImgAndUploadOss2(
@@ -116,9 +122,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
                 ossUrl = configConstant.ossDomain+configConstant.ossBasePath + sceneCode;
 
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+
 
             entity.setSceneCode(sceneCode);
             entity.setFilePath(savePath);
@@ -126,7 +130,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
         } else {
             ossUrl = configConstant.ossDomain+ossPath;
-            try {
+
                 if (type.equals("image")) {
                     // 图片大于1MB生成缩略图, 小于1MB使用原图作为缩略图
                     if (size > 1024) {
@@ -143,35 +147,35 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
                 // 普通素材直接上传oss, 服务器不保留文件
                 aliyunOssUtil.upload(file.getBytes(), ossPath);
 
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-
 
-
-        entity.setType(type);
-        entity.setName(fileName);
-        entity.setFileName(newName);
-        log.info("ossUrl: " + ossUrl);
-        log.info("iconPath:" + iconPath);
-
-        entity.setOssPath(ossUrl);
-        entity.setIcon(iconPath);
-        entity.setUserId(getUserNameForToken());
-        entity.setFileSize(size+"");
-        entity.setDpi(dpi);
-        entity.setPreviewIcon(ossPreviewIcon);
-
-        save(entity);
-
-
-        if (type.equals("pano")) {
+        }
+            entity.setType(type);
+            entity.setName(fileName);
+            entity.setFileName(newName);
+            log.info("ossUrl: " + ossUrl);
+            log.info("iconPath:" + iconPath);
+
+            entity.setOssPath(ossUrl);
+            entity.setIcon(iconPath);
+            entity.setUserId(getUserNameForToken());
+            entity.setFileSize(size+"");
+            entity.setDpi(dpi);
+            entity.setPreviewIcon(ossPreviewIcon);
+
+            save(entity);
+
+            if (type.equals("pano")) {
             //发消息到mq
             rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
             log.info("发送消息到队列完成: " + entity.getId());
         }
 
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error(e.getMessage());
+            throw new BaseRuntimeException(MsgCode.e5003, "不支持此图片");
+        }
+
         return Result.success(entity);
     }
 

+ 2 - 0
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/FodderController.java

@@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.Date;
 import java.util.List;
 
@@ -32,6 +33,7 @@ import java.util.List;
 @Api(tags = "我的素材")
 @RestController
 @RequestMapping("manage/fodder")
+//@RequestMapping("a/fodder")
 public class FodderController extends BaseController {
 
     @Autowired