Quellcode durchsuchen

自定义文件上传

dengsixing vor 3 Jahren
Ursprung
Commit
12f2296726

+ 11 - 0
4dkankan-center-scene/src/main/java/com/fdkankan/scene/controller/SceneEditController.java

@@ -855,6 +855,17 @@ public class SceneEditController extends BaseController {
     }
 
     /**
+     * 用户自定义上传文本内容上传
+     * @param param
+     * @return
+     * @throws Exception
+     */
+    @RequestMapping(value = "/upload/content", method = RequestMethod.POST)
+    public String uploadContent(@RequestBody @Validated UploadContentParamVO param) throws Exception {
+        return sceneUploadService.uploadContent(param);
+    }
+
+    /**
      * <p>
             删除文件
      * </p>

+ 5 - 0
4dkankan-center-scene/src/main/java/com/fdkankan/scene/service/ISceneUploadService.java

@@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.fdkankan.common.response.ResultData;
 import com.fdkankan.scene.entity.SceneUpload;
 import com.fdkankan.scene.vo.DeleteFileParamVO;
+import com.fdkankan.scene.vo.UploadContentParamVO;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.multipart.MultipartFile;
 
 /**
@@ -20,4 +23,6 @@ public interface ISceneUploadService extends IService<SceneUpload> {
         String sceneCode, Integer type,String token, String uploadPath) throws Exception;
 
     ResultData delete(DeleteFileParamVO param) throws Exception;
+
+    String uploadContent(UploadContentParamVO param) throws Exception;
 }

+ 26 - 0
4dkankan-center-scene/src/main/java/com/fdkankan/scene/service/impl/SceneUploadServiceImpl.java

@@ -22,7 +22,10 @@ import com.fdkankan.scene.mapper.ISceneUploadMapper;
 import com.fdkankan.scene.service.ISceneProService;
 import com.fdkankan.scene.service.ISceneUploadService;
 import com.fdkankan.scene.vo.DeleteFileParamVO;
+import com.fdkankan.scene.vo.UploadContentParamVO;
 import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.HashSet;
 import java.util.Set;
 import org.apache.commons.lang3.StringUtils;
@@ -63,6 +66,9 @@ public class SceneUploadServiceImpl extends ServiceImpl<ISceneUploadMapper, Scen
     @Value("${oss.bucket:4dkankan}")
     private String bucket;
 
+    @Value("${oss.prefix.url}")
+    private String ossUrlPrefix;
+
     @Autowired
     private SSOLoginHelper ssoLoginHelper;
 
@@ -188,4 +194,24 @@ public class SceneUploadServiceImpl extends ServiceImpl<ISceneUploadMapper, Scen
         this.save(sceneUpload);
     }
 
+    @Override
+    public String uploadContent(UploadContentParamVO param) throws Exception {
+
+        String ossPath = param.getOssPath();
+        String fileName = ossPath.substring(ossPath.lastIndexOf("/") + 1);
+        if(StrUtil.isEmpty(fileName)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
+        }
+        String suffix = fileName.substring(fileName.lastIndexOf("."));
+        if(StrUtil.isEmpty(suffix)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
+        }
+
+        File tempFile = File.createTempFile(UUID.randomUUID().toString(), suffix);
+        cn.hutool.core.io.FileUtil.writeString(param.getContent(), tempFile, StandardCharsets.UTF_8);
+        uploadToOssUtil.upload(tempFile.getPath(), param.getOssPath());
+        tempFile.deleteOnExit();
+
+        return this.ossUrlPrefix + param.getOssPath();
+    }
 }

+ 23 - 0
4dkankan-center-scene/src/main/java/com/fdkankan/scene/vo/UploadContentParamVO.java

@@ -0,0 +1,23 @@
+package com.fdkankan.scene.vo;
+
+import javax.validation.constraints.NotBlank;
+import lombok.Data;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/6/8
+ **/
+@Data
+public class UploadContentParamVO {
+
+    @NotBlank(message = "content不能为空")
+    private String content;
+
+    @NotBlank(message = "ossPath不能为空")
+    private String ossPath;
+
+}