Bläddra i källkod

Merge remote-tracking branch 'origin/master'

PLF 5 år sedan
förälder
incheckning
a19a64687a
1 ändrade filer med 33 tillägg och 17 borttagningar
  1. 33 17
      src/main/java/com/caimei/utils/ImageUploadUtils.java

+ 33 - 17
src/main/java/com/caimei/utils/ImageUploadUtils.java

@@ -43,22 +43,38 @@ public class ImageUploadUtils {
     @ResponseBody
     @RequestMapping(value = "/imageUpload", method = RequestMethod.POST)
     public JsonModel imageUpload(@RequestBody String imgStr) throws FileNotFoundException {
-        logger.info("--------------------imgStr:"+imgStr);
         JsonModel jsonModel = JsonModel.newInstance();
-        try {
-            String filepath = GenerateImage(imgStr);//解析编码
-            String result = client.uploadFile(filepath);
-            if (!StringUtils.isEmpty(result)) {
-                result = imageDomain + "/" + result;
-                logger.info(">>>>>>>>>>>>>>>>图片上传成功:" + result);
-                jsonModel.success(result);
-            } else {
-                logger.info(">>>>>>>>>>>>>>>>图片上传失败:");
-                jsonModel.error("图片上传失败");
+        logger.info("--------------------imgStr:"+imgStr);
+        if(!StringUtils.isEmpty(imgStr)){
+            if(imgStr.contains(",")){
+                String[] split = imgStr.split(",");
+                String s = split[0];//data:image/png;base64
+                String imgStrType = ".jpg";
+                if(s.contains("image/")){
+                    imgStrType="." + s.substring(s.indexOf("image/")+1, s.indexOf(";"));
+                }
+                imgStr = split[1];
+                try {
+                    String filepath = GenerateImage(imgStr,imgStrType);//解析编码
+                    String result = client.uploadFile(filepath);
+                    if (!StringUtils.isEmpty(result)) {
+                        result = imageDomain + "/" + result;
+                        logger.info(">>>>>>>>>>>>>>>>图片上传成功:" + result);
+                        jsonModel.success(result);
+                    } else {
+                        logger.info(">>>>>>>>>>>>>>>>图片上传失败:");
+                        jsonModel.error("图片上传失败");
+                    }
+                } catch (Exception e) {
+                    logger.info("图片上传异常:" + e.getMessage());
+                    jsonModel.error("图片上传异常");
+                }
+
+            }else{
+                jsonModel.error("图片格式错误");
             }
-        } catch (Exception e) {
-            logger.info("图片上传异常:" + e.getMessage());
-            jsonModel.error("图片上传异常");
+        }else{
+            jsonModel.error("图片为空");
         }
         return jsonModel;
     }
@@ -70,7 +86,7 @@ public class ImageUploadUtils {
         try {
             String imgStr = GetImageStr();
             imgStr = GetImageStr();//获取base64文件编码
-            String filepath = GenerateImage(imgStr);//解析编码
+            String filepath = GenerateImage(imgStr,"jpg");//解析编码
             String result = client.uploadFile(filepath);
             if (!StringUtils.isEmpty(result)) {
                 result = imageDomain + "/" + result;
@@ -117,7 +133,7 @@ public class ImageUploadUtils {
      * @param imgStr
      * @return
      */
-    public String GenerateImage(String imgStr) {
+    public String GenerateImage(String imgStr,String imgStrType) {
         if (imgStr == null) {//图像数据为空
             return null;
         }
@@ -133,7 +149,7 @@ public class ImageUploadUtils {
             //生成jpeg图片
             String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
             logger.info("1--------------" + path);
-            String file = new Random().nextInt(99999999) + ".jpg";
+            String file = new Random().nextInt(99999999) + imgStrType;
             String filePath = "/mnt/newdatadrive/data/custom/autodeploy/client/releasecode/mai/mai@beta@2020010900000000/tempImage/" + file;
             //新生成的图片
             OutputStream out = new FileOutputStream(filePath);