Răsfoiți Sursa

添加OOS文件上传代码 等搞定二级域名预览 再上线

fangzhen 7 luni în urmă
părinte
comite
8fc69897be

+ 6 - 1
ruoyi-admin/pom.xml

@@ -60,7 +60,12 @@
             <groupId>com.ruoyi</groupId>
             <artifactId>ruoyi-generator</artifactId>
         </dependency>
-
+        <!--oss依赖-->
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>3.11.2</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 37 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java

@@ -99,6 +99,43 @@ public class CommonController {
         }
     }
 
+//    /**
+//     * 通用上传请求(单个)
+//     */
+//    @PostMapping("/upload")
+//    public AjaxResult uploadFile(MultipartFile file) throws Exception {
+//        try {
+//            // 上传文件路径
+//            String filePath = RuoYiConfig.getUploadPath();
+//            String thumbnailPath = "";
+//            // 上传并返回新文件名称
+//            String fileName = OssUtils.uploadMultipartFile(file);
+//            System.out.println(fileName);
+//            String url = OssUtils.getOssFilePath(fileName);
+//            String fileType = FileUtils.getFileType(file);
+//            if ("video".equalsIgnoreCase(fileType)) {
+//                thumbnailPath = fileName;
+//                int dotIndex = thumbnailPath.lastIndexOf(".");
+//                if (dotIndex != -1) {
+//                    thumbnailPath = thumbnailPath.substring(0, dotIndex) + ".jpg";
+//                } else {
+//                    thumbnailPath = thumbnailPath + ".jpg";  // 如果没有后缀,直接加 .jpg
+//                }
+//                FileUtils.screenShots(fileName.replace("/profile", RuoYiConfig.getProfile()), thumbnailPath, "00:00:01");
+//            }
+//            AjaxResult ajax = AjaxResult.success();
+//            ajax.put("url", url);
+//            ajax.put("fileName", fileName);
+//            ajax.put("newFileName", FileUtils.getName(fileName));
+//            ajax.put("originalFilename", file.getOriginalFilename());
+//            ajax.put("fileType", fileType);
+//            ajax.put("thumbnailPath", thumbnailPath);
+//            return ajax;
+//        } catch (Exception e) {
+//            return AjaxResult.error(e.getMessage());
+//        }
+//    }
+
     /**
      * 通用上传请求(多个)
      */

+ 1 - 1
ruoyi-admin/src/main/resources/application.yml

@@ -7,7 +7,7 @@ ruoyi:
   # 版权年份
   copyrightYear: 2024
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
-  profile: D:/ruoyi/uploadPath
+  profile: /home/ruoyi/uploadPath
   # 获取ip地址开关
   addressEnabled: false
   # 验证码类型 math 数字计算 char 字符验证

+ 7 - 0
ruoyi-admin/src/main/resources/i18n/messages.properties

@@ -37,3 +37,10 @@ no.update.permission=您没有修改数据的权限,请联系管理员添加
 no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
 no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
 no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]
+
+##配置OOS信息
+uoload.oos.endPoint=oss-cn-shanghai.aliyuncs.com
+uoload.oos.accessKeyId=LTAI5tS4n2s89tQLDxjb2jfE
+uoload.oos.accessKeySecret=OdSKx43VHz0MwWnCjAzzOwF9ze8VDR
+uoload.oos.accessPre=https://cysd.oss-cn-shanghai.aliyuncs.com/
+uoload.oos.bucketName=cysd

+ 7 - 0
ruoyi-common/pom.xml

@@ -133,6 +133,13 @@
             <groupId>org.apache.tika</groupId>
             <artifactId>tika-core</artifactId>
         </dependency>
+
+        <!--oss依赖-->
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>3.11.2</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 157 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/OssUtils.java

