From e310ec109ca26173730587e5ff1d16f736f73f52 Mon Sep 17 00:00:00 2001 From: zhuo <2089219845@qq.com> Date: 星期四, 13 二月 2025 11:59:24 +0800 Subject: [PATCH] 添加mybatis-plus和minio --- /dev/null | 0 ruoyi-admin/src/main/java/com/ruoyi/CodeGenerator.java | 104 ++++++++++ ruoyi-admin/src/main/resources/application-druid.yml | 11 + ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java | 16 + ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java | 9 ruoyi-common/src/main/java/com/ruoyi/common/core/domain/MinioResult.java | 16 + ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtils.java | 294 +++++++++++++++++++++++++++++ ruoyi-common/src/main/java/com/ruoyi/common/config/MinioConfig.java | 27 ++ pom.xml | 85 ++++++++ ruoyi-admin/src/main/resources/application.yml | 16 + ruoyi-common/pom.xml | 22 + 11 files changed, 578 insertions(+), 22 deletions(-) diff --git "a/doc/\350\213\245\344\276\235\347\216\257\345\242\203\344\275\277\347\224\250\346\211\213\345\206\214.docx" "b/doc/\350\213\245\344\276\235\347\216\257\345\242\203\344\275\277\347\224\250\346\211\213\345\206\214.docx" deleted file mode 100644 index 9e4daef..0000000 --- "a/doc/\350\213\245\344\276\235\347\216\257\345\242\203\344\275\277\347\224\250\346\211\213\345\206\214.docx" +++ /dev/null Binary files differ diff --git a/pom.xml b/pom.xml index 1a1454f..1e1daae 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> - + <groupId>com.ruoyi</groupId> <artifactId>ruoyi</artifactId> <version>3.8.9</version> @@ -11,7 +11,7 @@ <name>ruoyi</name> <url>http://www.ruoyi.vip</url> <description>鑻ヤ緷绠$悊绯荤粺</description> - + <properties> <ruoyi.version>3.8.9</ruoyi.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> @@ -35,6 +35,13 @@ <logback.version>1.2.13</logback.version> <spring-security.version>5.7.12</spring-security.version> <spring-framework.version>5.3.39</spring-framework.version> + <mybatis-plus.version>3.5.3.1</mybatis-plus.version> + <freemarker.version>2.3.30</freemarker.version> + <knife4j-spring-ui.version>3.0.3</knife4j-spring-ui.version> + <core.version>3.4.1</core.version> + <javase.version>3.4.1</javase.version> + <minio.version>8.4.3</minio.version> + <okhttp.version>4.9.0</okhttp.version> </properties> <!-- 渚濊禆澹版槑 --> @@ -218,8 +225,80 @@ <version>${ruoyi.version}</version> </dependency> + <!-- minio --> + <dependency> + <groupId>io.minio</groupId> + <artifactId>minio</artifactId> + <version>${minio.version}</version> + <exclusions> + <exclusion> + <groupId>com.squareup.okhttp3</groupId> + <artifactId>okhttp</artifactId> + </exclusion> + </exclusions> + </dependency> + + <!-- minio渚濊禆okhttp 涓嶇劧鎶ラ敊 --> + <dependency> + <groupId>com.squareup.okhttp3</groupId> + <artifactId>okhttp</artifactId> + <version>${okhttp.version}</version> + </dependency> </dependencies> </dependencyManagement> + + <!-- 鍏ㄥ眬寮曞叆 --> + <dependencies> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <scope>provided</scope> + </dependency> + + <!--mybatis-plus--> + <dependency> + <groupId>com.baomidou</groupId> + <artifactId>mybatis-plus-boot-starter</artifactId> + <version>${mybatis-plus.version}</version> + </dependency> + + <!--mybatis-plus浠g爜鐢熸垚鍣�--> + <dependency> + <groupId>com.baomidou</groupId> + <artifactId>mybatis-plus-generator</artifactId> + <version>${mybatis-plus.version}</version> + </dependency> + + <dependency> + <groupId>org.freemarker</groupId> + <artifactId>freemarker</artifactId> + <version>${freemarker.version}</version> + </dependency> + + <!-- Swagger3渚濊禆 --> + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-boot-starter</artifactId> + <version>${swagger.version}</version> + </dependency> + + <!--Swagger3-Ui缇庡寲--> + <dependency> + <groupId>com.github.xiaoymin</groupId> + <artifactId>knife4j-spring-ui</artifactId> + <version>${knife4j-spring-ui.version}</version> + </dependency> + <dependency> + <groupId>com.google.zxing</groupId> + <artifactId>core</artifactId> + <version>${core.version}</version> <!-- 鎴栬�呮洿楂樼増鏈� --> + </dependency> + <dependency> + <groupId>com.google.zxing</groupId> + <artifactId>javase</artifactId> + <version>${javase.version}</version> <!-- 鎴栬�呮洿楂樼増鏈� --> + </dependency> + </dependencies> <modules> <module>ruoyi-admin</module> @@ -271,4 +350,4 @@ </pluginRepository> </pluginRepositories> -</project> \ No newline at end of file +</project> diff --git a/ruoyi-admin/src/main/java/com/ruoyi/CodeGenerator.java b/ruoyi-admin/src/main/java/com/ruoyi/CodeGenerator.java new file mode 100644 index 0000000..c78cf33 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/CodeGenerator.java @@ -0,0 +1,104 @@ +package com.ruoyi; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; +import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; +import com.baomidou.mybatisplus.generator.fill.Column; + +import java.util.Collections; +import java.util.Scanner; + +// 婕旂ず渚嬪瓙锛屾墽琛� main 鏂规硶鎺у埗鍙拌緭鍏ユā鍧楄〃鍚嶅洖杞﹁嚜鍔ㄧ敓鎴愬搴旈」鐩洰褰曚腑 +public class CodeGenerator { + + public static String database_url = "jdbc:mysql://114.132.189.42:9004/lims-zb"; + public static String database_username = "root"; + public static String database_password= "123456"; + public static String author = "鑺杞欢锛堟睙鑻忥級鏈夐檺鍏徃"; + public static String model_name = "/mes-technical"; // 濡傛灉涓哄垎甯冨紡濉瓙妯″潡鍚嶇О锛屽鏋滀笉鏄垎甯冨紡涓虹┖鍗冲彲 + public static String setParent = "com.ruoyi.technical"; // 鍖呰矾寰� + public static String tablePrefix = "technical"; // 璁剧疆杩囨护琛ㄥ墠缂� + public static void main(String[] args) { + String projectPath = System.getProperty("user.dir"); + FastAutoGenerator.create(database_url, database_username, database_password) + // 鍏ㄥ眬閰嶇疆 + .globalConfig(builder -> { + builder.author(author) // 璁剧疆浣滆�� + .commentDate("yyyy-MM-dd hh:mm:ss") //娉ㄩ噴鏃ユ湡 + .outputDir(projectPath + model_name + "/src/main/java") // 鎸囧畾杈撳嚭鐩綍 + .disableOpenDir() //绂佹鎵撳紑杈撳嚭鐩綍锛岄粯璁ゆ墦寮� + .enableSwagger() // 寮�鍚痵wagger + + ; + }) + // 鍖呴厤缃� + .packageConfig(builder -> { + builder.entity("pojo"); + builder.parent(setParent) // 璁剧疆鐖跺寘鍚� + .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + model_name + "/src/main/resources/mapper")); // 璁剧疆mapperXml鐢熸垚璺緞 + }) + // 绛栫暐閰嶇疆 + .strategyConfig(builder -> { + builder.addInclude(scanner("琛ㄥ悕锛屽涓┖鏍煎垎鍓�").split(" ")) // 璁剧疆闇�瑕佺敓鎴愮殑琛ㄥ悕 + .addTablePrefix(tablePrefix) // 璁剧疆杩囨护琛ㄥ墠缂� + // Entity 绛栫暐閰嶇疆 + .entityBuilder() + .enableLombok() //寮�鍚� Lombok + .naming(NamingStrategy.underline_to_camel) //鏁版嵁搴撹〃鏄犲皠鍒板疄浣撶殑鍛藉悕绛栫暐锛氫笅鍒掔嚎杞┘宄板懡 + .columnNaming(NamingStrategy.underline_to_camel) //鏁版嵁搴撹〃瀛楁鏄犲皠鍒板疄浣撶殑鍛藉悕绛栫暐锛氫笅鍒掔嚎杞┘宄板懡 + .enableFileOverride() // 瑕嗙洊宸茬粡鐢熸垚鐨凟ntity鏂囦欢 + .logicDeleteColumnName("state") + .addTableFills( + new Column("create_time", FieldFill.INSERT), + new Column("update_time", FieldFill.INSERT_UPDATE), + new Column("create_user", FieldFill.INSERT), + new Column("update_user", FieldFill.INSERT_UPDATE) + ) + .idType(IdType.AUTO) // 鑷涓婚敭 + + // Mapper 绛栫暐閰嶇疆 + .mapperBuilder() + .enableFileOverride() // 瑕嗙洊宸茬敓鎴怣apper鏂囦欢 + .enableBaseResultMap() // 鑷姩鐢熸垚resultMap + // Service 绛栫暐閰嶇疆 + .serviceBuilder() + .formatServiceFileName("%sService") //鏍煎紡鍖� service 鎺ュ彛鏂囦欢鍚嶇О锛�%s杩涜鍖归厤琛ㄥ悕锛屽 UserService + .formatServiceImplFileName("%sServiceImpl") //鏍煎紡鍖� service 瀹炵幇绫绘枃浠跺悕绉帮紝%s杩涜鍖归厤琛ㄥ悕锛屽 UserServiceImpl + .enableFileOverride() // 瑕嗙洊宸茬敓鎴愭枃浠� + + // Controller 绛栫暐閰嶇疆 + .controllerBuilder() + .enableFileOverride() // 瑕嗙洊宸茬敓鎴愭枃浠� + .enableRestStyle() // 寮�鍚敓鎴怈RestController 鎺у埗鍣� + ; + }) + //5銆佹ā鏉垮紩鎿� + .templateEngine(new FreemarkerTemplateEngine()) //榛樿 + .execute(); + } + + /** + * <p> + * 璇诲彇鎺у埗鍙板唴瀹� + * </p> + */ + public static String scanner(String tip) { + Scanner scanner = new Scanner(System.in); + StringBuilder help = new StringBuilder(); + help.append("璇疯緭鍏�" + tip + "锛�"); + System.out.println(help.toString()); + if (scanner.hasNext()) { + String ipt = scanner.next(); + + if (StringUtils.isNotBlank(ipt)) { + return ipt; + } + } + throw new MybatisPlusException("璇疯緭鍏ユ纭殑" + tip + "锛�"); + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java index ae1c3ec..3822cfd 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SwaggerConfig.java @@ -24,7 +24,7 @@ /** * Swagger2鐨勬帴鍙i厤缃� - * + * * @author ruoyi */ @Configuration @@ -37,10 +37,6 @@ /** 鏄惁寮�鍚痵wagger */ @Value("${swagger.enabled}") private boolean enabled; - - /** 璁剧疆璇锋眰鐨勭粺涓�鍓嶇紑 */ - @Value("${swagger.pathMapping}") - private String pathMapping; /** * 鍒涘缓API @@ -64,8 +60,7 @@ .build() /* 璁剧疆瀹夊叏妯″紡锛宻wagger鍙互璁剧疆璁块棶token */ .securitySchemes(securitySchemes()) - .securityContexts(securityContexts()) - .pathMapping(pathMapping); + .securityContexts(securityContexts()); } /** diff --git a/ruoyi-admin/src/main/resources/application-druid.yml b/ruoyi-admin/src/main/resources/application-druid.yml index 06ecbeb..a77bf59 100644 --- a/ruoyi-admin/src/main/resources/application-druid.yml +++ b/ruoyi-admin/src/main/resources/application-druid.yml @@ -6,7 +6,7 @@ druid: # 涓诲簱鏁版嵁婧� master: - url: jdbc:mysql://localhost:3306/lims-zb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + url: jdbc:mysql://114.132.189.42:9004/lims-ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: 123456 # 浠庡簱鏁版嵁婧� @@ -59,3 +59,12 @@ wall: config: multi-statement-allow: true + + +minio: + endpoint: http://114.132.189.42/ + port: 7019 + secure: false + accessKey: admin + secretKey: 12345678 + preview-expiry: 24 # 棰勮鍦板潃榛樿24灏忔椂 diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index cb2179d..f756298 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -116,8 +116,6 @@ swagger: # 鏄惁寮�鍚痵wagger enabled: true - # 璇锋眰鍓嶇紑 - pathMapping: /dev-api # 闃叉XSS鏀诲嚮 xss: @@ -127,3 +125,17 @@ excludes: /system/notice # 鍖归厤閾炬帴 urlPatterns: /system/*,/monitor/*,/tool/* + +# mybatis-plus閰嶇疆 +mybatis-plus: + mapper-locations: classpath*:mapper/**/*Mapper.xml + type-aliases-package: com.ruoyi.**.domain + global-config: + banner: off + db-config: + id-type: auto + configuration: + map-underscore-to-camel-case: true + cache-enabled: false + log-impl: # org.apache.ibatis.logging.stdout.StdOutImpl + diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 6f9c726..a6fe9b7 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -52,13 +52,13 @@ <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> - + <!-- JSON宸ュ叿绫� --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> - + <!-- 闃块噷JSON瑙f瀽鍣� --> <dependency> <groupId>com.alibaba.fastjson2</groupId> @@ -119,6 +119,22 @@ <artifactId>javax.servlet-api</artifactId> </dependency> + <!-- minio --> + <dependency> + <groupId>io.minio</groupId> + <artifactId>minio</artifactId> + </dependency> + + <!-- minio渚濊禆okhttp 涓嶇劧鎶ラ敊 --> + <dependency> + <groupId>com.squareup.okhttp3</groupId> + <artifactId>okhttp</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + </dependency> </dependencies> -</project> \ No newline at end of file +</project> diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/MinioConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/MinioConfig.java new file mode 100644 index 0000000..28f489f --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/MinioConfig.java @@ -0,0 +1,27 @@ +package com.ruoyi.common.config; + +import io.minio.MinioClient; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@Configuration +@Component +@ConfigurationProperties(prefix = "minio") +@Data +public class MinioConfig { + private String endpoint; + private int port; + private String accessKey; + private String secretKey; + private Boolean secure; + + @Bean + public MinioClient getMinioClient() { + return MinioClient.builder().endpoint(endpoint, port, secure) + .credentials(accessKey, secretKey) + .build(); + } +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/MinioResult.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/MinioResult.java new file mode 100644 index 0000000..07b7eef --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/MinioResult.java @@ -0,0 +1,16 @@ +package com.ruoyi.common.core.domain; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class MinioResult { + @ApiModelProperty("minio涓殑鏂囦欢鍚嶇О") + private String bucketFileName; + + @ApiModelProperty("婧愭枃浠跺悕绉�") + private String originalName; + + @ApiModelProperty("棰勮璺緞") + private String previewExpiry; +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtils.java new file mode 100644 index 0000000..25efe7c --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MinioUtils.java @@ -0,0 +1,294 @@ +package com.ruoyi.common.utils.file; + +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.ruoyi.common.core.domain.MinioResult; +import com.ruoyi.common.exception.UtilException; +import com.ruoyi.common.exception.file.InvalidExtensionException; +import io.minio.*; +import io.minio.http.Method; +import io.minio.messages.DeleteError; +import io.minio.messages.DeleteObject; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.util.FastByteArrayOutputStream; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +@Component +public class MinioUtils { + @Resource + private MinioClient minioClient; + + @Value("${minio.preview-expiry}") + private Integer previewExpiry; + + /** + * 鍒ゆ柇瀛樺偍妗舵槸鍚﹀瓨鍦紝涓嶅瓨鍦ㄥ垯鍒涘缓 + * + * @param bucketName 瀛樺偍妗跺悕绉� + */ + public void existBucket(String bucketName) { + try { + boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); + if (!exists) { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 鍒涘缓瀛樺偍妗� + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @return 鏄惁鍒涘缓鎴愬姛 + */ + public Boolean makeBucket(String bucketName) { + try { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * 鍒犻櫎瀛樺偍妗� + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @return 鏄惁鍒犻櫎鎴愬姛 + */ + public Boolean removeBucket(String bucketName) { + try { + minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * 鍒ゆ柇瀵硅薄鏄惁瀛樺湪 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param originalFileName MinIO涓瓨鍌ㄥ璞″叏璺緞 + * @return 瀵硅薄鏄惁瀛樺湪 + */ + public boolean existObject(String bucketName, String originalFileName) { + try { + minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(originalFileName).build()); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * 鏂囦欢涓婁紶 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param file 鏂囦欢 + * @return 妗朵腑浣嶇疆 + */ + public MinioResult upload(String bucketName, MultipartFile file, Boolean isPreviewExpiry) throws InvalidExtensionException { + MultipartFile[] fileArr = {file}; + List<MinioResult> fileNames = upload(bucketName, fileArr, isPreviewExpiry); + return fileNames.isEmpty() ? null : fileNames.get(0); + } + + /** + * 涓婁紶鏂囦欢 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param fileList 鏂囦欢鍒楄〃 + * @return 妗朵腑浣嶇疆鍒楄〃 + */ + public List<MinioResult> upload(String bucketName, List<MultipartFile> fileList, Boolean isPreviewExpiry) throws InvalidExtensionException { + MultipartFile[] fileArr = fileList.toArray(new MultipartFile[0]); + return upload(bucketName, fileArr, isPreviewExpiry); + } + + /** + * description: 涓婁紶鏂囦欢 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param fileArr 鏂囦欢鍒楄〃 + * @return 妗朵腑浣嶇疆鍒楄〃 + */ + public List<MinioResult> upload(String bucketName, MultipartFile[] fileArr, Boolean isPreviewExpiry) throws InvalidExtensionException { + for (MultipartFile file : fileArr) { + FileUploadUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); + } + // 淇濊瘉妗朵竴瀹氬瓨鍦� + existBucket(bucketName); + // 鎵ц姝e父鎿嶄綔 + List<MinioResult> bucketFileNames = new ArrayList<>(fileArr.length); + for (MultipartFile file : fileArr) { + // 鑾峰彇鍘熷鏂囦欢鍚嶇О + String originalFileName = file.getOriginalFilename(); + // 鑾峰彇褰撳墠鏃ユ湡锛屾牸寮忎緥濡傦細2020-11 + String datePath = new SimpleDateFormat("yyyy-MM").format(new Date()); + // 鏂囦欢鍚嶇О + String uuid = IdWorker.get32UUID(); + // 鑾峰彇鏂囦欢鍚庣紑 + String suffix = originalFileName.substring(originalFileName.lastIndexOf(".")); + String bucketFilePath = datePath + "/" + uuid + suffix; + + // 鎺ㄩ�佹枃浠跺埌MinIO + try (InputStream in = file.getInputStream()) { + minioClient.putObject(PutObjectArgs.builder() + .bucket(bucketName) + .object(bucketFilePath) + .stream(in, in.available(), -1) + .contentType(file.getContentType()) + .build() + ); + } catch (Exception e) { + throw new UtilException("MinioUtils锛氫笂浼犳枃浠跺伐鍏风被寮傚父"); + } + MinioResult minioResult = new MinioResult(); + minioResult.setBucketFileName(bucketFilePath); + // 杩斿洖姘镐箙棰勮鍦板潃 + if (isPreviewExpiry) { + String previewUrl = getPreviewUrl(bucketFilePath, bucketName, isPreviewExpiry); + minioResult.setPreviewExpiry(previewUrl); + } + minioResult.setOriginalName(originalFileName); + bucketFileNames.add(minioResult); + } + return bucketFileNames; + } + + /** + * 鏂囦欢涓嬭浇 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param bucketFileName 妗朵腑鏂囦欢鍚嶇О + * @param originalFileName 鍘熷鏂囦欢鍚嶇О + * @param response response瀵硅薄 + */ + public void download(String bucketName, String bucketFileName, String originalFileName, HttpServletResponse response) { + GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName).object(bucketFileName).build(); + try (GetObjectResponse objResponse = minioClient.getObject(objectArgs)) { + byte[] buf = new byte[1024]; + int len; + try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) { + while ((len = objResponse.read(buf)) != -1) { + os.write(buf, 0, len); + } + os.flush(); + byte[] bytes = os.toByteArray(); + response.setCharacterEncoding("utf-8"); + //璁剧疆寮哄埗涓嬭浇涓嶆墦寮� + response.setContentType("application/force-download"); + // 璁剧疆闄勪欢鍚嶇О缂栫爜 + originalFileName = new String(originalFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); + // 璁剧疆闄勪欢鍚嶇О + response.addHeader("Content-Disposition", "attachment;fileName=" + originalFileName); + // 鍐欏叆鏂囦欢 + try (ServletOutputStream stream = response.getOutputStream()) { + stream.write(bytes); + stream.flush(); + } + } + } catch (Exception e) { + throw new UtilException("MinioUtils锛氫笂浼犳枃浠跺伐鍏风被寮傚父"); + } + } + + /** + * 鑾峰彇宸蹭笂浼犲璞$殑鏂囦欢娴侊紙鍚庣鍥犱负涓氬姟闇�瑕佽幏鍙栨枃浠舵祦鍙互璋冪敤璇ユ柟娉曪級 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param bucketFileName 妗朵腑鏂囦欢鍚嶇О + * @return 鏂囦欢娴� + */ + public InputStream getFileStream(String bucketName, String bucketFileName) throws Exception { + GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName).object(bucketFileName).build(); + return minioClient.getObject(objectArgs); + } + + /** + * 鎵归噺鍒犻櫎鏂囦欢瀵硅薄缁撴灉 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param bucketFileName 妗朵腑鏂囦欢鍚嶇О + * @return 鍒犻櫎缁撴灉 + */ + public DeleteError removeObjectsResult(String bucketName, String bucketFileName) { + List<DeleteError> results = removeObjectsResult(bucketName, Collections.singletonList(bucketFileName)); + return !results.isEmpty() ? results.get(0) : null; + } + + /** + * 鎵归噺鍒犻櫎鏂囦欢瀵硅薄缁撴灉 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param bucketFileNames 妗朵腑鏂囦欢鍚嶇О闆嗗悎 + * @return 鍒犻櫎缁撴灉 + */ + public List<DeleteError> removeObjectsResult(String bucketName, List<String> bucketFileNames) { + Iterable<Result<DeleteError>> results = removeObjects(bucketName, bucketFileNames); + List<DeleteError> res = new ArrayList<>(); + for (Result<DeleteError> result : results) { + try { + res.add(result.get()); + } catch (Exception e) { + throw new UtilException("MinioUtils锛氫笂浼犳枃浠跺伐鍏风被寮傚父"); + } + } + return res; + } + + /** + * 鎵归噺鍒犻櫎鏂囦欢瀵硅薄 + * + * @param bucketName 瀛樺偍妗跺悕绉� + * @param bucketFileNames 妗朵腑鏂囦欢鍚嶇О闆嗗悎 + */ + private Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> bucketFileNames) { + List<DeleteObject> dos = bucketFileNames.stream().map(DeleteObject::new).collect(Collectors.toList()); + return minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build()); + } + + /** + * 鏌ヨ棰勮url + * @param bucketFileName minio鏂囦欢鍚嶇О + * @param bucketName 瀛樺偍妗跺悕绉� + * @param isPreviewExpiry 鏄惁闇�瑕佽繃鏈熸椂闂� 榛樿24灏忔椂 + * @return + */ + public String getPreviewUrl(String bucketFileName, String bucketName, Boolean isPreviewExpiry) { + if (StringUtils.isNotBlank(bucketFileName)) { + try { + minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(bucketFileName).build()); + // 涓篺alse鍙敓鎴�24灏忔椂鏈夋晥鏃堕暱鐨剈rl閾炬帴锛屽彲浠ヨ闂鏂囦欢 + if (isPreviewExpiry){ + return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(bucketFileName).build()); + }else { + return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(bucketFileName).expiry(previewExpiry, TimeUnit.HOURS).build()); + } + } catch (Exception e) { + throw new UtilException("MinioUtils锛氫笂浼犳枃浠跺伐鍏风被寮傚父"); + } + } + return null; + } +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java index 3900552..7b0300c 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java @@ -17,7 +17,7 @@ /** * 閫氱敤閰嶇疆 - * + * * @author ruoyi */ @Configuration @@ -32,11 +32,15 @@ /** 鏈湴鏂囦欢涓婁紶璺緞 */ registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**") .addResourceLocations("file:" + RuoYiConfig.getProfile() + "/"); +// +// /** swagger閰嶇疆 */ +// registry.addResourceHandler("/swagger-ui/**") +// .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") +// .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic()); - /** swagger閰嶇疆 */ - registry.addResourceHandler("/swagger-ui/**") - .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") - .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic()); + registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** @@ -70,4 +74,4 @@ // 杩斿洖鏂扮殑CorsFilter return new CorsFilter(source); } -} \ No newline at end of file +} -- Gitblit v1.9.3