From b5326f8d497a6f6e97a487cb9c565fdae1dc4790 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期四, 12 九月 2024 17:32:55 +0800 Subject: [PATCH] 添加基础类 --- src/main/java/com/se/simu/helper/WebHelper.java | 6 src/main/java/com/se/simu/helper/StringHelper.java | 253 ++++++++++++ src/main/java/com/se/simu/controller/WaterController.java | 9 src/main/java/com/se/simu/service/SedbService.java | 48 ++ src/main/java/com/se/simu/config/Knife4jConfig.java | 69 +++ src/main/java/com/se/simu/config/RestTemplateConfig.java | 100 ++++ src/main/java/com/se/simu/helper/GdalHelper.java | 116 +++++ pom.xml | 47 ++ src/main/java/com/se/simu/config/InitConfig.java | 48 ++ src/main/java/com/se/simu/config/MybatisPlusConfig.java | 48 ++ src/main/resources/application.yml | 25 + src/main/java/com/se/simu/helper/FileHelper.java | 444 +++++++++++++++++++++ 12 files changed, 1,212 insertions(+), 1 deletions(-) diff --git a/pom.xml b/pom.xml index d4b09d1..4d71531 100644 --- a/pom.xml +++ b/pom.xml @@ -35,11 +35,50 @@ <version>1.18.34</version> <optional>true</optional> </dependency> + <!--aop--> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-aop</artifactId> + </dependency> + <!--mybatis-plus--> + <dependency> + <groupId>com.baomidou</groupId> + <artifactId>mybatis-plus-boot-starter</artifactId> + <version>3.5.7</version> + </dependency> + <!--redis--> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-redis</artifactId> + </dependency> + <!--text--> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-text</artifactId> + <version>1.12.0</version> + </dependency> <!--hutool--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.29</version> + </dependency> + <!--httpclient--> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + <!--knife4j--> + <dependency> + <groupId>com.github.xiaoymin</groupId> + <artifactId>knife4j-spring-boot-starter</artifactId> + <version>3.0.3</version> + </dependency> + <!--fast-md5--> + <dependency> + <groupId>com.joyent.util</groupId> + <artifactId>fast-md5</artifactId> + <version>2.7.1</version> </dependency> <!--fastjson--> <dependency> @@ -52,6 +91,14 @@ <artifactId>fastjson2</artifactId> <version>2.0.52</version> </dependency--> + <!--gdal--> + <dependency> + <groupId>org.gdal</groupId> + <artifactId>gdal</artifactId> + <!--<version>3.9.0</version>--> + <version>3.5.0</version> + <!--version>3.2.0</version--> + </dependency> </dependencies> <build> diff --git a/src/main/java/com/se/simu/config/InitConfig.java b/src/main/java/com/se/simu/config/InitConfig.java new file mode 100644 index 0000000..9553d32 --- /dev/null +++ b/src/main/java/com/se/simu/config/InitConfig.java @@ -0,0 +1,48 @@ +package com.se.simu.config; + +import com.se.simu.helper.GdalHelper; +import com.se.simu.helper.WebHelper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * 鍒濆鍖栭厤缃被 + * + * @author WWW + * @date 2024-09-12 + */ +@Slf4j +@Component +public class InitConfig implements ApplicationRunner { + @Resource + Environment env; + + @Value("${server.port}") + String serverPort; + + @Value("${server.servlet.context-path}") + String contextPath; + + @Override + public void run(ApplicationArguments args) { + // noinspection AlibabaRemoveCommentedCode + try { + log.info("***************** 鍒濆鍖� GDAL *****************" + "\n"); + GdalHelper.init(env.getProperty("sys.path.gdalPath")); + + String path = null != contextPath && contextPath.length() > 1 ? contextPath : ""; + log.info("API鏂囨。:http://localhost:" + serverPort + path + "/doc.html"); + log.info("API鏂囨。:http://{}:{}{}/doc.html", WebHelper.getHostIp(), serverPort, path); + + log.info("***************** 绯荤粺鍚姩瀹屾瘯 *****************" + "\n"); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } +} diff --git a/src/main/java/com/se/simu/config/Knife4jConfig.java b/src/main/java/com/se/simu/config/Knife4jConfig.java new file mode 100644 index 0000000..62077df --- /dev/null +++ b/src/main/java/com/se/simu/config/Knife4jConfig.java @@ -0,0 +1,69 @@ +package com.se.simu.config; + +import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +/** + * Knife4j閰嶇疆绫� + * + * @author WWW + * @date 2024-09-12 + */ +@Configuration +@EnableKnife4j +public class Knife4jConfig { + @Value("${server.port}") + String serverPort; + + @Value("${knife4j.enabled}") + private boolean enabled; + + @Value("${knife4j.pathMapping}") + private String pathMapping; + + @Value("${server.servlet.context-path}") + String contextPath; + + @Bean + public Docket createRestApi() { + return new Docket(new DocumentationType("openApi", "3.0")) + // 鏄惁鍚敤Swagger + .enable(enabled) + // 鐢ㄦ潵鍒涘缓璇PI鐨勫熀鏈俊鎭紝灞曠ず鍦ㄦ枃妗g殑椤甸潰涓紙鑷畾涔夊睍绀虹殑淇℃伅锛� + .apiInfo(apiInfo()) + // 鍒嗙粍鍚嶇О + .groupName("鏈嶅姟") + // 璁剧疆鍝簺鎺ュ彛鏆撮湶缁橲wagger灞曠ず + .select() + // 鎵弿鎵�鏈夋湁娉ㄨВ鐨刟pi锛岀敤杩欑鏂瑰紡鏇寸伒娲� + .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) + // 鎵弿鎸囧畾鍖呬腑鐨剆wagger娉ㄨВ + // .apis(RequestHandlerSelectors.basePackage("com.cn.project.tool.swagger")) + // 鎵弿鎵�鏈� .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build() + /* 璁剧疆瀹夊叏妯″紡锛宻wagger鍙互璁剧疆璁块棶token */ + // .securitySchemes(securitySchemes()) + .pathMapping(pathMapping); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + //鎻忚堪瀛楁鏀寔Markdown璇硶 + .description("鎺ュ彛鏂囨。") + .contact(new Contact("WuWeiwei", "http://127.0.0.1:" + serverPort + contextPath + "/doc.html", "252740454@qq.com")) + .version("0.2") + .title("AI鍚庡彴鏈嶅姟鎺ュ彛鏂囨。") + .build(); + } +} diff --git a/src/main/java/com/se/simu/config/MybatisPlusConfig.java b/src/main/java/com/se/simu/config/MybatisPlusConfig.java new file mode 100644 index 0000000..30072ef --- /dev/null +++ b/src/main/java/com/se/simu/config/MybatisPlusConfig.java @@ -0,0 +1,48 @@ +package com.se.simu.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.apache.ibatis.type.JdbcType; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * Mybatis-Plus鍒嗛〉閰嶇疆 + * + * @author WWW + * @date 2024-09-12 + */ +@EnableTransactionManagement +@Configuration +@MapperScan("com.se.ai.mapper") +public class MybatisPlusConfig { + /** + * 鏂扮殑鍒嗛〉鎻掍欢,涓�缂撳拰浜岀紦閬靛惊mybatis鐨勮鍒�,闇�瑕佽缃� + * MybatisConfiguration#useDeprecatedExecutor = false + * 閬垮厤缂撳瓨鍑虹幇闂(璇ュ睘鎬т細鍦ㄦ棫鎻掍欢绉婚櫎鍚庝竴鍚岀Щ闄�) + */ + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL)); + return interceptor; + } + + @Bean + public ConfigurationCustomizer configurationCustomizer() { + return new ConfigurationCustomizer() { + @Override + public void customize(MybatisConfiguration configuration) { + configuration.setCacheEnabled(true); + configuration.setMapUnderscoreToCamelCase(true); + configuration.setCallSettersOnNulls(true); + configuration.setJdbcTypeForNull(JdbcType.NULL); + } + }; + } +} diff --git a/src/main/java/com/se/simu/config/RestTemplateConfig.java b/src/main/java/com/se/simu/config/RestTemplateConfig.java new file mode 100644 index 0000000..d30a9d8 --- /dev/null +++ b/src/main/java/com/se/simu/config/RestTemplateConfig.java @@ -0,0 +1,100 @@ +package com.se.simu.config; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +/** + * RestTemplate閰嶇疆绫� + * + * @author WWW + * @date 2024-09-12 + */ +@Configuration +public class RestTemplateConfig { + /** + * 杩炴帴姹犵殑鏈�澶ц繛鎺ユ暟榛樿涓�0锛屼笉闄愬埗 + */ + @Value("${remote.maxTotalConnect:0}") + private int maxTotalConnect; + + /** + * 鍗曚釜涓绘満鐨勬渶澶ц繛鎺ユ暟 + */ + @Value("${remote.maxConnectPerRoute:1000}") + private int maxConnectPerRoute; + + /** + * 杩炴帴瓒呮椂榛樿5s锛�-1涓轰笉闄愬埗 + */ + @Value("${remote.connectTimeout:5000}") + private int connectTimeout; + + /** + * 璇诲彇瓒呮椂榛樿30s锛�-1涓轰笉闄愬埗 + */ + @Value("${remote.readTimeout:30000}") + private int readTimeout; + + /** + * 鍒涘缓HTTP瀹㈡埛绔伐鍘� + * + * @return 瀹㈡埛绔伐鍘� + */ + private ClientHttpRequestFactory createFactory() { + if (this.maxTotalConnect <= 0) { + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); + factory.setConnectTimeout(this.connectTimeout); + factory.setReadTimeout(this.readTimeout); + return factory; + } + + HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(this.maxTotalConnect).setMaxConnPerRoute(this.maxConnectPerRoute).build(); + + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); + factory.setConnectTimeout(this.connectTimeout); + factory.setReadTimeout(this.readTimeout); + + return factory; + } + + /** + * 鍒濆鍖朢estTemplate,骞跺姞鍏pring鐨凚ean宸ュ巶锛岀敱spring缁熶竴绠$悊 + * 蹇呴』鍔犳敞瑙LoadBalanced + * + * @return + */ + @Bean + @ConditionalOnMissingBean(RestTemplate.class) + public RestTemplate getRestTemplate() { + RestTemplate restTemplate = new RestTemplate(this.createFactory()); + List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters(); + + // 閲嶆柊璁剧疆StringHttpMessageConverter瀛楃闆嗕负UTF-8锛岃В鍐充腑鏂囦贡鐮侀棶棰� + HttpMessageConverter<?> converterTarget = null; + for (HttpMessageConverter<?> item : converterList) { + if (StringHttpMessageConverter.class == item.getClass()) { + converterTarget = item; + break; + } + } + if (null != converterTarget) { + converterList.remove(converterTarget); + } + converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); + + return restTemplate; + } +} diff --git a/src/main/java/com/se/simu/controller/WaterController.java b/src/main/java/com/se/simu/controller/WaterController.java index f8a1bb9..de94f4a 100644 --- a/src/main/java/com/se/simu/controller/WaterController.java +++ b/src/main/java/com/se/simu/controller/WaterController.java @@ -1,6 +1,7 @@ package com.se.simu.controller; import com.se.simu.helper.WebHelper; +import com.se.simu.service.SedbService; import com.se.simu.service.WaterService; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -22,6 +23,9 @@ @RestController @RequestMapping("/waterlogging") public class WaterController { + @Resource + SedbService sedbService; + @Resource WaterService waterService; @@ -146,4 +150,9 @@ return true; } + + @GetMapping("/test") + public Object test() { + return sedbService.getToken(); + } } diff --git a/src/main/java/com/se/simu/helper/FileHelper.java b/src/main/java/com/se/simu/helper/FileHelper.java new file mode 100644 index 0000000..a2357e3 --- /dev/null +++ b/src/main/java/com/se/simu/helper/FileHelper.java @@ -0,0 +1,444 @@ +package com.se.simu.helper; + +import com.twmacinta.util.MD5; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.digest.DigestUtils; + +import java.io.*; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.text.DecimalFormat; +import java.util.List; + +/** + * 鏂囦欢甯姪绫� + * + * @author WWW + * @date 2024-09-12 + */ +@Slf4j +public class FileHelper { + public final static String POINT = "."; + + public final static int I16 = 16; + + public static final double D1024 = 1024.0; + + public static final double D1050 = 1050.0; + + public final static int I1000000 = 1000000; + + public static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + /** + * 鑾峰彇鏂囦欢鍚� + */ + public static String getFileName(String file) { + int idx = file.lastIndexOf(File.separator); + if (idx > -1) { + return file.substring(idx + 1); + } + + return ""; + } + + /** + * 鑾峰彇鏂囦欢鍚嶇О + */ + public static String getName(String file) { + String fileName = getFileName(file); + int idx = fileName.lastIndexOf("."); + if (idx > -1) { + return fileName.substring(0, idx); + } + + return fileName; + } + + /** + * 鑾峰彇鏂囦欢鎵╁睍鍚� + */ + public static String getExtension(File file) { + if (file == null) { + return null; + } + + String fileName = file.getName().toLowerCase(); + + int idx = fileName.indexOf(POINT); + if (idx == -1) { + return ""; + } + + return fileName.substring(idx); + } + + /** + * 鑾峰彇鏂囦欢鎵╁睍鍚� + */ + public static String getExtension(String fileName) { + if (StringHelper.isEmpty(fileName)) { + return ""; + } + + int idx = fileName.lastIndexOf(POINT); + if (idx == -1) { + return ""; + } + + return fileName.substring(idx).toLowerCase(); + } + + /** + * 鑾峰彇澶氱敤閫斾簰鑱旂綉閭欢鎵╁睍绫诲瀷 + */ + public static String getMime(String ext) { + switch (ext) { + // 鍥剧墖 + case ".tif": + case ".tiff": + return "image/tiff"; + case ".img": + return "application/x-img"; + case ".gif": + return "image/gif"; + case ".jpg": + case ".jpeg": + return "image/jpeg"; + case ".png": + return "image/png"; + // 闊�/瑙嗛 + case ".mp3": + return "audio/mp3"; + case ".mp4": + return "video/mpeg4"; + case ".avi": + return "video/avi"; + case ".mpg": + case ".mpeg": + return "video/mpg"; + case ".wav": + return "audio/wav"; + case ".wma": + return "audio/x-ms-wma"; + case ".swf": + return "application/x-shockwave-flash"; + case ".wmv": + return "video/x-ms-wmv"; + case ".rm": + return "application/vnd.rn-realmedia"; + case ".rmvb": + return "application/vnd.rn-realmedia-vbr"; + // 缃戦〉 + case ".js": + return "application/x-javascript"; + case ".css": + return "text/css"; + case ".asp": + return "text/asp"; + case ".mht": + return "message/rfc822"; + case ".jsp": + case ".htm": + case ".html": + case ".xhtml": + return "text/html"; + case ".xml": + case ".svg": + return "text/xml"; + // 鏂囦欢 + case ".txt": + return "text/plain"; + case ".dbf": + return "application/x-dbf"; + case ".mdb": + return "application/msaccess"; + case ".pdf": + return "application/pdf"; + case ".ppt": + case ".pptx": + return "application/x-ppt"; + case ".doc": + case ".docx": + return "application/msword"; + case ".xls": + case ".xlsx": + return "application/vnd.ms-excel"; + case ".dgn": + return "application/x-dgn"; + case ".dwg": + return "application/x-dwg"; + case ".ext": + return "application/x-msdownload"; + // 榛樿 + default: + return "application/octet-stream"; + } + } + + /** + * 瀛楄妭鍗曚綅鎹㈢畻 + */ + public static String formatByte(long byteNumber) { + double kbNumber = byteNumber / D1024; + if (kbNumber < D1024) { + return new DecimalFormat("#.##KB").format(kbNumber); + } + double mbNumber = kbNumber / D1024; + if (mbNumber < D1024) { + return new DecimalFormat("#.##MB").format(mbNumber); + } + double gbNumber = mbNumber / D1024; + if (gbNumber < D1024) { + return new DecimalFormat("#.##GB").format(gbNumber); + } + double tbNumber = gbNumber / D1024; + + return new DecimalFormat("#.##TB").format(tbNumber); + } + + /** + * 鑾峰彇骞虫柟绫� + */ + public static String getSquareMeter(double num) { + if (num < I1000000) { + return new DecimalFormat("#.##骞虫柟绫�").format(num); + } + + double knum = num / I1000000; + + return new DecimalFormat("#.##骞虫柟鍗冪背").format(knum); + } + + /** + * byte杞琈B + */ + public static double sizeToMb(long size) { + if (size < D1050) { + return 0.001; + } + + String str = String.format("%.3f", size / D1024 / D1024); + + return Double.parseDouble(str); + } + + /** + * 3.鑾峰彇鏂囦欢MD5鐮侊紙JDK锛� + */ + public static String getMd5ByJdk(String filePath) throws IOException { + FileInputStream fileStream = new FileInputStream(filePath); + String md5 = DigestUtils.md5Hex(fileStream); + fileStream.close(); + + return md5; + } + + /** + * 2.鑾峰彇蹇�� MD5 鐮� + */ + public static String getFastMd5(String filePath) throws IOException { + String hash = MD5.asHex(MD5.getHash(new File(filePath))); + + MD5 md5 = new MD5(); + md5.Update(hash, null); + + return md5.asHex(); + } + + /** + * 鍒犻櫎鏂囦欢澶� + * + * @param dir 鏂囦欢澶� + */ + public static void deleteDir(String dir) { + File file = new File(dir); + + deleteFiles(file); + } + + /** + * 绾ц仈鍒犻櫎鏂囦欢 + * + * @param file 鏂囦欢 + */ + public static void deleteFiles(File file) { + if (null == file || !file.exists()) { + return; + } + + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (null != files && files.length > 0) { + for (File f : files) { + if (f.isDirectory()) { + deleteFiles(f); + } else { + f.delete(); + } + } + } + } + + file.delete(); + } + + /** + * 鑾峰彇鐩稿璺緞 + * + * @param file 鏂囦欢 + * @return 鐩稿璺緞 + */ + public static String getRelativePath(String file) { + if (StringHelper.isEmpty(file)) { + return null; + } + + int idx = file.lastIndexOf(File.separator); + int start = file.lastIndexOf(File.separator, idx - 1); + + return file.substring(start + 1); + } + + /** + * 鑾峰彇璺緞 + * + * @param file 鏂囦欢 + * @return 鏂囦欢璺緞 + */ + public static String getPath(String file) { + if (StringHelper.isEmpty(file)) { + return null; + } + + int end = file.lastIndexOf(File.separator); + + return file.substring(0, end); + } + + /** + * 1.鑾峰彇鏂囦欢鐨凪D5 + */ + @SuppressWarnings("unused") + public static String getFileMd5(String filePath) { + FileInputStream fis = null; + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + + fis = new FileInputStream(new File(filePath)); + FileChannel fChannel = fis.getChannel(); + ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024); + + while (fChannel.read(buffer) != -1) { + buffer.flip(); + md.update(buffer); + buffer.compact(); + } + byte[] b = md.digest(); + + return byteToHexString(b); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } finally { + try { + if (null != fis) { + fis.close(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + /** + * 瀛楄妭鐮佽浆16杩涘埗 + */ + public static String byteToHexString(byte[] tmp) { + // 姣忎釜瀛楄妭鐢� 16 杩涘埗琛ㄧず鐨勮瘽锛屼娇鐢ㄤ袱涓瓧绗︼紝 + char[] str = new char[16 * 2]; + + // 鎵�浠ヨ〃绀烘垚 16 杩涘埗闇�瑕� 32 涓瓧绗︼紝琛ㄧず杞崲缁撴灉涓搴旂殑瀛楃浣嶇疆 + int k = 0; + // 浠庣涓�涓瓧鑺傚紑濮嬶紝瀵� MD5 鐨勬瘡涓�涓瓧鑺� + for (int i = 0; i < I16; i++) { + // 杞崲鎴� 16 杩涘埗瀛楃鐨勮浆鎹� + byte byte0 = tmp[i]; + // 鍙栧瓧鑺備腑楂� 4 浣嶇殑鏁板瓧杞崲 + str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf]; + // >>> 涓洪�昏緫鍙崇Щ锛屽皢绗﹀彿浣嶄竴璧峰彸绉伙紝 鍙栧瓧鑺備腑浣� 4 浣嶇殑鏁板瓧杞崲 + str[k++] = HEX_DIGITS[byte0 & 0xf]; + } + // 鎹㈠悗鐨勭粨鏋滆浆鎹负瀛楃涓� + return new String(str); + } + + /** + * 鑾峰彇瀛楃涓茬殑MD5鐮� + */ + public static String getStringMd5(String text) { + StringBuilder builder = new StringBuilder(); + try { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + + byte[] bytes = md5.digest(text.getBytes(StandardCharsets.UTF_8)); + for (byte aByte : bytes) { + builder.append(Integer.toHexString((0x000000FF & aByte) | 0xFFFFFF00).substring(6)); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + + return builder.toString(); + } + + /** + * 鏍规嵁璺緞鑾峰彇鏂囦欢 + */ + public static void getFilesByPath(List<String> list, String path) { + File file = new File(path); + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (null == files) { + return; + } + + for (File f : files) { + if (f.isDirectory()) { + getFilesByPath(list, f.getPath()); + } else { + list.add(f.getPath()); + } + } + } else { + list.add(file.getPath()); + } + } + + /** + * 澶嶅埗鏂囦欢 + * + * @param src 婧愭枃浠� + * @param dest 鐩綍鏂囦欢 + */ + public static void copyFile(File src, File dest) throws IOException { + InputStream is = null; + OutputStream os = null; + try { + is = new FileInputStream(src); + os = new FileOutputStream(dest); + + byte[] buffer = new byte[1024]; + + int length; + while ((length = is.read(buffer)) > 0) { + os.write(buffer, 0, length); + } + } finally { + os.close(); + is.close(); + } + } +} diff --git a/src/main/java/com/se/simu/helper/GdalHelper.java b/src/main/java/com/se/simu/helper/GdalHelper.java new file mode 100644 index 0000000..451314d --- /dev/null +++ b/src/main/java/com/se/simu/helper/GdalHelper.java @@ -0,0 +1,116 @@ +package com.se.simu.helper; + +import lombok.extern.slf4j.Slf4j; +import org.gdal.gdal.Band; +import org.gdal.gdal.Dataset; +import org.gdal.gdal.gdal; +import org.gdal.gdalconst.gdalconst; +import org.gdal.ogr.ogr; +import org.gdal.osr.SpatialReference; +import org.gdal.osr.osr; + +import java.io.File; + +/** + * GDAL甯姪绫� + * + * @author WWW + * @date 2024-09-12 + */ +@Slf4j +@SuppressWarnings("ALL") +public class GdalHelper { + public final static int I4326 = 4326; + + public final static int I4490 = 4490; + + public static SpatialReference SR4326; + + public static SpatialReference SR4490; + + public final static String CGCS2000 = "CGCS2000"; + + /** + * 鍒濆鍖� + */ + public static void init(String gdalPath) { + // 閰嶇疆鐜鍙橀噺 + if (!StringHelper.isEmpty(gdalPath)) { + if (WebHelper.isWin()) { + gdal.SetConfigOption("GDAL_DATA", gdalPath + "/gdal-data"); + gdal.SetConfigOption("PROJ_LIB", gdalPath + "/proj7/share"); + //System.setProperty("PROJ_LIB", gdalPath + "/proj7/share") + gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + "/gdalplugins"); + + String path = System.getenv("PATH"); + if (!path.contains(gdalPath)) { + System.setProperty("PATH", path + ";" + gdalPath); + } + } else { + //System.setProperty("java.library.path", gdalPath); + } + } + + // 鏀寔涓枃璺緞 + gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); + // 灞炴�ц〃鏀寔涓枃锛欳P936 + gdal.SetConfigOption("SHAPE_ENCODING", ""); + gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); + gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); + + // 娉ㄥ唽鎵�鏈夌殑椹卞姩 + gdal.AllRegister(); + ogr.RegisterAll(); + initSr(); + } + + /** + * 鍒濆鍖栧潗鏍囩郴 + * + * https://blog.csdn.net/CallmeAdo/article/details/127558139 + */ + public static void initSr() { + try { + SR4326 = new SpatialReference(); + SR4326.ImportFromEPSG(I4326); + // 瀵逛簬lat/long椤哄簭鐨勫湴鐞咰RS锛屾暟鎹粛鐒舵槸long/lat椤哄簭鐨� + SR4326.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); + + SR4490 = new SpatialReference(); + SR4490.ImportFromEPSG(I4490); + SR4490.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + /** + * 鍒涘缓閲戝瓧濉� + */ + public static void createPyramid(String file) { + Dataset ds = null; + try { + File f = new File(file); + if (!f.exists() || f.isDirectory()) { + return; + } + + ds = gdal.Open(file, gdalconst.GA_ReadOnly); + if (null == ds || ds.getRasterCount() < 1 || null == ds.GetSpatialRef()) { + return; + } + + // 鍒涘缓閲戝瓧濉� + Band band = ds.GetRasterBand(1); + if (0 == band.GetOverviewCount()) { + ds.BuildOverviews("nearest", new int[]{2, 4, 6, 8, 16}, null); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } finally { + if (null != ds) { + ds.delete(); + } + } + } +} diff --git a/src/main/java/com/se/simu/helper/StringHelper.java b/src/main/java/com/se/simu/helper/StringHelper.java new file mode 100644 index 0000000..067ae87 --- /dev/null +++ b/src/main/java/com/se/simu/helper/StringHelper.java @@ -0,0 +1,253 @@ +package com.se.simu.helper; + +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 瀛楃涓插府鍔╃被 + * + * @author WWW + * @date 2024-09-12 + */ +public class StringHelper { + public final static String COMMA = ","; + + public final static String PWD_REG = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W!@#$%^&*`~()\\-_+=,.?;<>]+$)(?![a-z0-9]+$)(?![a-z\\W!@#$%^&*`~()\\-_+=,.?;<>]+$)(?![0-9\\W!@#$%^&*`~()\\-_+=,.?;<>]+$)[a-zA-Z0-9\\W!@#$%^&*`~()\\-_+=,.?;<>]{12,20}$"; + + /** + * 鏁板瓧姝e垯 + */ + public static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?"); + + /** + * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 1 + */ + public static final SimpleDateFormat YMD_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + /** + * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 2 + */ + public static final SimpleDateFormat YMDHMS_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + /** + * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 3 + */ + public static final SimpleDateFormat YMD2_FORMAT = new SimpleDateFormat("yyyyMMdd"); + + /** + * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 4 + */ + public static final SimpleDateFormat YMDHMS2_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss"); + + /** + * 鍒ゆ柇瀛楃涓�,鏄惁涓烘暣鏁� + */ + public static boolean isInteger(String str) { + return str != null && str.matches("[0-9]+"); + } + + /** + * 鍒ゆ柇瀛楃涓�,鏄惁涓烘诞鐐规暟 + */ + public static boolean isNumeric(String str) { + return str != null && str.matches("-?\\d+(\\.\\d+)?"); + } + + /** + * 鍒ゆ柇瀛楃涓�,鏄惁涓烘诞鐐规暟 + */ + public static boolean isNumeric2(String str) { + return str != null && NUMBER_PATTERN.matcher(str).matches(); + } + + /** + * 鏃ユ湡姝e垯 + */ + public static Pattern datePattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/]((((0?[13578])|(1[02]))[\\-\\/]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/]((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/]((((0?[13578])|(1[02]))[\\-\\/]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/]((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"); + + /** + * SQL姝e垯 + */ + public static Pattern sqlPattern = Pattern.compile("|and|exec|execute|insert|select|delete|update|count|drop|\\*|%|chr|mid|master|truncate|char|declare|sitename|net user|xp_cmdshell|;|or|-|\\+|,|like"); + + /** + * 瀛楃涓茶浆涓烘棩鏈� + */ + public static Date parseDate(String str) { + try { + return YMD_FORMAT.parse(str); + } catch (Exception ex) { + return null; + } + } + + /** + * 瀛楃涓茶浆涓烘棩鏈熸椂闂� + */ + public static Date parseTime(String str) { + try { + return YMDHMS_FORMAT.parse(str); + } catch (Exception e) { + return null; + } + } + + /** + * 鍒ゆ柇鍊兼槸鍚︿负鏃ユ湡鏍煎紡 + */ + public static boolean isDate(String strDate) { + Matcher m = datePattern.matcher(strDate); + + return m.matches(); + } + + /** + * 瀛楃涓诧紝鏄惁涓簄ull 鎴� "" + */ + public static boolean isNull(String str) { + return null == str || str.length() == 0; + } + + /** + * 瀛楃涓�,鏄惁涓虹┖null鍜岀┖鏍� + */ + public static boolean isEmpty(String str) { + return null == str || "".equals(str.trim()); + } + + /** + * 鑾峰彇 like 瀛楃涓� + */ + public static String getLikeStr(String str) { + return isEmpty(str) ? null : "%" + str.trim() + "%"; + } + + /** + * 鑾峰彇 like 瀛楃涓� + */ + public static String getLikeUpperStr(String str) { + return isEmpty(str) ? null : "%" + str.trim().toUpperCase() + "%"; + } + + /** + * 鑾峰彇 鍙砽ike 瀛楃涓� + */ + public static String getRightLike(String str) { + return isEmpty(str) ? null : str.trim() + "%"; + } + + /** + * 鑾峰彇鍥惧舰鐨刉KT瀛楃涓� + */ + public static String getGeomWkt(String wkt) { + if (isEmpty(wkt)) { + return "null"; + } + + return String.format("ST_GeomFromText('%s')", wkt); + } + + /** + * 棣栧瓧姣嶅ぇ鍐� + */ + public static String firstCharToUpperCase(String str) { + return str.substring(0, 1).toUpperCase() + str.substring(1); + } + + /** + * 棣栧瓧姣嶅皬鍐� + */ + public static String firstCharToLowerCase(String str) { + return str.substring(0, 1).toLowerCase() + str.substring(1); + } + + /** + * 鍒ゆ柇鍊兼槸鍚﹀瓨鍦⊿QL娉ㄥ叆 + * + * @param str 瀛楃涓� + * @return 鏄�/鍚� + */ + public static boolean isSqlInjection(String str) { + if (null == str) { + return false; + } + + Matcher m = sqlPattern.matcher(str); + + return m.matches(); + } + + /** + * 鏍¢獙瀵嗙爜鏄�/鍚﹀悎娉� + * + * @param pwd 瀵嗙爜 + * @return 鏄�/鍚︿负鏃犳晥鐨� + */ + public static boolean isPwdInvalid(String pwd) { + return !Pattern.matches(PWD_REG, pwd); + } + + /** + * 鑾峰彇GUID + */ + public static String getGuid() { + return UUID.randomUUID().toString(); + } + + /** + * 鑾峰彇鍒嗛挓宸暟 + */ + public static long getMinuteDifference(Timestamp ts) { + return (ts.getTime() - System.currentTimeMillis()) / 1000 / 60; + } + + /** + * 杩炴帴List闆嗗悎 + * + * @param list list 鏁存暟闆嗗悎 + * @param join join 杩炴帴瀛楃 + * @param <T> 娉涘瀷绫� + * @return 瀛楃涓� + */ + public static <T> String join(List<T> list, String join) { + if (null == list || list.isEmpty()) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + for (T t : list) { + if (null != t) { + sb.append(t.toString()).append(join); + } + } + + if (sb.length() > 0 && sb.lastIndexOf(join) == sb.length() - join.length()) { + // 鍒犻櫎浠庣储寮� start 寮�濮嬪埌 end 涔嬮棿鐨勫瓧绗︼紝鍗� 鍓嶅寘鎷� 鍚庝笉鍖呮嫭銆� + sb.delete(sb.length() - join.length(), sb.length()); + } + + return sb.toString(); + } + + /** + * 瀛楃涓茶浆鏁存暟闆嗗悎 + */ + public static List<Integer> strToIntegers(String str) { + if (isEmpty(str)) { + return null; + } + + List<Integer> list = new ArrayList<>(); + for (String s : str.split(COMMA)) { + list.add(Integer.parseInt(s)); + } + + return list; + } +} diff --git a/src/main/java/com/se/simu/helper/WebHelper.java b/src/main/java/com/se/simu/helper/WebHelper.java index 099878d..dd1d36a 100644 --- a/src/main/java/com/se/simu/helper/WebHelper.java +++ b/src/main/java/com/se/simu/helper/WebHelper.java @@ -31,6 +31,12 @@ private final static String UNKNOWN = "unknown"; + public static boolean isWin() { + String osName = System.getProperty("os.name"); + + return osName.startsWith("Windows"); + } + /** * 鏍煎紡鍖栨棩鏈� */ diff --git a/src/main/java/com/se/simu/service/SedbService.java b/src/main/java/com/se/simu/service/SedbService.java new file mode 100644 index 0000000..1a6a556 --- /dev/null +++ b/src/main/java/com/se/simu/service/SedbService.java @@ -0,0 +1,48 @@ +package com.se.simu.service; + +import cn.hutool.json.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import javax.annotation.Resource; + +/** + * SDDB鏈嶅姟绫� + * + * @author WWW + * @date 2024-09-12 + */ +@Slf4j +@Service +public class SedbService { + @Value("${sedb.url}") + String url; + + @Value("${sedb.user}") + String user; + + @Value("${sedb.pwd}") + String pwd; + + @Resource + RestTemplate restTemplate; + + public String getToken() { + //http://106.120.22.26:8013/account-service/security/publickey + String key = getPublicKey(); + + + return key; + } + + public String getPublicKey() { + String uri = url + "account-service/security/publickey"; + + //{"datetime":"2024-09-12 17:24:38","code":200,"data":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtFwJCh2taVTEi05C8qT2oG7Y+rDmJhlO4zicpSeRtiro9LsytePeWI7BXM6sfDU0WeKun1izawcfgGkZgnoJuMBluAOKI1tL0uCrR+DreNLqMVtnXHwoWEIk/hGJedDWaf3q22aGDyEB5h9qCq0JklSShP1Ih4ppap4LmgxdTPQIDAQAB"} + JSONObject obj = restTemplate.getForObject(uri, JSONObject.class); + + return obj.getStr("data"); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5626f6a..4568929 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,7 +10,30 @@ application: name: SimuServer +mybatis-plus: + type-aliases-package: com.se.ai.domain + config-location: classpath:mybatis.xml + mapper-locations: classpath:mapper/**/*.xml + +pagehelper: + autoDialect: true + autoRuntimeDialect: true + reasonable: true + supportMethodsArguments: true + params: count=countSql + +remote: + maxTotalConnect: 0 + maxConnectPerRoute: 1000 + connectTimeout: -1 + readTimeout: -1 + sys: ver: 0.1 path: - data: D:\simu \ No newline at end of file + data: D:\simu + +sedb: + url: http://106.120.22.26:8013/ + user: WUWEIWEI + pwd: WUWEIWEI -- Gitblit v1.9.3