拆分模块
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
public class CompanyUtils {
|
||||
public static String formatCompanyVendorId(int itemId) {
|
||||
if (itemId > 99) {
|
||||
return String.valueOf(itemId);
|
||||
} else if (itemId > 9) {
|
||||
return "0" + itemId;
|
||||
} else if (itemId > 0) {
|
||||
return "00" + itemId;
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,32 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
public class FileUtils {
|
||||
public final static String PDF = ".pdf";
|
||||
public final static String DOC = ".doc";
|
||||
public final static String DOCX = ".docx";
|
||||
public final static String XLS = ".xls";
|
||||
public final static String XLSX = ".xlsx";
|
||||
public final static String PNG = ".png";
|
||||
public final static String JPG = ".jpg";
|
||||
public final static String JPEG = ".jpeg";
|
||||
public final static String JSON = ".json";
|
||||
public final static String FILE_DB_THUMBS = "Thumbs.db";
|
||||
public final static String FILE_DB_JSON = "db.json";
|
||||
public final static String FILE_BLACK_LIST_JSON = "black_list.json";
|
||||
public final static String FILE_B1001_JSON = "b1001.json";
|
||||
|
||||
/**
|
||||
* 检查文件是否符合后缀要求
|
||||
*
|
||||
* @param name 文件名
|
||||
* @param fileExtensions 后缀
|
||||
*/
|
||||
public static boolean withExtensions(String name, String... fileExtensions) {
|
||||
for (String fn : fileExtensions) {
|
||||
if (name.endsWith(fn)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class HttpJsonUtils {
|
||||
public static void post(String url,
|
||||
Consumer<Map<String, Object>> data,
|
||||
Consumer<JsonNode> consumer,
|
||||
ObjectMapper objectMapper, Proxy proxy) throws IOException {
|
||||
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
data.accept(params);
|
||||
URI uri = URI.create(url);
|
||||
HttpURLConnection conn = (HttpURLConnection) (proxy == null ? uri.toURL().openConnection() : uri.toURL().openConnection(proxy));
|
||||
conn.usingProxy();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
PrintWriter writer = new PrintWriter(conn.getOutputStream());
|
||||
objectMapper.writeValue(writer, params);
|
||||
writer.flush();
|
||||
conn.connect();
|
||||
try (InputStream is = conn.getInputStream()) {
|
||||
JsonNode json = objectMapper.readTree(is);
|
||||
consumer.accept(json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void get(String url, Consumer<JsonNode> consumer, ObjectMapper objectMapper, Proxy proxy) throws IOException {
|
||||
URI uri = URI.create(url);
|
||||
HttpURLConnection conn = (HttpURLConnection) (proxy == null ? uri.toURL().openConnection() : uri.toURL().openConnection(proxy));
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("Accept", "application/json, text/plain, */*");
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36");
|
||||
try (InputStream is = conn.getInputStream()) {
|
||||
JsonNode json = objectMapper.readTree(is);
|
||||
consumer.accept(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 我的字符串工具箱
|
||||
*/
|
||||
public class MyStringUtils {
|
||||
/**
|
||||
* 把 append 拼接到 description 后面,如果 append 不在 description 内时
|
||||
*
|
||||
* @param description 被拼接的字符串
|
||||
* @param append 要拼接的内容
|
||||
* @return 拼接后的字符串
|
||||
*/
|
||||
public static String appendIfAbsent(String description, String append) {
|
||||
if (StringUtils.hasText(append)) {
|
||||
if (description != null && !description.contains(append)) {
|
||||
return description + ", " + description;
|
||||
}
|
||||
}
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符串中的每个字符,是否都是数字 {@link Character#isDigit(char)}
|
||||
*
|
||||
* @param str 要检查的字符串
|
||||
* @return 含有非字符串返回false,否则返回true
|
||||
*/
|
||||
public static boolean isAllDigit(String str) {
|
||||
if (!StringUtils.hasText(str)) {
|
||||
return false;
|
||||
}
|
||||
for (byte aByte : str.getBytes()) {
|
||||
if (!Character.isDigit(aByte)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static long toLong(String string, long defaultValue) {
|
||||
if (!StringUtils.hasText(string)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
try {
|
||||
return Long.parseLong(string);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分割字符串
|
||||
*
|
||||
* @param str 要分割的字符串
|
||||
* @param regex 要分割的字符
|
||||
* @return 分割后的字符集合
|
||||
*/
|
||||
public static List<String> split(String str, String regex) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// 未含有字符时,直接返回空集合
|
||||
if (!StringUtils.hasText(str)) {
|
||||
return list;
|
||||
}
|
||||
// 按照指定字符分割字符串
|
||||
String[] parts = str.split(regex);
|
||||
for (String part : parts) {
|
||||
if (StringUtils.hasText(part)) {
|
||||
list.add(part);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
21
common/src/main/java/com/ecep/contract/util/NumberUtils.java
Normal file
21
common/src/main/java/com/ecep/contract/util/NumberUtils.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
/**
|
||||
* 数字工具类
|
||||
*/
|
||||
public class NumberUtils {
|
||||
/**
|
||||
* 判断两个double是否相等
|
||||
*
|
||||
* @param a a
|
||||
* @param b b
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean equals(double a, double b) {
|
||||
return Math.abs(a - b) < 0.01;
|
||||
}
|
||||
|
||||
public static boolean equals(float a, float b) {
|
||||
return Math.abs(a - b) < 0.01;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user