package com.qiwuai.garden.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.qiwuai.garden.common.TimingAcquisition; import com.qiwuai.garden.dto.RegionIPDto; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.system.ApplicationHome; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.yaml.snakeyaml.Yaml; import java.io.*; import java.net.URL; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @Component @Data public class YamlUtil { private static LinkedHashMap>> properties = new LinkedHashMap<>(); /** * 将话术转成map格式 * @param * @return Map>> */ public static Map>> loadSpeechToMap(String speechFilePath, String fileName) { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + fileName); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 读取配置文件yml中的某个值,只支持取第一层的值 * @param * @return Map>> */ public static String getValueByKey(String key) { String value = ""; Map applicationMap = TimingAcquisition.applicationMap; if (applicationMap.size() != 0) { return String.valueOf(applicationMap.get(key)); } else { try { Yaml yaml = new Yaml(); ClassPathResource resource = new ClassPathResource("\\application.yml"); properties = yaml.load(resource.getInputStream()); String val = properties.get("spring").get("profiles").get("active"); // ClassPathResource resourceNext = new ClassPathResource("\\application-" + val + ".yml"); // Map map = yaml.load(resourceNext.getInputStream()); FileInputStream fileInputStream = new FileInputStream("/home/jiaoyou/projects/secretGarden/application-" + val + ".yml"); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); Map map = yaml.load(inputStreamReader); applicationMap.putAll(map); value = String.valueOf(map.get(key)); } catch (Exception e) { e.printStackTrace(); } } return value; } public static void main(String[] args) throws FileNotFoundException { // Map>> speech = plantSpeechToMap(); // System.out.println("yaml文件读取结果:" + plantSpeechToMap()); // Integer i = RandomDropUtil.getRandomNum(0, speech.get("man").get("plant_fail_near_plant").size()); // System.out.println("随机数i:" + i); // System.out.println("植物因没有伴生宠物种植失败的回复:" + speech.get("man").get("plant_fail_near_plant").get(i)); System.out.println("从yml文件取出的值" + getValueByKey("userFilePath")); Map> IPRegionsMap = new ConcurrentHashMap<>(); List regionIPList = new ArrayList<>(); RegionIPDto regionIP = new RegionIPDto(); regionIP.setId(1); regionIP.setPlace("昆仑"); regionIP.setServerIP("192.168.1.142"); regionIP.setNumMax(10000); regionIP.setPopulation(4); regionIPList.add(regionIP); IPRegionsMap.put("192.168.1.142", regionIPList); String IPRegions = JSON.toJSONString(IPRegionsMap); System.out.println(IPRegions); } }