浏览代码

常用软件关键字存入redis,项目依赖指向阿里云仓库

wutt 5 年之前
父节点
当前提交
3ad65e0e56

+ 19 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/PlatformStartApplication.java

@@ -1,8 +1,14 @@
 package com.management.platform;
 package com.management.platform;
 
 
 import org.mybatis.spring.annotation.MapperScan;
 import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
 
 
 /**
 /**
  * Author: 吴涛涛
  * Author: 吴涛涛
@@ -16,4 +22,17 @@ public class PlatformStartApplication {
     public static void main(String[] args) {
     public static void main(String[] args) {
         SpringApplication.run(PlatformStartApplication.class,args);
         SpringApplication.run(PlatformStartApplication.class,args);
     }
     }
+
+    //防止存入redis数据后乱码
+    @Bean(name = "redisTemplate")
+    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
+        RedisTemplate<String, String> template = new RedisTemplate<>();
+        template.setConnectionFactory(factory);
+        template.setKeySerializer(new StringRedisSerializer());
+        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+        template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
+        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
+        template.afterPropertiesSet();
+        return template;
+    }
 }
 }

+ 7 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/test/TestApplicationTests.java

@@ -24,10 +24,16 @@ public class TestApplicationTests {
 //        Map<String,Object> map = AuthService.getAuth(Constant.API_KEY, Constant.SECRET_KEY);
 //        Map<String,Object> map = AuthService.getAuth(Constant.API_KEY, Constant.SECRET_KEY);
 //        String accessToken = (String)map.get("access_token");
 //        String accessToken = (String)map.get("access_token");
 //        Long expiresTimeOut = (Long)map.get("expires_in");
 //        Long expiresTimeOut = (Long)map.get("expires_in");
+//        System.out.println("accessToken:==="+accessToken);
+//        System.out.println("expiresTimeOut:==="+expiresTimeOut);
 //        redisUtil.setKeyWithExpireTime("accessToken",accessToken,expiresTimeOut);
 //        redisUtil.setKeyWithExpireTime("accessToken",accessToken,expiresTimeOut);
+////        redisUtil.setString("wtt","aaa");
 //        System.out.println(redisUtil.getKey("accessToken"));
 //        System.out.println(redisUtil.getKey("accessToken"));
-//        System.out.println(path);
 
 
+//        for (String keyWord : Constant.keyWords) {
+//            redisUtil.sSet("keyWords",keyWord);
+//        }
+//        System.out.println(redisUtil.members("keyWords").toString());
     }
     }
 
 
 }
 }

+ 4 - 3
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/AuthService.java

@@ -2,8 +2,6 @@ package com.management.platform.util;
 
 
 import com.management.platform.constant.Constant;
 import com.management.platform.constant.Constant;
 import org.json.JSONObject;
 import org.json.JSONObject;
-import org.springframework.stereotype.Component;
-
 import java.io.BufferedReader;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.HttpURLConnection;
@@ -13,7 +11,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
 /**
 /**
- * 获取token类
+ * Author: 吴涛涛 cuiyi@itany.com
+ * Date : 2019 - 08 - 30 13:59
+ * Description:<描述> 百度api识别图片里的文字,获取token类工具类
+ * Version: 1.0
  */
  */
 public class AuthService {
 public class AuthService {
     /**
     /**

+ 53 - 4
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/CheckPicUtil.java

@@ -1,16 +1,28 @@
 package com.management.platform.util;
 package com.management.platform.util;
 
 
 
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baidu.aip.util.Base64Util;
 import com.baidu.aip.util.Base64Util;
 
 
 import java.net.URLEncoder;
 import java.net.URLEncoder;
+import java.util.HashSet;
+import java.util.Set;
 
 
+/**
+ * Author: 吴涛涛 cuiyi@itany.com
+ * Date : 2019 - 08 - 30 13:59
+ * Description:<描述> 百度文字识别api识别图片里的文字工具类
+ * Version: 1.0
+ */
 public class CheckPicUtil {
 public class CheckPicUtil {
 
 
     /**
     /**
-    * 重要提示代码中所需工具类
-    * 下载
-    */
+     * 调用百度文字识别api识别图片里的文字
+     * @param path 图片路径
+     * @param token 调用百度文字识别api的access_token
+     * @return 返回调用api识别出来的内容字符串
+     */
     public static String general(String path,String token) {
     public static String general(String path,String token) {
         // 请求url
         // 请求url
         String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
         String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
@@ -34,8 +46,45 @@ public class CheckPicUtil {
         }
         }
         return null;
         return null;
     }
     }
+    /**
+     * 调用百度文字识别api识别图片里的文字
+     * @param path 图片路径
+     * @param token 调用百度文字识别api的access_token
+     * @return 返回调用api识别出来的内容的set集合
+     */
+    public static Set<String>  generalPicTextContent (String path,String token) {
+        // 请求url
+        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general";
+        try {
+            // 本地文件路径
+            String filePath = path;
+            byte[] imgData = FileUtil.readFileByBytes(filePath);
+            String imgStr = Base64Util.encode(imgData);
+            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
+
+            String param = "image=" + imgParam;
+
+            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
+            String accessToken = token;
+
+            String result = HttpUtil.post(url, accessToken, param);
+            Set<String> wordsList = new HashSet<>();
+            JSONObject object = JSONArray.parseObject(result);
+            JSONArray resultArray = object.getJSONArray("words_result");
+            for (int i = 0; i < resultArray.size(); i++) {
+                JSONObject json  = resultArray.getJSONObject(i);
+                String words = (String)json.get("words");
+                wordsList.add(words);
+            }
+            System.out.println(wordsList);
+            return wordsList;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
 
     public static void main(String[] args) {
     public static void main(String[] args) {
-        CheckPicUtil.general("D:\\1.png",(String)AuthService.getAuth().get("access_token"));
+        CheckPicUtil.generalPicTextContent("D:\\360MoveData\\Users\\Administrator\\Desktop\\idea.jpg",(String)AuthService.getAuth().get("access_token"));
     }
     }
 }
 }

+ 23 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/RedisUtil.java

@@ -3,6 +3,7 @@ package com.management.platform.util;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.connection.DataType;
 import org.springframework.data.redis.connection.DataType;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.SetOperations;
 import org.springframework.data.redis.core.script.DefaultRedisScript;
 import org.springframework.data.redis.core.script.DefaultRedisScript;
 import org.springframework.data.redis.core.script.RedisScript;
 import org.springframework.data.redis.core.script.RedisScript;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
@@ -29,9 +30,30 @@ public class RedisUtil {
     }
     }
 
 
     public void setKeyWithExpireTime(String key,String value,long timeout){
     public void setKeyWithExpireTime(String key,String value,long timeout){
-        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS);
+        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
     }
     }
 
 
+    /**
+     * 将数据放入set缓存
+     *
+     * @param key 键
+     * @return
+     */
+    public void sSet(String key, String value) {
+        redisTemplate.opsForSet().add(key, value);
+    }
+
+    /**
+     * 获取变量中的值
+     *
+     * @param key 键
+     * @return
+     */
+    public Set<Object> members(String key) {
+        return redisTemplate.opsForSet().members(key);
+    }
+
+
     /**
     /**
      * 指定缓存失效时间
      * 指定缓存失效时间
      *
      *

+ 14 - 0
fhKeeper/formulahousekeeper/pom.xml

@@ -40,6 +40,20 @@
         <fastjson.version>1.2.7</fastjson.version>
         <fastjson.version>1.2.7</fastjson.version>
         <baidu.api.sdk.version>4.2.0</baidu.api.sdk.version>
         <baidu.api.sdk.version>4.2.0</baidu.api.sdk.version>
     </properties>
     </properties>
+
+    <repositories>
+        <repository>
+            <id>alimaven</id>
+            <name>aliyun maven</name>
+            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
+        </repository>
+        <repository>
+            <id>alimaven2</id>
+            <name>aliyun maven 2</name>
+            <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
+        </repository>
+    </repositories>
+
     <!--统一管理依赖-->
     <!--统一管理依赖-->
     <dependencyManagement>
     <dependencyManagement>
         <dependencies>
         <dependencies>