utils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.onemap.api.util;
  2. import com.onemap.common.core.utils.StringUtils;
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.*;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipInputStream;
  11. import java.util.zip.ZipOutputStream;
  12. /**
  13. * @author : wanger
  14. * @createDate: 2025/2/18
  15. * @description:公用工具类
  16. **/
  17. public class utils {
  18. /**
  19. * 自治区成果包查询需要的plantype
  20. *
  21. * @param ghlx
  22. * @return
  23. */
  24. public static Integer getPlanType(String ghlx) {
  25. switch (ghlx) {
  26. case "总体规划":
  27. return 1;
  28. case "村庄规划":
  29. return 2;
  30. case "报批项目":
  31. return 3;
  32. case "总体规划(对部)":
  33. return 4;
  34. case "详细规划":
  35. return 5;
  36. case "专项规划":
  37. return 6;
  38. case "乡镇苏木国土空间规划":
  39. return 7;
  40. default:
  41. return 1;
  42. }
  43. }
  44. /**
  45. * 市级成果接口需要的plantype
  46. *
  47. * @param ghlx
  48. * @return
  49. */
  50. public static Integer getPlanTypeForSj(String ghlx) {
  51. switch (ghlx) {
  52. case "评估成果":
  53. return 1;
  54. case "规划成果":
  55. return 2;
  56. case "总体规划":
  57. return 3;
  58. case "详细规划":
  59. return 4;
  60. case "专项规划":
  61. return 5;
  62. case "报批项目":
  63. return 6;
  64. case "苏木乡镇规划":
  65. return 7;
  66. case "城市更新":
  67. return 8;
  68. case "村庄规划":
  69. return 9;
  70. default:
  71. return 0;
  72. }
  73. }
  74. /**
  75. * 判断文件夹是否存在,不存在则创建
  76. *
  77. * @param folderpath
  78. */
  79. public static void makedir(String folderpath) {
  80. File folderFile = new File(folderpath);
  81. if (!folderFile.exists()) {
  82. try {
  83. Files.createDirectories(Paths.get(folderpath));
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. /**
  90. * 控制台打印
  91. *
  92. * @param info
  93. */
  94. public static void loginfo(String info) {
  95. System.out.println(info);
  96. }
  97. /**
  98. * 合并zip分片数据包
  99. *
  100. * @param fileNames
  101. * @param outputFileName
  102. * @throws IOException
  103. */
  104. public static void mergeZipFiles(List<String> fileNames, String outputFileName) throws IOException {
  105. FileOutputStream fos = new FileOutputStream(outputFileName);
  106. for (String fileName : fileNames) {
  107. File cur = new File(fileName);
  108. byte[] buffer = Files.readAllBytes(Paths.get(fileName));
  109. fos.write(buffer, 0, buffer.length);
  110. }
  111. fos.flush();
  112. fos.close();
  113. }
  114. /**
  115. * 读取txt文本文件
  116. *
  117. * @param filepath
  118. * @return Map<String, String>
  119. */
  120. public static Map<String, String> readTxt(String filepath) {
  121. Map<String, String> resMap = new HashMap<>();
  122. try {
  123. File file = new File(filepath);
  124. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
  125. String line;
  126. while ((line = reader.readLine()) != null) {
  127. if (StringUtils.isNotEmpty(line)) {
  128. String[] curline = line.split("=");
  129. resMap.put(curline[0], curline[1]);
  130. }
  131. }
  132. reader.close();
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. return resMap;
  137. }
  138. /**
  139. * 搜索文件夹下指定文件名结尾的文件 默认返回第一个
  140. *
  141. * @param filepath
  142. * @param endsWith
  143. * @return String
  144. */
  145. public static String FileSearch(String filepath, String endsWith) {
  146. File folder = new File(filepath);
  147. File[] files = folder.listFiles(new FilenameFilter() {
  148. @Override
  149. public boolean accept(File dir, String name) {
  150. return name.endsWith(endsWith);
  151. }
  152. });
  153. if (files != null) {
  154. return files[0].getAbsolutePath();
  155. } else {
  156. return null;
  157. }
  158. }
  159. /**
  160. * 正则表达式 获取参数占位信息
  161. *
  162. * @param str
  163. * @return
  164. */
  165. public static List<String> extractUniqueMatches(String str) {
  166. // 定义正则表达式
  167. Pattern regex = Pattern.compile("\\{.*?\\}", Pattern.CASE_INSENSITIVE);
  168. Matcher matcher = regex.matcher(str);
  169. // 用于存储匹配结果的集合
  170. Set<String> uniqueMatches = new HashSet<>();
  171. // 查找所有匹配项
  172. while (matcher.find()) {
  173. uniqueMatches.add(matcher.group());
  174. }
  175. // 将Set转换为List并返回
  176. return new ArrayList<>(uniqueMatches);
  177. }
  178. }