Browse Source

first commit

Edwin 1 year ago
commit
71833d87a0

+ 38 - 0
.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 7 - 0
.idea/encodings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
+  </component>
+</project>

+ 16 - 0
.idea/misc.xml

@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
+  <component name="JsBuildToolPackageJson" sorting="DEFINITION_ORDER" />
+  <component name="MavenProjectsManager">
+    <option name="originalFiles">
+      <list>
+        <option value="$PROJECT_DIR$/pom.xml" />
+      </list>
+    </option>
+    <option name="workspaceImportForciblyTurnedOn" value="true" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 73 - 0
pom.xml

@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         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.itheima</groupId>
+    <artifactId>wukuang-demo</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.6.7</version>
+    </parent>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.5.3.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+        </dependency>
+
+        <!-- https://mvnrepository.com/artifact/com.dameng/DmJdbcDriver18 -->
+
+<!--         docker load -i dm8_20220822_rev166351_x86_rh6_64_ctm.tar-->
+<!--docker run -d -p 5236:5236 &#45;&#45;restart=always &#45;&#45;name dm8_single &#45;&#45;privileged=true -e PAGE_SIZE=16 \-->
+<!-- -e LD_LIBRARY_PATH=/opt/dmdbms/bin -e INSTANCE_NAME=dm8_single \-->
+<!-- -v /data/dm8_01:/opt/dmdbms/data dm8_single:v8.1.2.128_ent_x86_64_ctm_pack4-->
+
+<!--docker logs -f dm8_01-->
+
+        <dependency>
+            <groupId>com.dameng</groupId>
+            <artifactId>DmJdbcDriver18</artifactId>
+            <version>8.1.3.140</version>
+        </dependency>
+
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>7.1.0</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 11 - 0
src/main/java/com/itheima/App.java

@@ -0,0 +1,11 @@
+package com.itheima;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class App {
+    public static void main(String[] args) {
+        SpringApplication.run(App.class, args);
+    }
+}

+ 31 - 0
src/main/java/com/itheima/file/config/MinIOConfig.java

@@ -0,0 +1,31 @@
+package com.itheima.file.config;
+
+import com.heima.file.service.FileStorageService;
+import io.minio.MinioClient;
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+
+@Data
+@Configuration
+@EnableConfigurationProperties({MinIOConfigProperties.class})
+//当引入FileStorageService接口时
+@ConditionalOnClass(FileStorageService.class)
+public class MinIOConfig {
+
+    @Autowired
+    private MinIOConfigProperties minIOConfigProperties;
+
+    @Bean
+    public MinioClient buildMinioClient() {
+        return MinioClient
+                .builder()
+                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
+                .endpoint(minIOConfigProperties.getEndpoint())
+                .build();
+    }
+}

+ 18 - 0
src/main/java/com/itheima/file/config/MinIOConfigProperties.java

@@ -0,0 +1,18 @@
+package com.itheima.file.config;
+
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.io.Serializable;
+
+@Data
+@ConfigurationProperties(prefix = "minio")  // 文件上传 配置前缀file.oss
+public class MinIOConfigProperties implements Serializable {
+
+    private String accessKey;
+    private String secretKey;
+    private String bucket;
+    private String endpoint;
+    private String readPath;
+}

+ 43 - 0
src/main/java/com/itheima/file/service/FileStorageService.java

@@ -0,0 +1,43 @@
+package com.itheima.file.service;
+
+import java.io.InputStream;
+
+/**
+ * @author itheima
+ */
+public interface FileStorageService {
+
+
+    /**
+     *  上传图片文件
+     * @param prefix  文件前缀
+     * @param filename  文件名
+     * @param inputStream 文件流
+     * @return  文件全路径
+     */
+    public String uploadImgFile(String prefix, String filename,InputStream inputStream);
+
+    /**
+     *  上传html文件
+     * @param prefix  文件前缀
+     * @param filename   文件名
+     * @param inputStream  文件流
+     * @return  文件全路径
+     */
+    public String uploadHtmlFile(String prefix, String filename,InputStream inputStream);
+
+    /**
+     * 删除文件
+     * @param pathUrl  文件全路径
+     */
+    public void delete(String pathUrl);
+
+    /**
+     * 下载文件
+     * @param pathUrl  文件全路径
+     * @return
+     *
+     */
+    public byte[]  downLoadFile(String pathUrl);
+
+}

+ 166 - 0
src/main/java/com/itheima/file/service/impl/MinIOFileStorageService.java

