yaml文件的读取以及其工具类

由于在工作中会出现一系列以键值对形式选项参数对应,可以通过yaml文件进行读取文件流。

type.yml

#计划审核表-查询类型
TCX_JHSBB_AUDIT_searchSblx:
  - valueData: 1
    displayName: 待审核
  - valueData: 2
    displayName: 已审核

YamlUtil

package com.yufei.core.util.conf;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.yufei.core.entity.SelectModel;
import com.yufei.core.util.PathUtil;

/**
 * type.yml 配置文件对应下拉集合显示
 * @author Lenovo
 */
public class YamlUtil {
	
	
	// 计划审核表-查询类型(计划审核、配合审核)
	public static final String TCX_JHSBB_AUDIT_SEARCHSBLX = "TCX_JHSBB_AUDIT_searchSblx"; // 查询类型
	
	
	/**
	 * 读取下拉状态配置参数List
	 * @param typeName
	 * @return
	 * @throws IOException
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static List getTypePropertie(String typeName) throws IOException{
		String yamlPath = PathUtil.getClassResources() + "conf/type.yml"; // yaml路径
		File yamlFile = new File(yamlPath);
		Yaml yaml = new Yaml();
		HashMap map = yaml.loadAs(new FileInputStream(yamlFile), HashMap.class);
		
		List list = new ArrayList();
		List contentList = (List)map.get(typeName);
		for (Object item : contentList){
			list.add(new SelectModel(((HashMap)item).get("valueData").toString(), ((HashMap)item).get("displayName").toString()));
		}
		return list;
	}
	
	/**
	 * 读取下拉状态配置参数返回map
	 * @param typeName
	 * @return
	 * @throws IOException
	 */
	public static Map getTypePropertieMap(String typeName) throws IOException{
		String yamlPath = PathUtil.getClassResources() + "conf/type.yml"; // yaml路径
		File yamlFile = new File(yamlPath);
		Yaml yaml = new Yaml();
		HashMap hashMap = yaml.loadAs(new FileInputStream(yamlFile), HashMap.class);
		
		Map map = new HashMap();
		List contentList = (List)hashMap.get(typeName);
		for (Object item : contentList){
			map.put(((HashMap)item).get("valueData").toString(), ((HashMap)item).get("displayName").toString());
		}
		return map;
	}
}

注意:这里需要导入一个包,可以在Maven中引入

	
		    org.yaml
		    snakeyaml
		    1.17
		

上面中还引入了一个PathUtil的路径工具类

package com.yufei.core.util;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 路径工具类
 * 
 * @author
 * 
 */
public class PathUtil {

	/**
	 * 图片访问路径
	 * 
	 * @param pathType
	 *            图片类型 visit-访问;save-保存
	 * @param pathCategory
	 *            图片类别,如:话题图片-topic、话题回复图片-reply、商家图片
	 * @return
	 */
	public static String getPicturePath(String pathType, String pathCategory) {
		String strResult = "";
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		StringBuffer strBuf = new StringBuffer();
		if ("visit".equals(pathType)) {
		} else if ("save".equals(pathType)) {
			String projectPath = PublicUtil.getPorjectPath().replaceAll("\\\\",
					"/");
			projectPath = splitString(projectPath, "bin/");

			strBuf.append(projectPath);
			strBuf.append("webapps/ROOT/");
		}

		strResult = strBuf.toString();

		return strResult;
	}

	private static String splitString(String str, String param) {
		String result = str;

		if (str.contains(param)) {
			int start = str.indexOf(param);
			result = str.substring(0, start);
		}

		return result;
	}
	
	/*
	 * 获取classpath1
	 */
	public static String getClasspath(){
		String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../").replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
	/*
	 * 获取classpath2
	 */
	public static String getClassResources(){
		String path =  (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))).replaceAll("file:/", "").replaceAll("%20", " ").trim();	
		if(path.indexOf(":") != 1){
			path = File.separator + path;
		}
		return path;
	}
	
	public static String PathAddress() {
		String strResult = "";

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();

		StringBuffer strBuf = new StringBuffer();

		strBuf.append(request.getScheme() + "://");
		strBuf.append(request.getServerName() + ":");
		strBuf.append(request.getServerPort() + "");

		strBuf.append(request.getContextPath() + "/");

		strResult = strBuf.toString();// +"ss/";//加入项目的名称

		return strResult;
	}
	
	
}

SelectModel

package com.yufei.core.entity;


/**
 * 下拉绑定对象
 * @author zhanghd 2017-07-27
 *
 */
public class SelectModel {
	public SelectModel() {
		super();
	}
	
	public SelectModel(String valueData, String displayName) {
		super();
		this.valueData = valueData;
		this.displayName = displayName;
	}

	/**
	 * 下拉值
	 */
	private String valueData;
	/**
	 * 下拉显示内容
	 */
	private String displayName;

	public String getValueData() {
		return valueData;
	}

	public void setValueData(String valueData) {
		this.valueData = valueData;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
}

注意:在测试过程中,报了一个Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1

原因,type.yml中的指定配置文件编码不是UTF-8的,转换成UTF-8就行了。

 

你可能感兴趣的:(工作总结)