作者 朱兆平

开发框架utils init

  1 +/target/
  2 +!.mvn/wrapper/maven-wrapper.jar
  3 +/logs/
  4 +/out/
  5 +.mvn
  6 +/xmlFromImf/
  7 +kakoRevice/
  8 +/errorLogs/
  9 +### STS ###
  10 +.apt_generated
  11 +.classpath
  12 +.factorypath
  13 +.project
  14 +.settings
  15 +.springBeans
  16 +.sts4-cache
  17 +
  18 +### IntelliJ IDEA ###
  19 +.idea
  20 +*.iws
  21 +*.iml
  22 +*.ipr
  23 +
  24 +### NetBeans ###
  25 +/nbproject/private/
  26 +/build/
  27 +/nbbuild/
  28 +/dist/
  29 +/nbdist/
  30 +/.nb-gradle/
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <modelVersion>4.0.0</modelVersion>
  6 +
  7 + <groupId>com.tianbo</groupId>
  8 + <artifactId>util</artifactId>
  9 + <version>1.0-SNAPSHOT</version>
  10 + <dependencies>
  11 + <dependency>
  12 + <groupId>commons-io</groupId>
  13 + <artifactId>commons-io</artifactId>
  14 + <version>2.6</version>
  15 + </dependency>
  16 + <dependency>
  17 + <groupId>commons-lang</groupId>
  18 + <artifactId>commons-lang</artifactId>
  19 + <version>2.6</version>
  20 + </dependency>
  21 + <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
  22 + <dependency>
  23 + <groupId>org.dom4j</groupId>
  24 + <artifactId>dom4j</artifactId>
  25 + <version>2.1.0</version>
  26 + </dependency>
  27 + </dependencies>
  28 +
  29 +
  30 +</project>
  1 +package com.tianbo.util.Date;
  2 +
  3 +import java.text.SimpleDateFormat;
  4 +import java.time.LocalDateTime;
  5 +import java.time.ZoneId;
  6 +import java.time.format.DateTimeFormatter;
  7 +import java.time.format.DateTimeParseException;
  8 +import java.util.Date;
  9 +
  10 +public final class DateUtil {
  11 + private static Date currentDate = new Date();
  12 + private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  13 + private static SimpleDateFormat timesdf = new SimpleDateFormat("yyyyMMddHHmmss");
  14 +
  15 + public static String getToday(){
  16 + return sdf.format(currentDate);
  17 + }
  18 + public static String getDDTM(){
  19 + return timesdf.format(currentDate);
  20 + }
  21 +
  22 + public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{
  23 + //毫秒级的去掉
  24 + if(dateStr.length()>14){
  25 + dateStr= dateStr.substring(0,14);
  26 + }
  27 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  28 + LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
  29 + return convertLDTToDate(dateTime);
  30 + }
  31 +
  32 + //LocalDateTime转换为Date
  33 + public static Date convertLDTToDate(LocalDateTime time) {
  34 + return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
  35 + }
  36 +
  37 +}
  1 +package com.tianbo.util;
  2 +
  3 +
  4 +
  5 +import java.math.BigDecimal;
  6 +import java.math.BigInteger;
  7 +import java.util.Map;
  8 +import java.util.UUID;
  9 +
  10 +public class Helper {
  11 +
  12 + /**
  13 + * Object转BigDecimal类型-MRZ-2018年5月14日09:56:26
  14 + *
  15 + * @param value 要转的object类型
  16 + * @return 转成的BigDecimal类型数据
  17 + */
  18 + static public BigDecimal getBigDecimal(Object value) {
  19 + BigDecimal ret = null;
  20 + if (value != null) {
  21 + if (value instanceof BigDecimal) {
  22 + ret = (BigDecimal) value;
  23 + } else if (value instanceof String) {
  24 + ret = new BigDecimal((String) value);
  25 + } else if (value instanceof BigInteger) {
  26 + ret = new BigDecimal((BigInteger) value);
  27 + } else if (value instanceof Number) {
  28 + ret = new BigDecimal(((Number) value).doubleValue());
  29 + } else {
  30 + throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
  31 + }
  32 + }
  33 + return ret;
  34 + }
  35 +
  36 +
  37 +
  38 + public static String getUUID(){
  39 + return UUID.randomUUID().toString().replace("-", "");
  40 + }
  41 +}
  1 +package com.tianbo.util.IO;
  2 +
  3 +import com.tianbo.util.Date.DateUtil;
  4 +import com.tianbo.util.Helper;
  5 +import org.apache.commons.io.FileUtils;
  6 +
  7 +import java.io.*;
  8 +import java.util.List;
  9 +import java.util.Properties;
  10 +
  11 +
  12 +public final class FileTool {
  13 + private final static String errorRootDirectory = "errorLogs";//错误的根目录名
  14 + private final static String xmlRootDirectory = "xmlLog"; //记录已收到的报文目录
  15 + private final static String Cherector = "UTF-8";
  16 +
  17 + /**
  18 + * 写入文件
  19 + * @param path 二级目录
  20 + * @param content 写入内容
  21 + * @param rightOrwrong 是写入错误记录目录还是记录目录
  22 + */
  23 + public static void writeFile(String path,String content,boolean rightOrwrong){
  24 + StringBuffer stringBuffer = new StringBuffer();
  25 +
  26 + if (rightOrwrong){
  27 + stringBuffer.append(xmlRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(".log");
  28 + }else {
  29 + stringBuffer.append(errorRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(".log");
  30 + }
  31 +
  32 + File file = new File(stringBuffer.toString());
  33 +
  34 + try{
  35 + FileUtils.writeStringToFile(file,content,Cherector);
  36 + }catch (IOException e){
  37 + e.printStackTrace();
  38 + }
  39 +
  40 + }
  41 + public static String readfile(File file) throws IOException{
  42 + String fileToString = FileUtils.readFileToString(file, "utf-8");
  43 + return fileToString;
  44 + }
  45 +
  46 + public static List<File> readDirectoryFiles(File dir){
  47 + List<File> files = (List<File>)FileUtils.listFiles(dir,new String[]{"xml"},false);
  48 + return files;
  49 + }
  50 + public static void writeWaybill(String path,String content,String waybillNo){
  51 + StringBuffer stringBuffer = new StringBuffer();
  52 + stringBuffer.append(xmlRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(waybillNo).append(".log");
  53 + File file = new File(stringBuffer.toString());
  54 + try{
  55 + FileUtils.writeStringToFile(file,content,Cherector);
  56 + }catch (IOException e){
  57 + e.printStackTrace();
  58 + }
  59 +
  60 + }
  61 +
  62 + public static void writeFileToBak(String content){
  63 + String bakDir = readProperties("bakDirectory");
  64 + StringBuffer stringBuffer = new StringBuffer();
  65 + stringBuffer.append(bakDir).append("/").append(Helper.getUUID()).append(".txt");
  66 +
  67 + File file = new File(stringBuffer.toString());
  68 +
  69 + try{
  70 + FileUtils.writeStringToFile(file,content,Cherector);
  71 + }catch (IOException e){
  72 + e.printStackTrace();
  73 + }
  74 +
  75 + }
  76 +
  77 + public static String readProperties(String key){
  78 + Properties properties = new Properties();
  79 + String value = "";
  80 + try{
  81 + BufferedReader bufferedReader = new BufferedReader(new FileReader("config/config.properties"));
  82 + properties.load(bufferedReader);
  83 + //获取key对应的value值
  84 + value= properties.getProperty(key);
  85 + }catch (Exception e){
  86 + e.printStackTrace();
  87 + }
  88 + return value;
  89 +
  90 + }
  91 +
  92 +
  93 +}
  1 +package com.tianbo.util.IO;
  2 +
  3 +import org.apache.commons.lang.ArrayUtils;
  4 +
  5 +import java.util.Arrays;
  6 +import java.util.stream.Stream;
  7 +
  8 +public abstract class StreamUtil implements Stream{
  9 +
  10 + public static <T> Stream<T> streamOf(T[] array) {
  11 + return ArrayUtils.isEmpty(array) ? Stream.empty() : Arrays.asList(array).stream();
  12 + }
  13 +}
  1 +package com.tianbo.util.XML;
  2 +
  3 +import org.dom4j.Document;
  4 +import org.dom4j.DocumentException;
  5 +import org.dom4j.DocumentHelper;
  6 +import org.dom4j.Element;
  7 +import org.dom4j.io.SAXReader;
  8 +import org.xml.sax.SAXParseException;
  9 +
  10 +import java.io.ByteArrayInputStream;
  11 +import java.io.File;
  12 +import java.io.UnsupportedEncodingException;
  13 +
  14 +public class MakeImfMeta {
  15 +
  16 + public static String makeImfDocument(String SNDR,String RCVR,String TYPE,String STYP,String DDTM,String SEQN,File Content) throws DocumentException,UnsupportedEncodingException,SAXParseException{
  17 + Document document = DocumentHelper.createDocument();
  18 + Element root = document.addElement( "MSG" );
  19 + Element meta = root.addElement("META");
  20 + meta.addElement("SNDR").addText(SNDR);
  21 + meta.addElement("RCVR").addText(RCVR);
  22 + meta.addElement("SEQN").addText(SEQN);
  23 + meta.addElement("DDTM").addText(DDTM);
  24 + meta.addElement("TYPE").addText(TYPE);
  25 + meta.addElement("STYP").addText(STYP);
  26 +
  27 + SAXReader saxReader = new SAXReader();
  28 +// saxReader.setEncoding("gb2312");
  29 + Document doc = saxReader.read(Content);
  30 + Element contentRoot = doc.getRootElement();
  31 + root.add(contentRoot);
  32 +
  33 +
  34 + return document.asXML();
  35 + }
  36 +}
  1 +package com.tianbo.util.XML;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.HashMap;
  5 +import java.util.Iterator;
  6 +import java.util.List;
  7 +import java.util.Map;
  8 +
  9 +import org.dom4j.Attribute;
  10 +import org.dom4j.Document;
  11 +import org.dom4j.Element;
  12 +
  13 +public class XML2ENTITY {
  14 + @SuppressWarnings("unchecked")
  15 + /**取KEY的VALUE
  16 + *
  17 + */
  18 + public Map<String, Object> Dom2Map(Document doc){
  19 + Map<String, Object> map = new HashMap<String, Object>();
  20 + if(doc == null){
  21 + return map;
  22 + }
  23 + Element root = doc.getRootElement();
  24 + for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
  25 + Element e = (Element) iterator.next();
  26 + List list = e.elements();
  27 + if(list.size() > 0){
  28 + map.put(e.getName(), Dom2Map(e));
  29 + }else{
  30 + map.put(e.getName(), e.getText());}
  31 + }
  32 + return map;
  33 + }
  34 + @SuppressWarnings("unchecked")
  35 + public Map Dom2Map(Element e){
  36 + Map map = new HashMap();
  37 + List list = e.elements();
  38 + if(list.size() > 0){
  39 + for (int i = 0;i < list.size(); i++) {
  40 + Element iter = (Element) list.get(i);
  41 + List mapList = new ArrayList();
  42 +
  43 + if(iter.elements().size() > 0){
  44 + Map m = Dom2Map(iter);
  45 + if(map.get(iter.getName()) != null){
  46 + Object obj = map.get(iter.getName());
  47 + if(!obj.getClass().getName().equals("java.util.ArrayList")){
  48 + mapList = new ArrayList();
  49 + mapList.add(obj);
  50 + mapList.add(m);
  51 + }
  52 + if(obj.getClass().getName().equals("java.util.ArrayList")){
  53 + mapList = (List) obj;
  54 + mapList.add(m);
  55 + }
  56 + map.put(iter.getName(), mapList);
  57 + }else{
  58 + map.put(iter.getName(), m);}
  59 + }
  60 + else{
  61 + if(map.get(iter.getName()) != null){
  62 + Object obj = map.get(iter.getName());
  63 + if(!obj.getClass().getName().equals("java.util.ArrayList")){
  64 + mapList = new ArrayList();
  65 + mapList.add(obj);
  66 + mapList.add(iter.getText());
  67 + }
  68 + if(obj.getClass().getName().equals("java.util.ArrayList")){
  69 + mapList = (List) obj;
  70 + mapList.add(iter.getText());
  71 + }
  72 + map.put(iter.getName(), mapList);
  73 + }else{
  74 + map.put(iter.getName(), iter.getText());}
  75 + }
  76 + }
  77 + }else{
  78 + map.put(e.getName(), e.getText());}
  79 + return map;
  80 + }
  81 +
  82 + public List<Map> attrOfElement(Element e){
  83 + List attList = new ArrayList();
  84 + List<Attribute> listAttr = e.attributes();
  85 + for(Attribute attr:listAttr){//遍历当前节点的所有属性
  86 + String name=attr.getName();//属性名称
  87 + String value=attr.getValue();//属性的值
  88 + Map<String, Object> attMap = new HashMap<String, Object>();
  89 + attMap.put(name,value);
  90 + attList.add(attMap);
  91 + }
  92 + return attList;
  93 + }
  94 + @SuppressWarnings("unchecked")
  95 + /**遍历所有节点的属性值
  96 + *
  97 + */
  98 + public Map<String, Object> Dom2Map_propety(Document doc){
  99 + Map<String, Object> map = new HashMap<String, Object>();
  100 + if(doc == null) {
  101 + return map;
  102 + }
  103 + Element root = doc.getRootElement();
  104 + for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
  105 + Element e = (Element) iterator.next();
  106 + List list = e.elements();
  107 + if(list.size() > 0){
  108 + map.put(e.getName(), Dom2Map_propety(e));
  109 + }else {
  110 + map.put(e.getName(), attrOfElement(e));
  111 + }
  112 + }
  113 + return map;
  114 + }
  115 + @SuppressWarnings("unchecked")
  116 + public Map Dom2Map_propety(Element e){
  117 + Map map = new HashMap();
  118 + List list = e.elements();
  119 + if(list.size() > 0){
  120 + for (int i = 0;i < list.size(); i++) {
  121 + Element iter = (Element) list.get(i);
  122 + List mapList = new ArrayList();
  123 +
  124 + if(iter.elements().size() > 0){
  125 + Map m = Dom2Map_propety(iter);
  126 + if(map.get(iter.getName()) != null){
  127 + Object obj = map.get(iter.getName());
  128 + if(!obj.getClass().getName().equals("java.util.ArrayList")){
  129 + mapList = new ArrayList();
  130 + mapList.add(obj);
  131 + mapList.add(m);
  132 + }
  133 + if(obj.getClass().getName().equals("java.util.ArrayList")){
  134 + mapList = (List) obj;
  135 + mapList.add(m);
  136 + }
  137 + map.put(iter.getName(), mapList);
  138 + }else {
  139 + map.put(iter.getName(), m);
  140 + }
  141 + }
  142 + else{
  143 + if(map.get(iter.getName()) != null){
  144 + Object obj = map.get(iter.getName());
  145 + if(!obj.getClass().getName().equals("java.util.ArrayList")){
  146 + mapList = new ArrayList();
  147 + mapList.add(obj);
  148 + mapList.add(iter.getText());
  149 + }
  150 + if(obj.getClass().getName().equals("java.util.ArrayList")){
  151 + mapList = (List) obj;
  152 + mapList.add(iter.getText());
  153 + }
  154 + map.put(iter.getName(), mapList);
  155 + }else{
  156 + map.put(iter.getName(), attrOfElement(iter));
  157 + }
  158 +
  159 +
  160 + }
  161 + }
  162 + }else {
  163 + map.put(e.getName(), attrOfElement(e));
  164 + }
  165 + return map;
  166 + }
  167 +
  168 + /**
  169 + * 判断map是否包含key,包含返回KEY值,不包含返回NULL
  170 + * @param map
  171 + * @param key
  172 + * @return
  173 + */
  174 +
  175 + public static Object getMap(Map map, String key){
  176 + if(map!=null && key!=null && !key.isEmpty() && map.containsKey(key)){
  177 + return map.get(key);
  178 + } else {
  179 + return "";
  180 + }
  181 + }
  182 +
  183 +
  184 +}
  185 +
  186 +
  1 +package com.tianbo.util.XML;
  2 +
  3 +import org.dom4j.Attribute;
  4 +import org.dom4j.Document;
  5 +import org.dom4j.Node;
  6 +
  7 +import java.util.Iterator;
  8 +import java.util.List;
  9 +import java.util.Map;
  10 +
  11 +public class XMLXPath {
  12 +
  13 + public static String getSingleValueByPath(Document document,String path){
  14 + Node node = document.selectSingleNode(path);
  15 + if (node!=null){
  16 + String nodeValue = node.getStringValue();
  17 + return nodeValue;
  18 + }else {
  19 + return null;
  20 + }
  21 +
  22 + }
  23 + public static List<Node> getPathValues(Document document,String path){
  24 + List<Node> nodes= document.selectNodes(path);
  25 + return nodes;
  26 + }
  27 +
  28 + public static void getPathValues2(Document document,String path){
  29 + List list = document.selectNodes(path);
  30 + for (Iterator it = list.iterator(); it.hasNext();) {
  31 + Attribute attr = (Attribute) it.next();
  32 + //TODO
  33 + }
  34 + }
  35 +}