@@ -0,0 +1,166 @@
+package com.itheima.file.service.impl;
+
+import com.heima.file.config.MinIOConfig;
+import com.heima.file.config.MinIOConfigProperties;
+import com.heima.file.service.FileStorageService;
+import io.minio.GetObjectArgs;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
+import io.minio.RemoveObjectArgs;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Import;
+import org.springframework.util.StringUtils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+@Slf4j
+@EnableConfigurationProperties(MinIOConfigProperties.class)
+@Import(MinIOConfig.class)
+public class MinIOFileStorageService implements FileStorageService {
+
+    @Autowired
+    private MinioClient minioClient;
+
+    @Autowired
+    private MinIOConfigProperties minIOConfigProperties;
+
+    private final static String separator = "/";
+
+    /**
+     * @param dirPath
+     * @param filename yyyy/mm/dd/file.jpg
+     * @return
+     */
+    public String builderFilePath(String dirPath, String filename) {
+        StringBuilder stringBuilder = new StringBuilder(50);
+        if (!StringUtils.isEmpty(dirPath)) {
+            stringBuilder.append(dirPath).append(separator);
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
+        String todayStr = sdf.format(new Date());
+        stringBuilder.append(todayStr).append(separator);
+        stringBuilder.append(filename);
+        return stringBuilder.toString();
+    }
+
+    /**
+     * 上传图片文件
+     *
+     * @param prefix      文件前缀
+     * @param filename    文件名
+     * @param inputStream 文件流
+     * @return 文件全路径
+     */
+    @Override
+    public String uploadImgFile(String prefix, String filename, InputStream inputStream) {
+        String filePath = builderFilePath(prefix, filename);
+        try {
+            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
+                    .object(filePath)
+                    .contentType("image/jpg")
+                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1)
+                    .build();
+            minioClient.putObject(putObjectArgs);
+            StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());
+            urlPath.append(separator + minIOConfigProperties.getBucket());
+            urlPath.append(separator);
+            urlPath.append(filePath);
+            return urlPath.toString();
+        } catch (Exception ex) {
+            log.error("minio put file error.", ex);
+            throw new RuntimeException("上传文件失败");
+        }
+    }
+
+    /**
+     * 上传html文件
+     *
+     * @param prefix      文件前缀
+     * @param filename    文件名
+     * @param inputStream 文件流
+     * @return 文件全路径
+     */
+    @Override
+    public String uploadHtmlFile(String prefix, String filename, InputStream inputStream) {
+        String filePath = builderFilePath(prefix, filename);
+        try {
+            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
+                    .object(filePath)
+                    .contentType("text/html")
+                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1)
+                    .build();
+            minioClient.putObject(putObjectArgs);
+            StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());
+            urlPath.append(separator + minIOConfigProperties.getBucket());
+            urlPath.append(separator);
+            urlPath.append(filePath);
+            return urlPath.toString();
+        } catch (Exception ex) {
+            log.error("minio put file error.", ex);
+            ex.printStackTrace();
+            throw new RuntimeException("上传文件失败");
+        }
+    }
+
+    /**
+     * 删除文件
+     *
+     * @param pathUrl 文件全路径
+     */
+    @Override
+    public void delete(String pathUrl) {
+        String key = pathUrl.replace(minIOConfigProperties.getEndpoint() + "/", "");
+        int index = key.indexOf(separator);
+        String bucket = key.substring(0, index);
+        String filePath = key.substring(index + 1);
+        // 删除Objects
+        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();
+        try {
+            minioClient.removeObject(removeObjectArgs);
+        } catch (Exception e) {
+            log.error("minio remove file error.  pathUrl:{}", pathUrl);
+            e.printStackTrace();
+        }
+    }
+
+
+    /**
+     * 下载文件
+     *
+     * @param pathUrl 文件全路径
+     * @return 文件流
+     */
+    @Override
+    public byte[] downLoadFile(String pathUrl) {
+        String key = pathUrl.replace(minIOConfigProperties.getEndpoint() + "/", "");
+        int index = key.indexOf(separator);
+        String bucket = key.substring(0, index);
+        String filePath = key.substring(index + 1);
+        InputStream inputStream = null;
+        try {
+            inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());
+        } catch (Exception e) {
+            log.error("minio down file error.  pathUrl:{}", pathUrl);
+            e.printStackTrace();
+        }
+
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        byte[] buff = new byte[100];
+        int rc = 0;
+        while (true) {
+            try {
+                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            byteArrayOutputStream.write(buff, 0, rc);
+        }
+        return byteArrayOutputStream.toByteArray();
+    }
+}

+ 28 - 0
src/main/resources/application.yml

@@ -0,0 +1,28 @@
+spring:
+  datasource:
+    driver-class-name: dm.jdbc.driver.DmDriver
+    url: jdbc:dm://139.198.181.54:3306/big_event?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
+    username: SYSDBA
+    password: SYSDBA
+  data:
+    redis:
+      host: 139.198.181.54
+      password: Dcp#7ujm
+      port: 6379
+
+server:
+  port: 8081
+mybatis-plus:
+  mapper-locations: classpath*:mapper/*.xml
+  #设置别名包扫描路径,通过该属性可以给包中的类注册别名
+  type-aliases-package: com.itheima.pojo
+  configuration:
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+    map-underscore-to-camel-case: true
+
+minio:
+  accessKey:
+  secretKey: 
+  bucket:
+  endpoint:
+  readPath: