diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/pom.xml b/jero-boot/jero-boot-base/jero-boot-base-core/pom.xml
index 50dded87b..63207df4b 100644
--- a/jero-boot/jero-boot-base/jero-boot-base-core/pom.xml
+++ b/jero-boot/jero-boot-base/jero-boot-base-core/pom.xml
@@ -241,7 +241,18 @@
com.squareup.okhttp3
okhttp
-
+
+
+ com.qcloud
+ cos_api
+ 5.2.4
+
+
+ slf4j-log4j12
+ org.slf4j
+
+
+
\ No newline at end of file
diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/oss/CosBootUtil.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/oss/CosBootUtil.java
new file mode 100644
index 000000000..7f6f42b23
--- /dev/null
+++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/oss/CosBootUtil.java
@@ -0,0 +1,260 @@
+package com.jero.common.util.oss;
+
+import com.jero.config.oss.CosConfiguration;
+import com.qcloud.cos.COSClient;
+import com.qcloud.cos.ClientConfig;
+import com.qcloud.cos.auth.BasicCOSCredentials;
+import com.qcloud.cos.auth.COSCredentials;
+import com.qcloud.cos.exception.CosClientException;
+import com.qcloud.cos.exception.CosServiceException;
+import com.qcloud.cos.http.HttpMethodName;
+import com.qcloud.cos.http.HttpProtocol;
+import com.qcloud.cos.region.Region;
+import com.qcloud.cos.utils.IOUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecgframework.core.util.ApplicationContextUtil;
+import org.jeecgframework.dict.service.AutoPoiDictServiceI;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.*;
+import java.net.*;
+import java.util.Date;
+
+/**
+ * @Author: liyawei
+ * @Description:
+ * @Date: Created in 9:12 2022/6/8
+ */
+@Slf4j
+public class CosBootUtil {
+ private static String secretId;
+ private static String secretKey;
+ private static String region;
+ private static String bucketName;
+ private static String appId;
+ private static String endPoint;
+
+ public static void setSecretId(String secretId) {
+ CosBootUtil.secretId = secretId;
+ }
+
+ public static void setSecretKey(String secretKey) {
+ CosBootUtil.secretKey = secretKey;
+ }
+
+ public static void setRegion(String region) {
+ CosBootUtil.region = region;
+ }
+
+ public static void setBucketName(String bucketName) {
+ CosBootUtil.bucketName = bucketName;
+ }
+
+ public static void setAppId(String appId) {
+ CosBootUtil.appId = appId;
+ }
+
+ public static void setEndPoint(String endPoint) {
+ CosBootUtil.endPoint = endPoint;
+ }
+
+ public static String getSecretId() {
+ return secretId;
+ }
+
+ public static String getSecretKey() {
+ return secretKey;
+ }
+
+ public static String getRegion() {
+ return region;
+ }
+
+ public static String getBucketName() {
+ return bucketName;
+ }
+
+ public static String getAppId() {
+ return appId;
+ }
+
+ public static String getEndPoint() {
+ return endPoint;
+ }
+
+ /**
+ * cos 工具客户端
+ */
+ private static COSClient cosClient = null;
+ private static COSCredentials cred = null;
+
+
+ /**
+ * 初始化
+ */
+ private static void init() {
+ CosConfiguration ccf = ApplicationContextUtil.getContext().getBean(CosConfiguration.class);
+ ccf.initCosBootConfiguration();
+ // 1 初始化用户身份信息(secretId, secretKey)
+ cred = new BasicCOSCredentials(secretId, secretKey);
+
+ // 2 设置bucket的区域,
+ ClientConfig clientConfig = new ClientConfig(new Region(region));
+ clientConfig.setHttpProtocol(HttpProtocol.https);
+
+ // 以下的设置,是可选的:
+
+ // 设置 socket 读取超时,默认 30s
+// clientConfig.setSocketTimeout(30*1000);
+ // 设置建立连接超时,默认 30s
+// clientConfig.setConnectionTimeout(30*1000);
+ // 3 生成cos客户端
+ cosClient = new COSClient(cred, clientConfig);
+ }
+
+ /**
+ * cos 上传
+ * 相同key 文件会覆盖
+ * @param file
+ * @param key
+ */
+ public static void upload(MultipartFile file, String key){
+ init();
+ Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
+ // 生成预签名上传 URL
+ URL url = cosClient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.PUT);
+ log.info("生成预签名上传URL:" + url);
+ int responseCode = upLoadFile(url.toString(), file);
+ if (responseCode == 200) {
+ log.info("上传成功!");
+ } else {
+ log.info("上传失败!");
+ }
+ cosClient.shutdown();
+ }
+
+ private static int upLoadFile(String singnedurl,MultipartFile file){
+ InputStream inputStream= null;
+ DataOutputStream out = null;
+ int responseCode = 0;
+ try {
+ URL url= null;
+ url = new URL(singnedurl);
+ inputStream= file.getInputStream();
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("PUT");
+ out = new DataOutputStream(connection.getOutputStream());
+
+ // 写入要上传的数据
+ IOUtils.copy(inputStream, out);
+ responseCode = connection.getResponseCode();
+ log.info("Service returned response code " + responseCode);
+ } catch (ProtocolException e) {
+ log.error(e.getMessage(), e);
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ } finally {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ }
+ try {
+ out.close();
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ }
+ return responseCode;
+ }
+ }
+
+ /**
+ * cos 下载
+ * @param key
+ * @return
+ */
+ public static InputStream download(String key) {
+ Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
+ // 生成预签名上传 URL
+ URL url = cosClient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.GET);
+ log.info("生成预签名上传URL:" + url);
+ InputStream in = downFile(url.toString());
+ if (in != null) {
+ log.info("下载成功!");
+ } else {
+ log.info("下载失败!");
+ }
+// System.out.println("下载成功");
+ cosClient.shutdown();
+ return in;
+ }
+
+ private static InputStream downFile(String singnedurl) {
+ InputStream in = null;
+ try {
+ URL url=new URL(singnedurl);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setDoInput(true);
+ connection.setRequestMethod("GET");
+ in = connection.getInputStream();
+ int responseCode = connection.getResponseCode();
+ log.info("Service returned response code " + responseCode);
+
+ // 写入要上传的数据
+// IOUtils.copy(in, outputStream);
+ return in;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }/* finally {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+// fileOutputStream.close();
+ }*/
+ return in;
+ }
+
+ /**
+ * 判断文件是否存在
+ * 上传,下载,删除时 必需调用此方法
+ * @param key
+ * @return
+ */
+ public static boolean doesObjectExist(String key) {
+ init();
+ boolean objectExists = false;
+ try {
+ objectExists = cosClient.doesObjectExist(bucketName, key);
+ return objectExists;
+ } catch (CosServiceException e) {
+ e.printStackTrace();
+ } catch (CosClientException e) {
+ e.printStackTrace();
+ } finally {
+ // 确认本进程不再使用 cosClient 实例之后,关闭之
+ cosClient.shutdown();
+ }
+ return objectExists;
+ }
+
+ /**
+ * 删除
+ */
+ public static void delete(String key) {
+ new Thread(new Runnable() {
+ public void run() {
+ // 指定要删除的 bucket 和路径
+ try {
+ cosClient.deleteObject(bucketName, key);
+ log.info("删除成功");
+ } catch (Throwable tb) {
+ log.info("删除文件失败");
+ tb.printStackTrace();
+ }
+ }
+ }).start();
+ }
+}
diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/oss/CosConfiguration.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/oss/CosConfiguration.java
new file mode 100644
index 000000000..fdc0d37e2
--- /dev/null
+++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/oss/CosConfiguration.java
@@ -0,0 +1,39 @@
+package com.jero.config.oss;
+
+import com.jero.common.util.oss.CosBootUtil;
+import com.jero.common.util.oss.OssBootUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @Author: liyawei
+ * @Description:
+ * @Date: Created in 9:11 2022/6/8
+ */
+@Configuration
+public class CosConfiguration {
+ @Value("${jero.cos.secretId}")
+ private String secretId;
+ @Value("${jero.cos.secretKey}")
+ private String secretKey;
+ @Value("${jero.cos.region}")
+ private String region;
+ @Value("${jero.cos.bucketName}")
+ private String bucketName;
+ @Value("${jero.cos.appId}")
+ private String appId;
+ @Value("${jero.cos.endPoint}")
+ private String endPoint;
+
+
+ @Bean
+ public void initCosBootConfiguration() {
+ CosBootUtil.setSecretId(secretId);
+ CosBootUtil.setSecretKey(secretKey);
+ CosBootUtil.setRegion(region);
+ CosBootUtil.setBucketName(bucketName);
+ CosBootUtil.setAppId(appId);
+ CosBootUtil.setEndPoint(endPoint);
+ }
+}
diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java
index 47722dd22..2866916e0 100644
--- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java
+++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java
@@ -19,6 +19,8 @@ public interface IOSSFileService extends IService {
*/
public OSSFile uploadLocal(MultipartFile mf, String bizPath,String state,String cut);
+ public OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath,String state,String cut);
+
public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath,String state,String cut);
diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java
index c812e6a41..e2587d558 100644
--- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java
+++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java
@@ -7,6 +7,7 @@ import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.CommonUtils;
import com.jero.common.util.oConvertUtils;
+import com.jero.common.util.oss.CosBootUtil;
import com.jero.common.util.oss.OssBootUtil;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.mapper.OSSFileMapper;
@@ -164,6 +165,103 @@ public class OSSFileServiceImpl extends ServiceImpl impl
return oSSFile;
}
+ /**
+ * 本地文件上传 cos
+ *
+ * @param mf 文件
+ * @param bizPath 自定义路径
+ * @return
+ */
+ @Override
+ public OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath,String state,String cut) {
+
+ //处理文件大小,大于100M抛出异常
+ long fileSize = mf.getSize() / 1024 / 1024;
+ String originalFilename = mf.getOriginalFilename();
+ if (fileSize >= 100) {
+ if(cut.equals(CutEnum.CN.getValue())){
+ throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
+ }else{
+ throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
+ }
+ }
+
+ OSSFile oSSFile = new OSSFile();
+ try {
+ String ctxPath = uploadpath;
+ String fileName = null;
+ String fileType = null;
+
+ String orgName = mf.getOriginalFilename();// 获取文件名
+ orgName = CommonUtils.getFileName(orgName);
+ if (orgName.indexOf(".") != -1) {
+ fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
+ } else {
+ fileName = orgName + "_" + System.currentTimeMillis();
+ }
+
+ String filePath = ctxPath + "/" + fileName;
+ fileType = orgName.substring(orgName.lastIndexOf("."));
+ String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
+ //判断文件类型
+ if("1".equals(state)){
+ //固定文件
+ if(!fileTypeStr.contains(fileType)){
+ if(cut.equals(CutEnum.CN.getValue())){
+ throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
+ }else{
+ throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!");
+ }
+ }
+ }else{
+ if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
+ if(cut.equals(CutEnum.CN.getValue())){
+ throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!");
+ }else{
+ throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again!");
+ }
+ }
+ }
+
+ if (StringUtils.isNotEmpty(fileType)) {
+ String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX",
+ ".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG",
+ ".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP",
+ ".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"};
+ boolean flag = true;
+ for (String fileTypeTemp : fileTypeArr) {
+ if (fileTypeTemp.equalsIgnoreCase(fileType)) {
+ flag = false;
+ }
+ }
+ if (flag) {
+ if(cut.equals(CutEnum.CN.getValue())){
+ throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
+ }else{
+ throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
+ }
+ }
+ }
+
+ // 将文件上传至腾讯云 cos
+ CosBootUtil.upload(mf, filePath);
+
+ LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+ //存入文件表
+ oSSFile.setFileName(orgName);
+ oSSFile.setId(UuidUtils.getUUID());
+ oSSFile.setUrl(filePath);
+ oSSFile.setCreateBy(loginUser.getUsername());
+ oSSFile.setCreateTime(new Date());
+ this.save(oSSFile);
+ oSSFile.setUrl(null);
+ return oSSFile;
+ } catch (Exception e) {
+ log.error(e.getMessage(), e);
+ }
+ return oSSFile;
+ }
+
/**
* 本地文件上传-文档拆分专用
*
diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java
index 2552347cb..307364f65 100644
--- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java
+++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java
@@ -11,6 +11,7 @@ import com.jero.common.system.api.ISysBaseAPI;
import com.jero.common.util.RestUtil;
import com.jero.common.util.TokenUtils;
import com.jero.common.util.oConvertUtils;
+import com.jero.common.util.oss.CosBootUtil;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.system.util.PDFUtils;
@@ -158,14 +159,16 @@ public class CommonController {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
if (oConvertUtils.isEmpty(bizPath)) {
- if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) {
- //未指定目录,则用阿里云默认目录 upload
- bizPath = "upload";
- } else {
- bizPath = "";
- }
+ bizPath = "";
+// if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) {
+// //未指定目录,则用阿里云默认目录 upload
+// bizPath = "upload";
+// } else {
+// bizPath = "";
+// }
}
OSSFile oSSFile = ossFileService.uploadLocal(file, bizPath,state,cut);
+// OSSFile oSSFile = ossFileService.uploadLocalOfCos(file, bizPath,state,cut); // 切换cos
if (oConvertUtils.isNotEmpty(oSSFile)) {
result.setResult(oSSFile);
result.setSuccess(true);
@@ -331,8 +334,73 @@ public class CommonController {
}
}
}
-
}
+
+ /**
+ * word,excel预览 cos
+ *
+ * @param id 传入文件id
+ * @param request
+ * @param response
+ */
+// @GetMapping(value = "/downLoadFile")
+ public void downLoadFromCos(String id,HttpServletRequest request, HttpServletResponse response) {
+ if(StringUtils.isBlank(id)){
+ throw new JeroBootException("参数信息不全");
+ }
+ id = id.split("\\.")[0];
+ // 查询数据表数据是否存在
+ LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
+ queryWrapper.eq(OSSFile::getId,id);
+ OSSFile ossFile = ossFileService.getOne(queryWrapper);
+ if( null == ossFile){
+ throw new JeroBootException("文件不存在");
+ }
+ InputStream inputStream = null;
+ OutputStream outputStream = null;
+ try {
+ String fileName = "";
+ //本地下载
+ String filePath = ossFile.getUrl();
+ if(!CosBootUtil.doesObjectExist(filePath)){
+ response.setStatus(404);
+ throw new RuntimeException("文件不存在..");
+ }
+ // 文件名称
+ fileName = ossFile.getFileName();
+ response.setContentType("application/force-download");// 设置强制下载不打开
+ response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
+
+ outputStream = response.getOutputStream();
+ inputStream = CosBootUtil.download(filePath);
+ byte[] buf = new byte[1024];
+ int len;
+ while ((len = inputStream.read(buf)) > 0) {
+ outputStream.write(buf, 0, len);
+ }
+ response.flushBuffer();
+ } catch (IOException e) {
+ log.error("文件下载失败" + e.getMessage());
+ response.setStatus(404);
+ e.printStackTrace();
+ } finally {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ }
+ }
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ }
+ }
+ }
+ }
+
/**
* 文件下载
*
diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysPermissionController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysPermissionController.java
index 9e9a6d8b1..9668a5a92 100644
--- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysPermissionController.java
+++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysPermissionController.java
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jero.common.constant.enums.CutEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import com.jero.common.api.vo.Result;
import com.jero.common.constant.CommonConstant;
@@ -387,6 +388,7 @@ public class SysPermissionController {
*
* @return
*/
+ @RequiresPermissions("sys:role:auth")
@RequestMapping(value = "/queryRolePermission", method = RequestMethod.GET)
public Result> queryRolePermission(@RequestParam(name = "roleId", required = true) String roleId) {
Result> result = new Result<>();
@@ -405,6 +407,7 @@ public class SysPermissionController {
*
* @return
*/
+ @RequiresPermissions("sys:role:auth")
@RequestMapping(value = "/saveRolePermission", method = RequestMethod.POST)
//@RequiresRoles({ "admin" })
public Result saveRolePermission(@RequestBody JSONObject json) {
diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysRoleController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysRoleController.java
index 3a693d1fd..e810083d7 100644
--- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysRoleController.java
+++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysRoleController.java
@@ -368,7 +368,7 @@ public class SysRoleController {
* @param request
* @return
*/
- @RequiresPermissions("sys:role:list")
+ @RequiresPermissions("sys:role:auth")
@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
public Result