Browse Source

文件上传服务器部署

Zhangtf 1 tuần trước cách đây
mục cha
commit
444b91f42a

+ 17 - 7
xzl-admin/pom.xml

@@ -16,6 +16,16 @@
     </description>
 
     <dependencies>
+        <dependency>
+            <groupId>com.squareup.okhttp3</groupId>
+            <artifactId>okhttp</artifactId>
+            <version>4.11.0</version>
+        </dependency>
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>8.5.7</version> <!-- 使用最新版本 -->
+        </dependency>
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>alibaba-dingtalk-service-sdk</artifactId>
@@ -131,17 +141,17 @@
                     </execution>
                 </executions>
             </plugin>
-            <plugin>   
-                <groupId>org.apache.maven.plugins</groupId>   
-                <artifactId>maven-war-plugin</artifactId>   
-                <version>3.1.0</version>   
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>3.1.0</version>
                 <configuration>
                     <failOnMissingWebXml>false</failOnMissingWebXml>
                     <warName>${project.artifactId}</warName>
-                </configuration>   
-           </plugin>   
+                </configuration>
+           </plugin>
         </plugins>
         <finalName>${project.artifactId}</finalName>
     </build>
 
-</project>
+</project>

+ 10 - 0
xzl-admin/src/main/java/com/xzl/XzlApplication.java

@@ -1,9 +1,19 @@
 package com.xzl;
 
+import com.xzl.common.utils.MinioUtil;
 import com.xzl.common.utils.SecurityUtils;
+import io.minio.BucketExistsArgs;
+import io.minio.MakeBucketArgs;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+
+import java.io.FileInputStream;
+
+import static com.xzl.web.core.config.MinIOConfig.getMinioClient;
 
 /**
  * 启动程序

+ 28 - 0
xzl-admin/src/main/java/com/xzl/web/core/config/MinIOConfig.java

@@ -0,0 +1,28 @@
+package com.xzl.web.core.config;
+import io.minio.BucketExistsArgs;
+import io.minio.MakeBucketArgs;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
+import io.minio.errors.*;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.FileInputStream;
+
+@Configuration
+public class MinIOConfig {
+    private static final String ENDPOINT = "http://192.168.1.12:9000";
+    private static final String ACCESS_KEY = "your-access-key";
+    private static final String SECRET_KEY = "your-secret-key";
+
+    @Bean
+    public static MinioClient getMinioClient() throws Exception {
+
+
+        return MinioClient.builder()
+                .endpoint("http://192.168.1.12:9000")
+                .credentials("minioadmin", "minioadmin")
+                .build();
+    }
+
+}

+ 36 - 0
xzl-admin/src/main/java/com/xzl/web/service/impl/KnowledgeFileServiceImpl.java

@@ -1,14 +1,21 @@
 package com.xzl.web.service.impl;
 
+import java.io.FileInputStream;
 import java.util.List;
 
 import com.xzl.common.core.domain.entity.KnowledgeFile;
 import com.xzl.common.utils.DateUtils;
 import com.xzl.web.mapper.KnowledgeFileMapper;
 import com.xzl.web.service.KnowledgeFileService;
+import io.minio.BucketExistsArgs;
+import io.minio.MakeBucketArgs;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import static com.xzl.web.core.config.MinIOConfig.getMinioClient;
+
 /**
  * 文件Service业务层处理
  *
@@ -106,6 +113,35 @@ public class KnowledgeFileServiceImpl implements KnowledgeFileService
     public int updateSysFileStatus(Long fileId, String status) {
         return knowledgeFileMapper.updateSysFileStatus(fileId, status);
     }
+
+    public void uploadFile(String bucketName, String objectName, String filePath) {
+        try {
+            MinioClient minioClient = getMinioClient();
+
+            // 检查存储桶是否存在,不存在则创建
+            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
+                    .bucket(bucketName)
+                    .build());
+
+            if (!found) {
+                minioClient.makeBucket(MakeBucketArgs.builder()
+                        .bucket(bucketName)
+                        .build());
+            }
+
+            // 上传文件
+            minioClient.putObject(
+                    PutObjectArgs.builder()
+                            .bucket(bucketName)
+                            .object(objectName)
+                            .stream(new FileInputStream(filePath), -1, 10485760) // 10MB part size
+                            .build());
+
+            System.out.println("文件上传成功: " + objectName);
+        } catch (Exception e) {
+            System.err.println("上传失败: " + e.getMessage());
+        }
+    }
 }
 
 

+ 6 - 0
xzl-admin/src/main/resources/application-dev.yml

@@ -69,3 +69,9 @@ pythonFilePath: /usr/local/python/
 pythonPath: /usr/local/Cellar/python@3.10/3.10.11/bin/python3.10
 sqlFilePath: /usr/local/sql
 ERImagePath: /usr/local/ERImage
+
+minio:
+    endpoint: http://192.168.1.12:9000
+    access-key: minioadmin
+    secret-key: minioadmin
+    bucket-name: mc-kb

+ 9 - 3
xzl-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>
-        
+
         <!-- 动态数据源 -->
 		<dependency>
 			<groupId>com.baomidou</groupId>
@@ -125,7 +125,13 @@
             <groupId>javax.servlet</groupId>
             <artifactId>javax.servlet-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>8.5.7</version>
+            <scope>compile</scope>
+        </dependency>
 
     </dependencies>
 
-</project>
+</project>

+ 36 - 0
xzl-common/src/main/java/com/xzl/common/core/domain/model/FileInfo.java

@@ -0,0 +1,36 @@
+package com.xzl.common.core.domain.model;
+
+/**
+ * 文件类
+ */
+public class FileInfo {
+    private String fileName;
+
+    private Boolean directoryFlag;
+
+    private String etag;
+
+    public String getFileName() {
+        return fileName;
+    }
+
+    public void setFileName(String fileName) {
+        this.fileName = fileName;
+    }
+
+    public Boolean getDirectoryFlag() {
+        return directoryFlag;
+    }
+
+    public void setDirectoryFlag(Boolean directoryFlag) {
+        this.directoryFlag = directoryFlag;
+    }
+
+    public String getEtag() {
+        return etag;
+    }
+
+    public void setEtag(String etag) {
+        this.etag = etag;
+    }
+}

