mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 10:57:15 +08:00 
			
		
		
		
	feat: 新增常用工具类
This commit is contained in:
		| @@ -0,0 +1,79 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
|  | ||||
| package top.charles7c.continew.starter.core.util; | ||||
|  | ||||
| import cn.hutool.core.date.DatePattern; | ||||
| import cn.hutool.core.date.DateUtil; | ||||
| import cn.hutool.core.io.file.FileNameUtil; | ||||
| import cn.hutool.core.util.IdUtil; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.web.multipart.MultipartFile; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| /** | ||||
|  * 文件上传工具类 | ||||
|  * | ||||
|  * @author Zheng Jie(<a href="https://gitee.com/elunez/eladmin">ELADMIN</a>) | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| @Slf4j | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class FileUploadUtils { | ||||
|  | ||||
|     /** | ||||
|      * 上传文件 | ||||
|      * | ||||
|      * @param multipartFile          源文件对象 | ||||
|      * @param filePath               文件路径 | ||||
|      * @param isKeepOriginalFilename 是否保留原文件名 | ||||
|      * @return 目标文件对象 | ||||
|      */ | ||||
|     public static File upload(MultipartFile multipartFile, String filePath, boolean isKeepOriginalFilename) { | ||||
|         String originalFilename = multipartFile.getOriginalFilename(); | ||||
|         String extensionName = FileNameUtil.extName(originalFilename); | ||||
|  | ||||
|         String fileName; | ||||
|         if (isKeepOriginalFilename) { | ||||
|             fileName = String.format("%s-%s.%s", FileNameUtil.getPrefix(originalFilename), | ||||
|                     DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN), extensionName); | ||||
|         } else { | ||||
|             fileName = String.format("%s.%s", IdUtil.fastSimpleUUID(), extensionName); | ||||
|         } | ||||
|  | ||||
|         try { | ||||
|             String pathname = filePath + fileName; | ||||
|             File dest = new File(pathname).getCanonicalFile(); | ||||
|             // 如果父路径不存在,自动创建 | ||||
|             if (!dest.getParentFile().exists()) { | ||||
|                 if (!dest.getParentFile().mkdirs()) { | ||||
|                     log.error("Create upload file parent path failed."); | ||||
|                 } | ||||
|             } | ||||
|             // 文件写入 | ||||
|             multipartFile.transferTo(dest); | ||||
|             return dest; | ||||
|         } catch (Exception e) { | ||||
|             log.error("Upload file occurred an error: {}. fileName: {}.", e.getMessage(), fileName, e); | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,74 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
|  | ||||
| package top.charles7c.continew.starter.core.util; | ||||
|  | ||||
| import cn.hutool.http.useragent.UserAgent; | ||||
| import cn.hutool.http.useragent.UserAgentUtil; | ||||
| import jakarta.servlet.http.HttpServletRequest; | ||||
| import jakarta.servlet.http.HttpServletResponse; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
| import org.springframework.web.context.request.RequestContextHolder; | ||||
| import org.springframework.web.context.request.ServletRequestAttributes; | ||||
|  | ||||
| import java.util.Objects; | ||||
|  | ||||
| /** | ||||
|  * Servlet 工具类 | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class ServletUtils { | ||||
|  | ||||
|     /** | ||||
|      * 获取请求对象 | ||||
|      * | ||||
|      * @return / | ||||
|      */ | ||||
|     public static HttpServletRequest getRequest() { | ||||
|         return getServletRequestAttributes().getRequest(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取响应对象 | ||||
|      * | ||||
|      * @return / | ||||
|      */ | ||||
|     public static HttpServletResponse getResponse() { | ||||
|         return getServletRequestAttributes().getResponse(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取浏览器及其版本信息 | ||||
|      * | ||||
|      * @param request 请求对象 | ||||
|      * @return 浏览器及其版本信息 | ||||
|      */ | ||||
|     public static String getBrowser(HttpServletRequest request) { | ||||
|         if (null == request) { | ||||
|             return null; | ||||
|         } | ||||
|         UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent")); | ||||
|         return userAgent.getBrowser().getName() + " " + userAgent.getVersion(); | ||||
|     } | ||||
|  | ||||
|     private static ServletRequestAttributes getServletRequestAttributes() { | ||||
|         return (ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,64 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
|  | ||||
| package top.charles7c.continew.starter.core.util; | ||||
|  | ||||
| import cn.hutool.extra.template.Template; | ||||
| import cn.hutool.extra.template.TemplateConfig; | ||||
| import cn.hutool.extra.template.TemplateEngine; | ||||
| import cn.hutool.extra.template.TemplateUtil; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
|  | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * 模板工具类 | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class TemplateUtils { | ||||
|  | ||||
|     private static final String DEFAULT_TEMPLATE_PARENT_PATH = "templates"; | ||||
|  | ||||
|     /** | ||||
|      * 渲染模板 | ||||
|      * | ||||
|      * @param templatePath 模板路径 | ||||
|      * @param bindingMap   绑定参数(此 Map 中的参数会替换模板中的变量) | ||||
|      * @return 渲染后的内容 | ||||
|      */ | ||||
|     public static String render(String templatePath, Map<?, ?> bindingMap) { | ||||
|         return render(DEFAULT_TEMPLATE_PARENT_PATH, templatePath, bindingMap); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 渲染模板 | ||||
|      * | ||||
|      * @param parentPath   模板父目录 | ||||
|      * @param templatePath 模板路径 | ||||
|      * @param bindingMap   绑定参数(此 Map 中的参数会替换模板中的变量) | ||||
|      * @return 渲染后的内容 | ||||
|      */ | ||||
|     public static String render(String parentPath, String templatePath, Map<?, ?> bindingMap) { | ||||
|         TemplateEngine engine = | ||||
|                 TemplateUtil.createEngine(new TemplateConfig(parentPath, TemplateConfig.ResourceMode.CLASSPATH)); | ||||
|         Template template = engine.getTemplate(templatePath); | ||||
|         return template.render(bindingMap); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,41 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
|  | ||||
| package top.charles7c.continew.starter.core.util; | ||||
|  | ||||
| import cn.hutool.http.HttpUtil; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
|  | ||||
| /** | ||||
|  * URL(Uniform Resource Locator)统一资源定位符相关工具类 | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class URLUtils { | ||||
|  | ||||
|     /** | ||||
|      * 提供的 URL 是否为 HTTP URL(协议包括:"http","https") | ||||
|      * | ||||
|      * @param url URL | ||||
|      * @return 是否为 HTTP URL | ||||
|      */ | ||||
|     public static boolean isHttpUrl(String url) { | ||||
|         return HttpUtil.isHttp(url) || HttpUtil.isHttps(url); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,93 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
|  | ||||
| package top.charles7c.continew.starter.core.util.db; | ||||
|  | ||||
| import cn.hutool.core.date.DateUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import cn.hutool.db.Db; | ||||
| import cn.hutool.db.Entity; | ||||
| import cn.hutool.db.meta.Column; | ||||
| import cn.hutool.db.meta.MetaUtil; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
|  | ||||
| import javax.sql.DataSource; | ||||
| import java.sql.SQLException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collection; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * 数据库元数据信息工具类 | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class MetaUtils { | ||||
|  | ||||
|     /** | ||||
|      * 获取所有表信息 | ||||
|      * | ||||
|      * @param dataSource 数据源 | ||||
|      * @return 表信息列表 | ||||
|      */ | ||||
|     public static List<Table> getTables(DataSource dataSource) throws SQLException { | ||||
|         return getTables(dataSource, null); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取所有表信息 | ||||
|      * | ||||
|      * @param dataSource 数据源 | ||||
|      * @param tableName  表名称 | ||||
|      * @return 表信息列表 | ||||
|      */ | ||||
|     public static List<Table> getTables(DataSource dataSource, String tableName) throws SQLException { | ||||
|         String querySql = "SHOW TABLE STATUS"; | ||||
|         List<Entity> tableEntityList; | ||||
|         Db db = Db.use(dataSource); | ||||
|         if (StrUtil.isNotBlank(tableName)) { | ||||
|             tableEntityList = db.query(String.format("%s WHERE NAME = ?", querySql), tableName); | ||||
|         } else { | ||||
|             tableEntityList = db.query(querySql); | ||||
|         } | ||||
|         List<Table> tableList = new ArrayList<>(tableEntityList.size()); | ||||
|         for (Entity tableEntity : tableEntityList) { | ||||
|             Table table = new Table(tableEntity.getStr("NAME")); | ||||
|             table.setComment(tableEntity.getStr("COMMENT")); | ||||
|             table.setEngine(tableEntity.getStr("ENGINE")); | ||||
|             table.setCharset(tableEntity.getStr("COLLATION")); | ||||
|             table.setCreateTime(DateUtil.toLocalDateTime(tableEntity.getDate("CREATE_TIME"))); | ||||
|             table.setUpdateTime(DateUtil.toLocalDateTime(tableEntity.getDate("UPDATE_TIME"))); | ||||
|             tableList.add(table); | ||||
|         } | ||||
|         return tableList; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取所有列信息 | ||||
|      * | ||||
|      * @param dataSource 数据源 | ||||
|      * @param tableName  表名称 | ||||
|      * @return 列信息列表 | ||||
|      */ | ||||
|     public static Collection<Column> getColumns(DataSource dataSource, String tableName) { | ||||
|         cn.hutool.db.meta.Table table = MetaUtil.getTableMeta(dataSource, tableName); | ||||
|         return table.getColumns(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,72 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
|  | ||||
| package top.charles7c.continew.starter.core.util.db; | ||||
|  | ||||
| import lombok.Getter; | ||||
| import lombok.Setter; | ||||
|  | ||||
| import java.io.Serial; | ||||
| import java.io.Serializable; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| /** | ||||
|  * 数据库表信息 | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| @Getter | ||||
| @Setter | ||||
| public class Table implements Serializable { | ||||
|  | ||||
|     @Serial | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** | ||||
|      * 表名称 | ||||
|      */ | ||||
|     private String tableName; | ||||
|  | ||||
|     /** | ||||
|      * 注释 | ||||
|      */ | ||||
|     private String comment; | ||||
|  | ||||
|     /** | ||||
|      * 存储引擎 | ||||
|      */ | ||||
|     private String engine; | ||||
|  | ||||
|     /** | ||||
|      * 字符集 | ||||
|      */ | ||||
|     private String charset; | ||||
|  | ||||
|     /** | ||||
|      * 创建时间 | ||||
|      */ | ||||
|     private LocalDateTime createTime; | ||||
|  | ||||
|     /** | ||||
|      * 修改时间 | ||||
|      */ | ||||
|     private LocalDateTime updateTime; | ||||
|  | ||||
|     public Table(String tableName) { | ||||
|         this.tableName = tableName; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user