@@ -0,0 +1,157 @@
+package com.ruoyi.common.utils.file;
+
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.model.GetObjectRequest;
+import com.aliyun.oss.model.PutObjectRequest;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.*;
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+/**
+ * Oss服务调用
+ */
+@Log4j2
+public class OssUtils {
+    private final static String endPoint = "oss-cn-shanghai.aliyuncs.com";
+    private final static String accessKeyId = "LTAI5tS4n2s89tQLDxjb2jfE";
+    private final static String accessKeySecret = "OdSKx43VHz0MwWnCjAzzOwF9ze8VDR";
+    private final static String accessPre = "https://cysd.oss-cn-shanghai.aliyuncs.com/";
+
+    /**
+     * bucket名称
+     *
+     * @return
+     */
+    private final static String bucketName = "cysd";
+
+    private static OSS ossClient;
+
+    static {
+        ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
+        log.info("oss服务连接成功!");
+    }
+
+    /**
+     * 默认路径上传本地文件
+     *
+     * @param filePath
+     */
+    public static String uploadFile(String filePath) {
+        return uploadFileForBucket(bucketName, getOssFilePath(filePath), filePath);
+    }
+
+    /**
+     * 默认路径上传multipartFile文件
+     *
+     * @param multipartFile
+     */
+    public static String uploadMultipartFile(MultipartFile multipartFile) throws IOException {
+        return uploadMultipartFile(bucketName, getOssFilePath(multipartFile.getOriginalFilename()), multipartFile);
+    }
+
+    /**
+     * 上传 multipartFile 类型文件
+     *
+     * @param bucketName
+     * @param ossPath
+     * @param multipartFile
+     */
+    public static String uploadMultipartFile(String bucketName, String ossPath, MultipartFile multipartFile) throws IOException {
+        InputStream inputStream = null;
+        try {
+            inputStream = multipartFile.getInputStream();
+            uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            inputStream.close();
+        }
+
+        return accessPre + ossPath;
+        //这是返回的图片路径,我们要用这个
+//        return ossPath;
+    }
+
+    /**
+     * 使用File上传PutObject上传文件 ** 程序默认使用次方法上传
+     *
+     * @param bucketName 实例名称
+     * @param ossPath    oss存储路径
+     * @param filePath   本地文件路径
+     */
+    public static String uploadFileForBucket(String bucketName, String ossPath, String filePath) {
+        // 创建PutObjectRequest对象。
+        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
+
+        // 上传
+        ossClient.putObject(putObjectRequest);
+        return accessPre + ossPath;
+//        return ossPath;
+    }
+
+    /**
+     * 使用文件流上传到指定的bucket实例
+     *
+     * @param bucketName 实例名称
+     * @param ossPath    oss存储路径
+     * @param filePath   本地文件路径
+     */
+    public static String uploadFileInputStreamForBucket(String bucketName, String ossPath, String filePath) {
+
+        // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
+        InputStream inputStream = null;
+        try {
+            inputStream = new FileInputStream(filePath);
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
+        // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
+        uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
+        return accessPre + ossPath;
+//        return ossPath;
+    }
+
+    public static void uploadFileInputStreamForBucket(String bucketName, String ossPath, InputStream inputStream) {
+        ossClient.putObject(bucketName, ossPath, inputStream);
+    }
+
+    /**
+     * 下载
+     *
+     * @param ossFilePath
+     * @param filePath
+     */
+    public static void downloadFile(String ossFilePath, String filePath) {
+        downloadFileForBucket(bucketName, ossFilePath, filePath);
+    }
+
+    /**
+     * 下载
+     *
+     * @param bucketName  实例名称
+     * @param ossFilePath oss存储路径
+     * @param filePath    本地文件路径
+     */
+    public static void downloadFileForBucket(String bucketName, String ossFilePath, String filePath) {
+        ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
+    }
+
+    /**
+     * @return
+     */
+    public static String getOssDefaultPath() {
+        LocalDateTime now = LocalDateTime.now();
+        String url = now.getYear() + "/" + now.getMonth() + "/" + now.getDayOfMonth() + "/" + now.getHour() + "/" + now.getMinute() + "/";
+        return url;
+    }
+
+    public static String getOssFilePath(String filePath) {
+        String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
+        return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
+    }
+
+}

+ 1 - 1
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticle.java

@@ -81,7 +81,7 @@ public class CommunityArticle implements Serializable {
      */
     @ApiModelProperty("文章标签")
     @TableField(exist = false)
-    private List<CommunityTag> tags;
+    private List<String> tags;
 
     /**
      * 合集

+ 4 - 4
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityArticleServiceImpl.java

@@ -212,12 +212,12 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
         communityArticle.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
         communityArticleMapper.insert(communityArticle);
         //插入标签日志
-        List<CommunityTag> tags = communityArticle.getTags();
+        List<String> tags = communityArticle.getTags();
         CommunityArticleTag tagLog = null;
-        for (CommunityTag tag : tags) {
+        for (String tag : tags) {
             tagLog = new CommunityArticleTag();
             tagLog.setArticleId(communityArticle.getId());
-            tagLog.setTagId(tag.getId());
+            tagLog.setTagId(Long.parseLong(tag));
             tagLog.setUpdateBy(userId);
             tagLog.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
             tagLog.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
@@ -225,7 +225,7 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
             articleTagMapper.insert(tagLog);
 
             //标签热度自增
-            CommunityTag communityTag = communityTagMapper.selectById(tag.getId());
+            CommunityTag communityTag = communityTagMapper.selectById(Long.parseLong(tag));
             communityTag.setTagHot(Objects.isNull(communityTag.getTagHot()) ? 1 : communityTag.getTagHot() + 1);
             communityTag.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
             communityTag.setUpdateBy(userId);