+ 107 - 0
xzl-common/src/main/java/com/xzl/common/utils/MinioUtil.java

@@ -0,0 +1,107 @@
+package com.xzl.common.utils;
+
+import com.xzl.common.core.domain.model.FileInfo;
+import io.minio.*;
+import io.minio.messages.Bucket;
+import io.minio.messages.Item;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Component
+public class MinioUtil {
+
+    @Autowired
+    private MinioClient minioClient;
+
+    /**
+     * 创建桶
+     * @param bucket
+     * @throws Exception
+     */
+    public void createBucket(String bucket) throws Exception {
+        //查看桶是否存在
+        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
+        if (!exists){
+            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
+        }
+    }
+
+    /**
+     * 上传文件
+     * @param inputStream
+     * @param bucket
+     * @param objectName
+     * @throws Exception
+     */
+    public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception{
+        //上传文件
+        minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
+                .stream(inputStream,-1,Integer.MAX_VALUE).build());
+    }
+
+    /**
+     * 获取所有的桶
+     * @return
+     * @throws Exception
+     */
+    public List<String> getAllBucket() throws Exception{
+        List<Bucket> buckets = minioClient.listBuckets();
+        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
+    }
+
+    /**
+     * 获取所有的问题件
+     * @param bucket
+     * @return
+     * @throws Exception
+     */
+    public List<FileInfo> getAllFiles(String bucket) throws Exception {
+        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
+        List<FileInfo> fileInfoList = new ArrayList<>();
+        for (Result<Item> result: results) {
+            FileInfo fileInfo = new FileInfo();
+            Item item = result.get();
+            //塞入属性
+            fileInfo.setFileName(item.objectName());
+            fileInfo.setDirectoryFlag(item.isDir());
+            fileInfo.setEtag(item.etag());
+            fileInfoList.add(fileInfo);
+        }
+        return fileInfoList;
+    }
+
+    /**
+     * 下载文件
+     * @param bucket
+     * @param objectName
+     * @return
+     * @throws Exception
+     */
+    public InputStream downLoad(String bucket,String objectName) throws Exception{
+        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
+    }
+
+    /**
+     * 删除桶
+     * @param bucket
+     * @throws Exception
+     */
+    public void deleteBucket(String bucket) throws Exception{
+        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
+    }
+
+    /**
+     * 删除文件
+     * @param objectName
+     * @throws Exception
+     */
+    public void deleteObject(String objectName) throws Exception{
+        minioClient.removeObject(RemoveObjectArgs.builder().object(objectName).build());
+    }
+}