| @ -0,0 +1,11 @@ | |||||
| package com.inspect.simulator.constant; | |||||
| public class Color { | |||||
| public static final String END = "\033[0m"; | |||||
| public static final String RED = "\033[31m"; | |||||
| public static final String GREEN = "\033[32m"; | |||||
| public static final String YELLOW = "\033[33m"; | |||||
| public static final String BLUE = "\033[34m"; | |||||
| public static final String MAGENTA = "\033[35m"; | |||||
| public static final String CYAN = "\033[36m"; | |||||
| } | |||||
| @ -0,0 +1,42 @@ | |||||
| package com.inspect.simulator.exception; | |||||
| public final class ServiceException extends RuntimeException { | |||||
| private static final long serialVersionUID = 1L; | |||||
| private Integer code; | |||||
| private String message; | |||||
| private String detailMessage; | |||||
| public ServiceException() { | |||||
| } | |||||
| public ServiceException(String message) { | |||||
| this.message = message; | |||||
| } | |||||
| public ServiceException(String message, Integer code) { | |||||
| this.message = message; | |||||
| this.code = code; | |||||
| } | |||||
| public String getDetailMessage() { | |||||
| return this.detailMessage; | |||||
| } | |||||
| public String getMessage() { | |||||
| return this.message; | |||||
| } | |||||
| public Integer getCode() { | |||||
| return this.code; | |||||
| } | |||||
| public ServiceException setMessage(String message) { | |||||
| this.message = message; | |||||
| return this; | |||||
| } | |||||
| public ServiceException setDetailMessage(String detailMessage) { | |||||
| this.detailMessage = detailMessage; | |||||
| return this; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,37 @@ | |||||
| package com.inspect.simulator.utils; | |||||
| import java.nio.charset.Charset; | |||||
| import java.nio.charset.StandardCharsets; | |||||
| public class CharsetKit { | |||||
| public static final String ISO_8859_1 = "ISO-8859-1"; | |||||
| public static final String UTF_8 = "UTF-8"; | |||||
| public static final String GBK = "GBK"; | |||||
| public static final Charset CHARSET_ISO_8859_1 = Charset.forName("ISO-8859-1"); | |||||
| public static final Charset CHARSET_UTF_8 = Charset.forName("UTF-8"); | |||||
| public static final Charset CHARSET_GBK = Charset.forName("GBK"); | |||||
| public static Charset charset(String charset) { | |||||
| return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset); | |||||
| } | |||||
| public static String convert(String source, String srcCharset, String destCharset) { | |||||
| return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset)); | |||||
| } | |||||
| public static String convert(String source, Charset srcCharset, Charset destCharset) { | |||||
| if (null == srcCharset) { | |||||
| srcCharset = StandardCharsets.ISO_8859_1; | |||||
| } | |||||
| if (null == destCharset) { | |||||
| destCharset = StandardCharsets.UTF_8; | |||||
| } | |||||
| return !StringUtils.isEmpty(source) && !srcCharset.equals(destCharset) ? new String(source.getBytes(srcCharset), destCharset) : source; | |||||
| } | |||||
| public static String systemCharset() { | |||||
| return Charset.defaultCharset().name(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,489 @@ | |||||
| package com.inspect.simulator.utils; | |||||
| import java.math.BigDecimal; | |||||
| import java.math.BigInteger; | |||||
| import java.nio.ByteBuffer; | |||||
| import java.nio.charset.Charset; | |||||
| import java.text.NumberFormat; | |||||
| import java.util.Set; | |||||
| public class Convert { | |||||
| public static String toStr(Object value, String defaultValue) { | |||||
| return null == value ? defaultValue : (value instanceof String ? (String) value : value.toString()); | |||||
| } | |||||
| public static String toStr(Object value) { | |||||
| return toStr(value, (String) null); | |||||
| } | |||||
| public static Character toChar(Object value, Character defaultValue) { | |||||
| if (null == value) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Character) { | |||||
| return (Character) value; | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0); | |||||
| } | |||||
| } | |||||
| public static Character toChar(Object value) { | |||||
| return toChar(value, null); | |||||
| } | |||||
| public static Byte toByte(Object value, Byte defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Byte) { | |||||
| return (Byte) value; | |||||
| } else if (value instanceof Number) { | |||||
| return ((Number) value).byteValue(); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return Byte.parseByte(valueStr); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Byte toByte(Object value) { | |||||
| return toByte(value, null); | |||||
| } | |||||
| public static Short toShort(Object value, Short defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Short) { | |||||
| return (Short) value; | |||||
| } else if (value instanceof Number) { | |||||
| return ((Number) value).shortValue(); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return Short.parseShort(valueStr.trim()); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Short toShort(Object value) { | |||||
| return toShort(value, null); | |||||
| } | |||||
| public static Number toNumber(Object value, Number defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Number) { | |||||
| return (Number) value; | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return NumberFormat.getInstance().parse(valueStr); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Number toNumber(Object value) { | |||||
| return toNumber(value, null); | |||||
| } | |||||
| public static Integer toInt(Object value, Integer defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Integer) { | |||||
| return (Integer) value; | |||||
| } else if (value instanceof Number) { | |||||
| return ((Number) value).intValue(); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return Integer.parseInt(valueStr.trim()); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Integer toInt(Object value) { | |||||
| return toInt(value, null); | |||||
| } | |||||
| public static Integer[] toIntArray(String str) { | |||||
| return toIntArray(StringUtils.COMMA, str); | |||||
| } | |||||
| public static Long[] toLongArray(String str) { | |||||
| return toLongArray(StringUtils.COMMA, str); | |||||
| } | |||||
| public static Integer[] toIntArray(String split, String str) { | |||||
| if (StringUtils.isEmpty(str)) { | |||||
| return new Integer[0]; | |||||
| } else { | |||||
| String[] arr = str.split(split); | |||||
| Integer[] ints = new Integer[arr.length]; | |||||
| for (int i = 0; i < arr.length; ++i) { | |||||
| Integer v = toInt(arr[i], 0); | |||||
| ints[i] = v; | |||||
| } | |||||
| return ints; | |||||
| } | |||||
| } | |||||
| public static Long[] toLongArray(String split, String str) { | |||||
| if (StringUtils.isEmpty(str)) { | |||||
| return new Long[0]; | |||||
| } else { | |||||
| String[] arr = str.split(split); | |||||
| Long[] longs = new Long[arr.length]; | |||||
| for (int i = 0; i < arr.length; ++i) { | |||||
| Long v = toLong(arr[i], null); | |||||
| longs[i] = v; | |||||
| } | |||||
| return longs; | |||||
| } | |||||
| } | |||||
| public static String[] toStrArray(String str) { | |||||
| return toStrArray(StringUtils.COMMA, str); | |||||
| } | |||||
| public static String[] toStrArray(String split, String str) { | |||||
| return str.split(split); | |||||
| } | |||||
| public static Long toLong(Object value, Long defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Long) { | |||||
| return (Long) value; | |||||
| } else if (value instanceof Number) { | |||||
| return ((Number) value).longValue(); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return (new BigDecimal(valueStr.trim())).longValue(); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Long toLong(Object value) { | |||||
| return toLong(value, null); | |||||
| } | |||||
| public static Double toDouble(Object value, Double defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Double) { | |||||
| return (Double) value; | |||||
| } else if (value instanceof Number) { | |||||
| return ((Number) value).doubleValue(); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return (new BigDecimal(valueStr.trim())).doubleValue(); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Double toDouble(Object value) { | |||||
| return toDouble(value, null); | |||||
| } | |||||
| public static Float toFloat(Object value, Float defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Float) { | |||||
| return (Float) value; | |||||
| } else if (value instanceof Number) { | |||||
| return ((Number) value).floatValue(); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return Float.parseFloat(valueStr.trim()); | |||||
| } catch (Exception e) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static Float toFloat(Object value) { | |||||
| return toFloat(value, null); | |||||
| } | |||||
| public static Boolean toBool(Object value, Boolean defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof Boolean) { | |||||
| return (Boolean) value; | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (!StringUtils.isEmpty(valueStr)) { | |||||
| valueStr = valueStr.trim().toLowerCase(); | |||||
| switch (valueStr) { | |||||
| case "0": | |||||
| case "no": | |||||
| case "false": | |||||
| return Boolean.FALSE; | |||||
| case "1": | |||||
| case "ok": | |||||
| case "yes": | |||||
| case "true": | |||||
| return Boolean.TRUE; | |||||
| } | |||||
| } | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| public static Boolean toBool(Object value) { | |||||
| return toBool(value, (Boolean) null); | |||||
| } | |||||
| public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (clazz.isAssignableFrom(value.getClass())) { | |||||
| E myE = (E) value; | |||||
| return myE; | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return Enum.valueOf(clazz, valueStr); | |||||
| } catch (Exception exception) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) { | |||||
| return toEnum(clazz, value, (E) null); | |||||
| } | |||||
| public static BigInteger toBigInteger(Object value, BigInteger defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof BigInteger) { | |||||
| return (BigInteger) value; | |||||
| } else if (value instanceof Long) { | |||||
| return BigInteger.valueOf((Long) value); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return new BigInteger(valueStr); | |||||
| } catch (Exception var4) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static BigInteger toBigInteger(Object value) { | |||||
| return toBigInteger(value, (BigInteger) null); | |||||
| } | |||||
| public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) { | |||||
| if (value == null) { | |||||
| return defaultValue; | |||||
| } else if (value instanceof BigDecimal) { | |||||
| return (BigDecimal) value; | |||||
| } else if (value instanceof Long) { | |||||
| return new BigDecimal((Long) value); | |||||
| } else if (value instanceof Double) { | |||||
| return new BigDecimal((Double) value); | |||||
| } else if (value instanceof Integer) { | |||||
| return new BigDecimal((Integer) value); | |||||
| } else { | |||||
| String valueStr = toStr(value, null); | |||||
| if (StringUtils.isEmpty(valueStr)) { | |||||
| return defaultValue; | |||||
| } else { | |||||
| try { | |||||
| return new BigDecimal(valueStr); | |||||
| } catch (Exception var4) { | |||||
| return defaultValue; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| public static BigDecimal toBigDecimal(Object value) { | |||||
| return toBigDecimal(value, null); | |||||
| } | |||||
| public static String utf8Str(Object obj) { | |||||
| return str(obj, CharsetKit.CHARSET_UTF_8); | |||||
| } | |||||
| public static String str(Object obj, String charsetName) { | |||||
| return str(obj, Charset.forName(charsetName)); | |||||
| } | |||||
| public static String str(Object obj, Charset charset) { | |||||
| if (null == obj) { | |||||
| return null; | |||||
| } else if (obj instanceof String) { | |||||
| return (String) obj; | |||||
| } else if (!(obj instanceof byte[]) && !(obj instanceof Byte[])) { | |||||
| return obj instanceof ByteBuffer ? str((ByteBuffer) obj, charset) : obj.toString(); | |||||
| } else if (obj instanceof byte[]) { | |||||
| String str = str((byte[]) obj, charset); | |||||
| return str; | |||||
| } else { | |||||
| Byte[] bytes = (Byte[]) obj; | |||||
| int length = bytes.length; | |||||
| byte[] dest = new byte[length]; | |||||
| for (int i = 0; i < length; ++i) { | |||||
| dest[i] = bytes[i]; | |||||
| } | |||||
| return str(dest, charset); | |||||
| } | |||||
| } | |||||
| public static String str(byte[] bytes, String charset) { | |||||
| return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset)); | |||||
| } | |||||
| public static String str(byte[] data, Charset charset) { | |||||
| return data == null ? null : (null == charset ? new String(data) : new String(data, charset)); | |||||
| } | |||||
| public static String str(ByteBuffer data, String charset) { | |||||
| return data == null ? null : str(data, Charset.forName(charset)); | |||||
| } | |||||
| public static String str(ByteBuffer data, Charset charset) { | |||||
| if (null == charset) { | |||||
| charset = Charset.defaultCharset(); | |||||
| } | |||||
| return charset.decode(data).toString(); | |||||
| } | |||||
| public static String toSBC(String input) { | |||||
| return toSBC(input, null); | |||||
| } | |||||
| public static String toSBC(String input, Set<Character> notConvertSet) { | |||||
| char[] c = input.toCharArray(); | |||||
| for (int i = 0; i < c.length; ++i) { | |||||
| if (null == notConvertSet || !notConvertSet.contains(c[i])) { | |||||
| if (c[i] == 32) { | |||||
| c[i] = 12288; | |||||
| } else if (c[i] < 127) { | |||||
| c[i] += 'ﻠ'; | |||||
| } | |||||
| } | |||||
| } | |||||
| return new String(c); | |||||
| } | |||||
| public static String toDBC(String input) { | |||||
| return toDBC(input, null); | |||||
| } | |||||
| public static String toDBC(String text, Set<Character> notConvertSet) { | |||||
| char[] c = text.toCharArray(); | |||||
| for (int i = 0; i < c.length; ++i) { | |||||
| if (null == notConvertSet || !notConvertSet.contains(c[i])) { | |||||
| if (c[i] == 12288) { | |||||
| c[i] = 32; | |||||
| } else if (c[i] > '\uff00' && c[i] < '⦅') { | |||||
| c[i] -= 'ﻠ'; | |||||
| } | |||||
| } | |||||
| } | |||||
| String returnString = new String(c); | |||||
| return returnString; | |||||
| } | |||||
| public static String digitUppercase(double n) { | |||||
| String[] fraction = new String[]{"角", "分"}; | |||||
| String[] digit = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; | |||||
| String[][] unit = new String[][]{{"元", "万", "亿"}, {"", "拾", "佰", "仟"}}; | |||||
| String head = n < 0.0D ? "负" : ""; | |||||
| n = Math.abs(n); | |||||
| StringBuilder s = new StringBuilder(); | |||||
| int integerPart; | |||||
| for (integerPart = 0; integerPart < fraction.length; ++integerPart) { | |||||
| s.append((digit[(int) (Math.floor(n * 10.0D * Math.pow(10.0D, integerPart)) % 10.0D)] + fraction[integerPart]).replaceAll("(零.)+", "")); | |||||
| } | |||||
| if (s.length() == 0) { | |||||
| s = new StringBuilder("整"); | |||||
| } | |||||
| integerPart = (int) Math.floor(n); | |||||
| for (int i = 0; i < unit[0].length && integerPart > 0; ++i) { | |||||
| String p = ""; | |||||
| for (int j = 0; j < unit[1].length && n > 0.0D; ++j) { | |||||
| p = digit[integerPart % 10] + unit[1][j] + p; | |||||
| integerPart /= 10; | |||||
| } | |||||
| s.insert(0, p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i]); | |||||
| } | |||||
| return head + s.toString().replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整"); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,50 @@ | |||||
| package com.inspect.simulator.utils; | |||||
| public class StrFormatter { | |||||
| public static final String EMPTY_JSON = "{}"; | |||||
| public static final char C_BACKSLASH = '\\'; | |||||
| public static final char C_DELIM_START = '{'; | |||||
| public static final char C_DELIM_END = '}'; | |||||
| public static String format(String strPattern, Object... argArray) { | |||||
| if (!StringUtils.isEmpty(strPattern) && !StringUtils.isEmpty(argArray)) { | |||||
| int strPatternLength = strPattern.length(); | |||||
| StringBuilder sbuf = new StringBuilder(strPatternLength + 50); | |||||
| int handledPosition = 0; | |||||
| for (int argIndex = 0; argIndex < argArray.length; ++argIndex) { | |||||
| int delimIndex = strPattern.indexOf("{}", handledPosition); | |||||
| if (delimIndex == -1) { | |||||
| if (handledPosition == 0) { | |||||
| return strPattern; | |||||
| } | |||||
| sbuf.append(strPattern, handledPosition, strPatternLength); | |||||
| return sbuf.toString(); | |||||
| } | |||||
| if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == 92) { | |||||
| if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == 92) { | |||||
| sbuf.append(strPattern, handledPosition, delimIndex - 1); | |||||
| sbuf.append(Convert.utf8Str(argArray[argIndex])); | |||||
| handledPosition = delimIndex + 2; | |||||
| } else { | |||||
| --argIndex; | |||||
| sbuf.append(strPattern, handledPosition, delimIndex - 1); | |||||
| sbuf.append('{'); | |||||
| handledPosition = delimIndex + 1; | |||||
| } | |||||
| } else { | |||||
| sbuf.append(strPattern, handledPosition, delimIndex); | |||||
| sbuf.append(Convert.utf8Str(argArray[argIndex])); | |||||
| handledPosition = delimIndex + 2; | |||||
| } | |||||
| } | |||||
| sbuf.append(strPattern, handledPosition, strPattern.length()); | |||||
| return sbuf.toString(); | |||||
| } else { | |||||
| return strPattern; | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,295 @@ | |||||
| package com.inspect.simulator.utils; | |||||
| import org.springframework.util.AntPathMatcher; | |||||
| import java.util.Collection; | |||||
| import java.util.Iterator; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| public class StringUtils extends org.apache.commons.lang3.StringUtils { | |||||
| private static final String NULLSTR = ""; | |||||
| private static final char SEPARATOR = '_'; | |||||
| public static final String SLASH = "/"; | |||||
| public static final String COMMA = ","; | |||||
| public static final String HASHTAG = "#"; | |||||
| public static final String UNDERLINE = "_"; | |||||
| public static final String QUESTION = "\\?"; | |||||
| public static final String AT = "@"; | |||||
| public static final String COLON = ":"; | |||||
| public static final String DASH = "-"; | |||||
| public static final String PERCENT = "%"; | |||||
| public static <T> T nvl(T value, T defaultValue) { | |||||
| return value != null ? value : defaultValue; | |||||
| } | |||||
| public static boolean isEmpty(Collection<?> coll) { | |||||
| return isNull(coll) || coll.isEmpty(); | |||||
| } | |||||
| public static boolean isNotEmpty(Collection<?> coll) { | |||||
| return !isEmpty(coll); | |||||
| } | |||||
| public static boolean isEmpty(Object[] objects) { | |||||
| return isNull(objects) || objects.length == 0; | |||||
| } | |||||
| public static boolean isNotEmpty(Object[] objects) { | |||||
| return !isEmpty(objects); | |||||
| } | |||||
| public static boolean isEmpty(Map<?, ?> map) { | |||||
| return isNull(map) || map.isEmpty(); | |||||
| } | |||||
| public static boolean isNotEmpty(Map<?, ?> map) { | |||||
| return !isEmpty(map); | |||||
| } | |||||
| public static boolean isEmpty(String str) { | |||||
| return isNull(str) || "".equals(str.trim()); | |||||
| } | |||||
| public static boolean isNotEmpty(String str) { | |||||
| return !isEmpty(str); | |||||
| } | |||||
| public static boolean isNull(Object object) { | |||||
| return object == null; | |||||
| } | |||||
| public static boolean isNotNull(Object object) { | |||||
| return !isNull(object); | |||||
| } | |||||
| public static boolean isArray(Object object) { | |||||
| return isNotNull(object) && object.getClass().isArray(); | |||||
| } | |||||
| public static String trim(String str) { | |||||
| return str == null ? "" : str.trim(); | |||||
| } | |||||
| public static String substring(String str, int start) { | |||||
| if (str == null) { | |||||
| return ""; | |||||
| } else { | |||||
| if (start < 0) { | |||||
| start += str.length(); | |||||
| } | |||||
| if (start < 0) { | |||||
| start = 0; | |||||
| } | |||||
| return start > str.length() ? "" : str.substring(start); | |||||
| } | |||||
| } | |||||
| public static String substring(String str, int start, int end) { | |||||
| if (str == null) { | |||||
| return ""; | |||||
| } else { | |||||
| if (end < 0) { | |||||
| end += str.length(); | |||||
| } | |||||
| if (start < 0) { | |||||
| start += str.length(); | |||||
| } | |||||
| if (end > str.length()) { | |||||
| end = str.length(); | |||||
| } | |||||
| if (start > end) { | |||||
| return ""; | |||||
| } else { | |||||
| if (start < 0) { | |||||
| start = 0; | |||||
| } | |||||
| if (end < 0) { | |||||
| end = 0; | |||||
| } | |||||
| return str.substring(start, end); | |||||
| } | |||||
| } | |||||
| } | |||||
| public static boolean hasText(String str) { | |||||
| return str != null && !str.isEmpty() && containsText(str); | |||||
| } | |||||
| private static boolean containsText(CharSequence str) { | |||||
| int strLen = str.length(); | |||||
| for (int i = 0; i < strLen; ++i) { | |||||
| if (!Character.isWhitespace(str.charAt(i))) { | |||||
| return true; | |||||
| } | |||||
| } | |||||
| return false; | |||||
| } | |||||
| public static String format(String template, Object... params) { | |||||
| return !isEmpty(params) && !isEmpty(template) ? StrFormatter.format(template, params) : template; | |||||
| } | |||||
| public static boolean isHttp(String link) { | |||||
| return startsWithAny(link, new CharSequence[]{"http://", "https://"}); | |||||
| } | |||||
| public static String toUnderScoreCase(String str) { | |||||
| if (str == null) { | |||||
| return null; | |||||
| } else { | |||||
| StringBuilder sb = new StringBuilder(); | |||||
| boolean preCharIsUpperCase = true; | |||||
| boolean curreCharIsUpperCase = true; | |||||
| boolean nexteCharIsUpperCase = true; | |||||
| for (int i = 0; i < str.length(); ++i) { | |||||
| char c = str.charAt(i); | |||||
| if (i > 0) { | |||||
| preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1)); | |||||
| } else { | |||||
| preCharIsUpperCase = false; | |||||
| } | |||||
| curreCharIsUpperCase = Character.isUpperCase(c); | |||||
| if (i < str.length() - 1) { | |||||
| nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1)); | |||||
| } | |||||
| if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) { | |||||
| sb.append('_'); | |||||
| } else if (i != 0 && !preCharIsUpperCase && curreCharIsUpperCase) { | |||||
| sb.append('_'); | |||||
| } | |||||
| sb.append(Character.toLowerCase(c)); | |||||
| } | |||||
| return sb.toString(); | |||||
| } | |||||
| } | |||||
| public static boolean inStringIgnoreCase(String str, String... strs) { | |||||
| if (str != null && strs != null) { | |||||
| for (String s : strs) { | |||||
| if (str.equalsIgnoreCase(trim(s))) { | |||||
| return true; | |||||
| } | |||||
| } | |||||
| } | |||||
| return false; | |||||
| } | |||||
| public static String convertToCamelCase(String name) { | |||||
| StringBuilder result = new StringBuilder(); | |||||
| if (name != null && !name.isEmpty()) { | |||||
| if (!name.contains("_")) { | |||||
| return name.substring(0, 1).toUpperCase() + name.substring(1); | |||||
| } else { | |||||
| String[] camels = name.split("_"); | |||||
| for (String camel : camels) { | |||||
| if (!camel.isEmpty()) { | |||||
| result.append(camel.substring(0, 1).toUpperCase()); | |||||
| result.append(camel.substring(1).toLowerCase()); | |||||
| } | |||||
| } | |||||
| return result.toString(); | |||||
| } | |||||
| } else { | |||||
| return ""; | |||||
| } | |||||
| } | |||||
| public static String toCamelCase(String s) { | |||||
| if (s == null) { | |||||
| return null; | |||||
| } else { | |||||
| s = s.toLowerCase(); | |||||
| StringBuilder sb = new StringBuilder(s.length()); | |||||
| boolean upperCase = false; | |||||
| for (int i = 0; i < s.length(); ++i) { | |||||
| char c = s.charAt(i); | |||||
| if (c == 95) { | |||||
| upperCase = true; | |||||
| } else if (upperCase) { | |||||
| sb.append(Character.toUpperCase(c)); | |||||
| upperCase = false; | |||||
| } else { | |||||
| sb.append(c); | |||||
| } | |||||
| } | |||||
| return sb.toString(); | |||||
| } | |||||
| } | |||||
| public static boolean matches(String str, List<String> strs) { | |||||
| if (!isEmpty(str) && !isEmpty(strs)) { | |||||
| Iterator<String> iterator = strs.iterator(); | |||||
| String pattern; | |||||
| do { | |||||
| if (!iterator.hasNext()) { | |||||
| return false; | |||||
| } | |||||
| pattern = iterator.next(); | |||||
| } while (!isMatch(pattern, str)); | |||||
| return true; | |||||
| } else { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| public static boolean isMatch(String pattern, String url) { | |||||
| AntPathMatcher matcher = new AntPathMatcher(); | |||||
| return matcher.match(pattern, url); | |||||
| } | |||||
| public static <T> T cast(Object obj) { | |||||
| return (T) obj; | |||||
| } | |||||
| public static final String padl(Number num, int size) { | |||||
| return padl(num.toString(), size, '0'); | |||||
| } | |||||
| public static final String padl(String s, int size, char c) { | |||||
| StringBuilder sb = new StringBuilder(size); | |||||
| int len; | |||||
| if (s != null) { | |||||
| len = s.length(); | |||||
| if (s.length() > size) { | |||||
| return s.substring(len - size, len); | |||||
| } | |||||
| for (int i = size - len; i > 0; --i) { | |||||
| sb.append(c); | |||||
| } | |||||
| sb.append(s); | |||||
| } else { | |||||
| for (len = size; len > 0; --len) { | |||||
| sb.append(c); | |||||
| } | |||||
| } | |||||
| return sb.toString(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,209 @@ | |||||
| package com.inspect.simulator.utils.sftp; | |||||
| import com.alibaba.fastjson.JSONObject; | |||||
| import com.inspect.simulator.constant.Color; | |||||
| import com.inspect.simulator.utils.StringUtils; | |||||
| import org.apache.commons.compress.utils.IOUtils; | |||||
| import org.apache.commons.net.ftp.FTPSClient; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import org.springframework.stereotype.Component; | |||||
| import java.io.*; | |||||
| @Component | |||||
| public class SftpClient { | |||||
| protected final Logger log = LoggerFactory.getLogger(this.getClass()); | |||||
| private final SftpLoginConfig sftpLoginConfig; | |||||
| private final SftpFactory sftpFactory; | |||||
| public SftpClient(SftpLoginConfig sftpLoginConfig, SftpFactory sftpFactory) { | |||||
| this.sftpLoginConfig = sftpLoginConfig; | |||||
| this.sftpFactory = sftpFactory; | |||||
| } | |||||
| public String upload(SftpUploadEntity sftpUploadEntity) throws Exception { | |||||
| // final String tmp = sftpUploadEntity.getFileFullName(); | |||||
| // final String fileFullName = tmp.startsWith(sftpLoginConfig.getBasePath()) ? tmp : sftpLoginConfig.getBasePath() + tmp; | |||||
| final String tmp = sftpUploadEntity.getFileFullName(); | |||||
| final String fileFullName = tmp.startsWith(sftpLoginConfig.getBasePath()) ? tmp : sftpLoginConfig.getBasePath() + tmp; | |||||
| final String fileFullNameEx = StringUtils.isNotEmpty(sftpLoginConfig.getPrefix()) | |||||
| ? sftpLoginConfig.getPrefix() + fileFullName | |||||
| : fileFullName; | |||||
| log.info(Color.GREEN + "[FTP] fileFullName: {}, fileFullNameEx: {}" + Color.END, fileFullName, fileFullNameEx); | |||||
| connect((ftps) -> { | |||||
| log.info(Color.GREEN + "[FTP] UPLOAD Entity: {}" + Color.END, JSONObject.toJSONString(sftpUploadEntity)); | |||||
| String path = fileFullNameEx.substring(0, fileFullNameEx.lastIndexOf(StringUtils.SLASH)); | |||||
| mkdir(ftps, path); | |||||
| boolean bOk = ftps.storeFile(fileFullNameEx, sftpUploadEntity.getInputStream()); | |||||
| log.info(Color.GREEN + "[FTP] UPLOAD {}, PATH: {}, FULL PATH: {}, REPLY CODE: {}, INFO: {}" + Color.END, bOk ? "OK" : "FAIL", path, fileFullNameEx, ftps.getReplyCode(), ftps.getReplyString()); | |||||
| sftpUploadEntity.getInputStream().close(); | |||||
| }); | |||||
| return sftpUploadEntity.toResultPath(sftpLoginConfig.getBasePath()); | |||||
| } | |||||
| public String upload(SftpUploadEntity sftpUploadEntity, Upload upload) throws Exception { | |||||
| final String tmp = sftpUploadEntity.getFileFullName(); | |||||
| final String fileFullName = tmp.startsWith(sftpLoginConfig.getBasePath()) ? tmp : sftpLoginConfig.getBasePath() + tmp; | |||||
| final String fileFullNameEx = StringUtils.isNotEmpty(sftpLoginConfig.getPrefix()) | |||||
| ? sftpLoginConfig.getPrefix() + fileFullName | |||||
| : fileFullName; | |||||
| log.info("[FTP] fileFullName: {}, fileFullNameEx: {}", fileFullName, fileFullNameEx); | |||||
| connect((ftps) -> { | |||||
| String path = fileFullNameEx.substring(0, fileFullNameEx.lastIndexOf("/")); | |||||
| mkdir(ftps, path); | |||||
| log.info("[FTP] UPLOAD fileFullName: {}", fileFullNameEx); | |||||
| OutputStream outputStream = ftps.storeFileStream(fileFullNameEx); | |||||
| log.info("[FTP] UPLOAD outputStream: {}", outputStream); | |||||
| upload.run(outputStream); | |||||
| outputStream.close(); | |||||
| log.info("[FTP] UPLOAD OK: {}", fileFullNameEx); | |||||
| }); | |||||
| return sftpUploadEntity.toResultPath(sftpLoginConfig.getBasePath()); | |||||
| } | |||||
| public void mkdir(FTPSClient ftps, String path) throws IOException { | |||||
| ftps.changeWorkingDirectory(StringUtils.SLASH); | |||||
| String[] dirs = path.split(StringUtils.SLASH); | |||||
| for (String dir : dirs) { | |||||
| //log.info("[FTP] dir: {}", dir); | |||||
| if (StringUtils.isEmpty(dir)) { | |||||
| continue; | |||||
| } | |||||
| if (!ftps.changeWorkingDirectory(dir)) { | |||||
| ftps.makeDirectory(dir); | |||||
| ftps.changeWorkingDirectory(dir); | |||||
| } | |||||
| } | |||||
| } | |||||
| public void downLoad(String fileFullPath, Download download) throws Exception { | |||||
| final String fileFullPathTmp | |||||
| = fileFullPath.startsWith(sftpLoginConfig.getBasePath()) ? fileFullPath : sftpLoginConfig.getBasePath() + fileFullPath; | |||||
| final String fileFullNameTmpEx = StringUtils.isNotEmpty(sftpLoginConfig.getPrefix()) ? sftpLoginConfig.getPrefix() + fileFullPathTmp : fileFullPathTmp; | |||||
| log.info(Color.CYAN + "[FTP] fileFullPath: {}, fileFullNameTmpEx: {}" + Color.END, fileFullPath, fileFullPathTmp); | |||||
| connect((ftps) -> { | |||||
| InputStream inputStream = ftps.retrieveFileStream(fileFullNameTmpEx); | |||||
| if (inputStream == null) { | |||||
| log.error("[FTP] DOWNLOAD FAIL, EMPTY STREAM: {}", fileFullPath); | |||||
| } else { | |||||
| download.run(inputStream); | |||||
| inputStream.close(); | |||||
| } | |||||
| }); | |||||
| } | |||||
| private void connect(JoinPoint joinPoint) throws Exception { | |||||
| log.debug("[FTP} START <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); | |||||
| long start = System.currentTimeMillis(); | |||||
| FTPSClient ftps = sftpFactory.connect(); | |||||
| try { | |||||
| ftps.setFileType(FTPSClient.BINARY_FILE_TYPE); | |||||
| } catch (IOException e) { | |||||
| log.error("[FTP] TIMEOUT, RETRY"); | |||||
| ftps = sftpFactory.connect(); | |||||
| ftps.setFileType(FTPSClient.BINARY_FILE_TYPE); | |||||
| sftpFactory.init(); | |||||
| } | |||||
| ftps.enterLocalPassiveMode(); | |||||
| ftps.enterLocalPassiveMode(); | |||||
| ftps.setFileTransferMode(FTPSClient.STREAM_TRANSFER_MODE); | |||||
| ftps.execPROT("P"); | |||||
| log.debug("[FTP] CONN COST: {}", System.currentTimeMillis() - start); | |||||
| start = System.currentTimeMillis(); | |||||
| joinPoint.run(ftps); | |||||
| //log.info("[FTP] TRANSPORT COST: {}", System.currentTimeMillis() - start); | |||||
| log.debug("[FTP] END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |||||
| ftps.logout(); | |||||
| } | |||||
| public interface Upload { | |||||
| void run(OutputStream outputStream) throws Exception; | |||||
| } | |||||
| public interface Download { | |||||
| void run(InputStream inputStream) throws Exception; | |||||
| } | |||||
| interface JoinPoint { | |||||
| void run(FTPSClient ftpsClient) throws Exception; | |||||
| } | |||||
| public static void main(String[] args) throws Exception { | |||||
| // try { | |||||
| // FTPSClient ftps = new FTPSClient(true); | |||||
| // ftps.connect("192.168.1.198", 990); | |||||
| // boolean loginRes = ftps.login("ftpuser", "atia2018"); | |||||
| // System.out.println(loginRes); | |||||
| // ftps.setFileType(2); | |||||
| // //ftps.enterLocalPassiveMode(); | |||||
| // //ftps.enterLocalPassiveMode(); | |||||
| // ftps.setControlEncoding("UTF-8"); | |||||
| // ftps.setFileTransferMode(10); | |||||
| // ftps.execPROT("P"); | |||||
| // ftps.storeFile("11.jpg", new FileInputStream("E:/1.jpg")); | |||||
| // | |||||
| // String fileFullPathTmp = "1/2025/02/26/1339/CCD/1339_20250226095445_2023833_02160558340721910141.jpg"; | |||||
| // InputStream inputStream = ftps.retrieveFileStream(fileFullPathTmp); | |||||
| // if (inputStream == null) { | |||||
| // System.out.println("[FTP] DOWNLOAD FAIL, EMPTY STREAM:" + fileFullPathTmp); | |||||
| // } else { | |||||
| // File tempFile = File.createTempFile("E:\\test001234", ".jpg"); | |||||
| // tempFile.deleteOnExit(); | |||||
| // FileOutputStream out = new FileOutputStream(tempFile); | |||||
| // IOUtils.copy(inputStream, out); | |||||
| // inputStream.close(); | |||||
| // } | |||||
| // | |||||
| // | |||||
| // } catch (Exception e) { | |||||
| // e.printStackTrace(); | |||||
| // } | |||||
| try { | |||||
| FTPSClient ftps = new FTPSClient(true); | |||||
| // ftps.connect("192.168.1.198", 21); | |||||
| // boolean loginRes = ftps.login("hangtian", "123qweasd"); | |||||
| // ftps.connect("124.221.104.159", 10990); | |||||
| // ftps.connect("124.221.104.159", 10990); | |||||
| ftps.connect("192.168.1.116", 10990); | |||||
| boolean loginRes = ftps.login("ftpuser", "atia2018"); | |||||
| System.out.println(loginRes); | |||||
| ftps.setFileType(2); | |||||
| // ftps.enterLocalPassiveMode(); | |||||
| ftps.enterLocalPassiveMode(); | |||||
| ftps.setControlEncoding("UTF-8"); | |||||
| ftps.setFileTransferMode(10); | |||||
| ftps.execPROT("P"); | |||||
| ftps.storeFile("1122234567.jpg", new FileInputStream("E:/1.jpg")); | |||||
| String fileFullPathTmp = "1122.jpg"; | |||||
| //String fileFullPathTmp = "/home/hangtian/11.jpg"; | |||||
| InputStream inputStream = ftps.retrieveFileStream(fileFullPathTmp); | |||||
| if (inputStream == null) { | |||||
| System.out.println("[FTP] DOWNLOAD FAIL, EMPTY STREAM:" + fileFullPathTmp); | |||||
| } else { | |||||
| File tempFile = File.createTempFile("test0012345555", ".jpg"); | |||||
| tempFile.deleteOnExit(); | |||||
| FileOutputStream out = new FileOutputStream(tempFile); | |||||
| IOUtils.copy(inputStream, out); | |||||
| inputStream.close(); | |||||
| } | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,6 @@ | |||||
| package com.inspect.simulator.utils.sftp; | |||||
| public class SftpConstant { | |||||
| public static final String CHANNEL_SNAP_PATH = "snap/"; | |||||
| public static final String FILE_SUFFIX_JPG = ".jpg"; | |||||
| } | |||||
| @ -0,0 +1,121 @@ | |||||
| package com.inspect.simulator.utils.sftp; | |||||
| import com.alibaba.fastjson.JSONObject; | |||||
| import com.inspect.simulator.constant.Color; | |||||
| import com.inspect.simulator.exception.ServiceException; | |||||
| import org.apache.commons.net.ftp.FTPSClient; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Component; | |||||
| import javax.annotation.PostConstruct; | |||||
| import java.io.IOException; | |||||
| import java.util.ArrayDeque; | |||||
| import java.util.Queue; | |||||
| @Component | |||||
| public class SftpFactory { | |||||
| protected final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||||
| @Autowired | |||||
| private SftpLoginConfig sftpLoginConfig; | |||||
| public volatile Queue<FTPSClient> queue = new ArrayDeque<>(); | |||||
| public int aliveNum = 20; | |||||
| public int initNum = 5; | |||||
| public synchronized FTPSClient getInstance() { | |||||
| return !this.queue.isEmpty() ? this.queue.remove() : null; | |||||
| } | |||||
| public void reset() { | |||||
| (new Thread(() -> { | |||||
| if (this.queue.size() < this.aliveNum) { | |||||
| try { | |||||
| this.queue.add(this.connect()); | |||||
| } catch (IOException var2) { | |||||
| ; | |||||
| } | |||||
| } | |||||
| })).start(); | |||||
| } | |||||
| @PostConstruct | |||||
| public void init() throws IOException { | |||||
| (new Thread(() -> { | |||||
| this.queue.clear(); | |||||
| for (int i = 0; i < this.initNum; ++i) { | |||||
| FTPSClient ftps = null; | |||||
| try { | |||||
| ftps = this.connect(); | |||||
| this.queue.add(ftps); | |||||
| } catch (IOException e) { | |||||
| ; | |||||
| } catch (ServiceException e) { | |||||
| if (!"Error".equals(e.getMessage())) { | |||||
| throw e; | |||||
| } | |||||
| } | |||||
| } | |||||
| })).start(); | |||||
| } | |||||
| public FTPSClient connect() throws IOException { | |||||
| boolean isImplicit = "true".equals(sftpLoginConfig.getIsImplicit()); | |||||
| //boolean isImplicit = Boolean.getBoolean(sftpLoginConfig.getIsImplicit()); | |||||
| logger.info(Color.MAGENTA + "[FTP] sftpLoginConfig: {}" + Color.END, sftpLoginConfig); | |||||
| FTPSClient ftps = new FTPSClient(isImplicit); | |||||
| try { | |||||
| ftps.setControlEncoding("UTF-8"); | |||||
| ftps.connect(sftpLoginConfig.getIp(), Integer.parseInt(sftpLoginConfig.getPort())); | |||||
| } catch (Exception e) { | |||||
| logger.error("[FTP] CONN FAIL: {}, CAUSE: {}", JSONObject.toJSONString(sftpLoginConfig), e.getMessage()); | |||||
| try { | |||||
| Thread.sleep(1000L); | |||||
| } catch (InterruptedException e2) { | |||||
| ; | |||||
| } | |||||
| return connect(); | |||||
| } | |||||
| boolean login = ftps.login(sftpLoginConfig.getUsername(), sftpLoginConfig.getPassword()); | |||||
| if (!login) { | |||||
| throw new ServiceException("LOGIN TO FTP FAIL!"); | |||||
| } else { | |||||
| return ftps; | |||||
| } | |||||
| } | |||||
| public static void main(String[] args) { | |||||
| for (int i = 0; i < 100; i++) { | |||||
| try { | |||||
| FTPSClient ftps = new FTPSClient(true); | |||||
| ftps.setControlEncoding("UTF-8"); | |||||
| ftps.connect("192.168.1.198", 990); | |||||
| //ftps.connect("192.168.1.116", 1990); | |||||
| boolean login = ftps.login("ftpuser", "atia2018"); | |||||
| System.out.println("ss:" + login); | |||||
| } catch (Exception var5) { | |||||
| var5.printStackTrace(); | |||||
| try { | |||||
| Thread.sleep(1000L); | |||||
| } catch (InterruptedException var4) { | |||||
| ; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,35 @@ | |||||
| package com.inspect.simulator.utils.sftp; | |||||
| import lombok.Getter; | |||||
| import lombok.Setter; | |||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | |||||
| import org.springframework.stereotype.Component; | |||||
| @Setter | |||||
| @Getter | |||||
| @Component | |||||
| @ConfigurationProperties( | |||||
| prefix = "sftp" | |||||
| ) | |||||
| public class SftpLoginConfig { | |||||
| private String ip; | |||||
| private String port; | |||||
| private String username; | |||||
| private String password; | |||||
| private String isImplicit; | |||||
| private String basePath; | |||||
| private String prefix; | |||||
| @Override | |||||
| public String toString() { | |||||
| return "SftpLoginConfig{" + | |||||
| "ip='" + ip + '\'' + | |||||
| ", port='" + port + '\'' + | |||||
| ", username='" + username + '\'' + | |||||
| ", password='" + password + '\'' + | |||||
| ", isImplicit='" + isImplicit + '\'' + | |||||
| ", basePath='" + basePath + '\'' + | |||||
| ", prefix='" + prefix + '\'' + | |||||
| '}'; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,61 @@ | |||||
| package com.inspect.simulator.utils.sftp; | |||||
| import com.inspect.simulator.utils.StringUtils; | |||||
| import lombok.Getter; | |||||
| import lombok.Setter; | |||||
| import java.io.InputStream; | |||||
| import java.util.UUID; | |||||
| public class SftpUploadEntity { | |||||
| @Getter | |||||
| private String filePath = ""; | |||||
| @Getter | |||||
| private String fileName = UUID.randomUUID().toString(); | |||||
| @Setter | |||||
| @Getter | |||||
| private String suffix = ".jpg"; | |||||
| @Setter | |||||
| private String fileFullName; | |||||
| @Setter | |||||
| @Getter | |||||
| private InputStream inputStream; | |||||
| public SftpUploadEntity() { | |||||
| } | |||||
| public SftpUploadEntity(InputStream inputStream) { | |||||
| this.inputStream = inputStream; | |||||
| } | |||||
| public void setFilePath(String filePath) { | |||||
| this.filePath = filePath; | |||||
| if (filePath.contains(".")) { | |||||
| String[] split = filePath.split("\\."); | |||||
| this.filePath = split[0]; | |||||
| this.suffix = "." + split[split.length - 1]; | |||||
| } else if (!filePath.endsWith("/")) { | |||||
| this.filePath = this.filePath + "/"; | |||||
| } | |||||
| } | |||||
| public void setFileName(String fileName) { | |||||
| this.fileName = fileName; | |||||
| if (fileName.contains(".")) { | |||||
| String[] split = fileName.split("\\."); | |||||
| this.fileName = split[0]; | |||||
| this.suffix = "." + split[split.length - 1]; | |||||
| } | |||||
| } | |||||
| public String getFileFullName() { | |||||
| return !StringUtils.isEmpty(this.fileFullName) ? this.fileFullName : this.filePath + this.fileName + this.suffix; | |||||
| } | |||||
| public String toResultPath(String basePath) { | |||||
| this.fileFullName = this.getFileFullName(); | |||||
| return this.fileFullName.startsWith(basePath) ? this.fileFullName.replaceFirst(basePath, "") : this.fileFullName; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,120 @@ | |||||
| package com.inspect.simulator.utils.sftp; | |||||
| import com.inspect.simulator.utils.StringUtils; | |||||
| import org.apache.commons.net.ftp.FTPClient; | |||||
| import org.apache.commons.net.ftp.FTPFile; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Component; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| @Component | |||||
| public class ftpClient { | |||||
| protected final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||||
| @Autowired | |||||
| private static SftpClient sftpClient; | |||||
| public static InputStream login(String host, int port, String username, String password, String fileName, String path) throws Exception { | |||||
| InputStream inputStream = null; | |||||
| FTPClient ftp = new FTPClient(); | |||||
| ftp.setControlEncoding("UTF-8"); | |||||
| ftp.connect(host, port); | |||||
| boolean success = ftp.login(username, password); | |||||
| if (success) { | |||||
| inputStream = downLoad(ftp, fileName, path); | |||||
| logout(ftp); | |||||
| } | |||||
| return inputStream; | |||||
| } | |||||
| public static void logout(FTPClient ftps) throws IOException { | |||||
| if (ftps != null && ftps.isConnected()) { | |||||
| ftps.disconnect(); | |||||
| } | |||||
| } | |||||
| public static boolean existFile(FTPClient ftp, String path, String fileName) throws IOException { | |||||
| boolean flag = false; | |||||
| String[] str = path.split(StringUtils.SLASH); | |||||
| for (String s : str) { | |||||
| ftp.changeWorkingDirectory(s); | |||||
| } | |||||
| FTPFile[] files = ftp.listFiles(); | |||||
| if (files != null) { | |||||
| for (FTPFile file : files) { | |||||
| if (file.getName().equals(fileName)) { | |||||
| System.out.println("File exists." + fileName); | |||||
| flag = true; | |||||
| break; | |||||
| } | |||||
| } | |||||
| } | |||||
| return flag; | |||||
| } | |||||
| public static void mkdir(FTPClient ftps, String path) throws IOException { | |||||
| String[] split = path.split(StringUtils.SLASH); | |||||
| ftps.changeWorkingDirectory(StringUtils.SLASH); | |||||
| for (String str : split) { | |||||
| if (ftps.changeWorkingDirectory(str)) { | |||||
| ftps.changeWorkingDirectory(str); | |||||
| } else { | |||||
| ftps.makeDirectory(str); | |||||
| ftps.changeWorkingDirectory(str); | |||||
| } | |||||
| } | |||||
| } | |||||
| public static InputStream downLoad(FTPClient ftp, String fileName, String path) { | |||||
| InputStream inputStream = null; | |||||
| Object object; | |||||
| try { | |||||
| ftp.enterLocalPassiveMode(); | |||||
| ftp.setFileType(2); | |||||
| boolean bl = existFile(ftp, path, fileName); | |||||
| if (bl) { | |||||
| inputStream = ftp.retrieveFileStream(path); | |||||
| ftp.logout(); | |||||
| return inputStream; | |||||
| } | |||||
| object = null; | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| return inputStream; | |||||
| } finally { | |||||
| try { | |||||
| if (ftp.isConnected()) { | |||||
| ftp.disconnect(); | |||||
| } | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| return (InputStream) object; | |||||
| } | |||||
| public static void main(String[] args) throws Exception { | |||||
| String host = "10.10.18.92"; | |||||
| int port = 21; | |||||
| String username = "xg"; | |||||
| String password = "Xg@123456"; | |||||
| String fileName = "极1低,极1高,极2高,极2低巡检任务20240226150000.xls"; | |||||
| String path = "//极1低,极1高,极2高,极2低巡检任务20240226150000/"; | |||||
| InputStream str = login(host, port, username, password, fileName, path); | |||||
| System.out.println(str == null); | |||||
| } | |||||
| } | |||||