diff --git a/hope-winery/pom.xml b/hope-winery/pom.xml index 35e857720..2481945b4 100644 --- a/hope-winery/pom.xml +++ b/hope-winery/pom.xml @@ -33,6 +33,8 @@ ${wx-java.version} + + \ No newline at end of file diff --git a/hope-winery/src/main/java/com/ruoyi/winery/controller/WineryGoodsController.java b/hope-winery/src/main/java/com/ruoyi/winery/controller/WineryGoodsController.java index 0f4e2e1bb..11d7367c6 100644 --- a/hope-winery/src/main/java/com/ruoyi/winery/controller/WineryGoodsController.java +++ b/hope-winery/src/main/java/com/ruoyi/winery/controller/WineryGoodsController.java @@ -64,14 +64,14 @@ public class WineryGoodsController extends BaseController { if (StringUtils.isNotBlank(wineryGoods.getGoodsSpec())){ lqw.eq(WineryGoods::getGoodsSpec ,wineryGoods.getGoodsSpec()); } - if (StringUtils.isNotBlank(wineryGoods.getGoodDesc())){ - lqw.eq(WineryGoods::getGoodDesc ,wineryGoods.getGoodDesc()); + if (StringUtils.isNotBlank(wineryGoods.getGoodsDesc())){ + lqw.eq(WineryGoods::getGoodsDesc ,wineryGoods.getGoodsDesc()); } - if (StringUtils.isNotBlank(wineryGoods.getGoodFaceImg())){ - lqw.eq(WineryGoods::getGoodFaceImg ,wineryGoods.getGoodFaceImg()); + if (StringUtils.isNotBlank(wineryGoods.getGoodsFaceImg())){ + lqw.eq(WineryGoods::getGoodsFaceImg ,wineryGoods.getGoodsFaceImg()); } - if (StringUtils.isNotBlank(wineryGoods.getGoodImg())){ - lqw.eq(WineryGoods::getGoodImg ,wineryGoods.getGoodImg()); + if (StringUtils.isNotBlank(wineryGoods.getGoodsImg())){ + lqw.eq(WineryGoods::getGoodsImg ,wineryGoods.getGoodsImg()); } List list = iWineryGoodsService.list(lqw); return getDataTable(list); diff --git a/hope-winery/src/main/java/com/ruoyi/winery/domain/WineryGoods.java b/hope-winery/src/main/java/com/ruoyi/winery/domain/WineryGoods.java index 8ad92ffa2..2f0276c70 100644 --- a/hope-winery/src/main/java/com/ruoyi/winery/domain/WineryGoods.java +++ b/hope-winery/src/main/java/com/ruoyi/winery/domain/WineryGoods.java @@ -58,15 +58,15 @@ private static final long serialVersionUID=1L; /** 商品说明 */ @Excel(name = "商品说明") - private String goodDesc; + private String goodsDesc; /** 商品封面 */ @Excel(name = "商品封面") - private String goodFaceImg; + private String goodsFaceImg; /** 商品图片 */ @Excel(name = "商品图片") - private String goodImg; + private String goodsImg; /** 创建者 */ private String createBy; diff --git a/pom.xml b/pom.xml index c6c402d04..0cfe4123c 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,7 @@ 3.4.1 5.4.0 4.0.0 + 5.6.34 diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java index e0c728fac..c6e2c799f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java @@ -2,12 +2,15 @@ package com.ruoyi.web.controller.common; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import com.ruoyi.common.utils.file.CosUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.ruoyi.common.config.RuoYiConfig; @@ -20,30 +23,30 @@ import com.ruoyi.framework.config.ServerConfig; /** * 通用请求处理 - * + * * @author ruoyi */ @RestController -public class CommonController -{ +public class CommonController { private static final Logger log = LoggerFactory.getLogger(CommonController.class); @Autowired private ServerConfig serverConfig; + + @Autowired + private CosUtils cosUtils; + /** * 通用下载请求 - * + * * @param fileName 文件名称 - * @param delete 是否删除 + * @param delete 是否删除 */ @GetMapping("common/download") - public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) - { - try - { - if (!FileUtils.checkAllowDownload(fileName)) - { + public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) { + try { + if (!FileUtils.checkAllowDownload(fileName)) { throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName)); } String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); @@ -52,13 +55,10 @@ public class CommonController response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, realFileName); FileUtils.writeBytes(filePath, response.getOutputStream()); - if (delete) - { + if (delete) { FileUtils.deleteFile(filePath); } - } - catch (Exception e) - { + } catch (Exception e) { log.error("下载文件失败", e); } } @@ -67,37 +67,44 @@ public class CommonController * 通用上传请求 */ @PostMapping("/common/upload") - public AjaxResult uploadFile(MultipartFile file) throws Exception - { - try - { + public AjaxResult uploadFile(MultipartFile file) throws Exception { + try { + // 上传文件路径 - String filePath = RuoYiConfig.getUploadPath(); +// String filePath = RuoYiConfig.getUploadPath(); // 上传并返回新文件名称 - String fileName = FileUploadUtils.upload(filePath, file); - String url = serverConfig.getUrl() + fileName; +// String fileName = FileUploadUtils.upload(filePath, file); + + + String fileName = cosUtils.upload(file); + String url = serverConfig.getUrl() + "/common/file?fileName=" + fileName; AjaxResult ajax = AjaxResult.success(); ajax.put("fileName", fileName); ajax.put("url", url); return ajax; - } - catch (Exception e) - { + } catch (Exception e) { return AjaxResult.error(e.getMessage()); } } + + /** + * 通用文件获取请求 + */ + @GetMapping("/common/file") + public void uploadFile(@RequestParam(required = true) String fileName, HttpServletResponse response) throws Exception { + + cosUtils.getFile(fileName, response); + } + /** * 本地资源通用下载 */ @GetMapping("/common/download/resource") public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) - throws Exception - { - try - { - if (!FileUtils.checkAllowDownload(resource)) - { + throws Exception { + try { + if (!FileUtils.checkAllowDownload(resource)) { throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource)); } // 本地资源路径 @@ -109,9 +116,7 @@ public class CommonController response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, downloadName); FileUtils.writeBytes(downloadPath, response.getOutputStream()); - } - catch (Exception e) - { + } catch (Exception e) { log.error("下载文件失败", e); } } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java index c2f3f2279..295d04347 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java @@ -1,6 +1,9 @@ package com.ruoyi.web.controller.system; import java.io.IOException; + +import com.ruoyi.common.utils.file.CosUtils; +import com.ruoyi.framework.config.ServerConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -38,6 +41,13 @@ public class SysProfileController extends BaseController @Autowired private TokenService tokenService; + + @Autowired + private CosUtils cosUtils; + + @Autowired + private ServerConfig serverConfig; + /** * 个人信息 */ @@ -111,7 +121,11 @@ public class SysProfileController extends BaseController if (!file.isEmpty()) { LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); - String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file); + + String fileName = cosUtils.upload(file); + String avatar = "/common/file?fileName=" + fileName; + +// String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file); if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) { AjaxResult ajax = AjaxResult.success(); diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 18315bf5a..7c5265f06 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -78,8 +78,9 @@ token: # 令牌密钥 secret: abcdefghijklmnopqrstuvwxyz # 令牌有效期(默认30分钟) - expireTime: 30 - +# expireTime: 30 + expireTime: 300 + # MyBatis配置 # https://baomidou.com/config/ mybatis-plus: diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 8d4f57b4e..f9b940f83 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -136,6 +136,14 @@ lombok + + + com.qcloud + cos_api + ${cos-java.version} + + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/CosConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/CosConfig.java new file mode 100644 index 000000000..bef9c56b9 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/CosConfig.java @@ -0,0 +1,39 @@ +package com.ruoyi.common.config; + +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.region.Region; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author tottimctj + * @since 2020-12-28 + */ +@Configuration +public class CosConfig { + + + @Bean(name = "cosCredentials") + public COSCredentials cosCredentials() { + // 1 初始化用户身份信息(secretId, secretKey)。 + String secretId = "AKIDovztW8x0DpQnunSw2uXUDhw3IH0fQC75"; + String secretKey = "rCH8tOfjX5XjegYBXfkZWc2E75nJq9Dx"; + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); + return cred; + } + + + + @Bean(name = "cosClientConfig") + public ClientConfig cosClientConfig() { + // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 + // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。 + Region region = new Region("ap-beijing"); + ClientConfig clientConfig = new ClientConfig(region); + return clientConfig; + } + +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/CosUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/CosUtils.java new file mode 100644 index 000000000..b3e08a7f9 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/CosUtils.java @@ -0,0 +1,108 @@ +package com.ruoyi.common.utils.file; + +import cn.hutool.core.util.ReUtil; +import cn.hutool.core.util.StrUtil; +import com.qcloud.cos.COSClient; +import com.qcloud.cos.ClientConfig; +import com.qcloud.cos.auth.COSCredentials; +import com.qcloud.cos.model.*; +import com.qcloud.cos.transfer.Download; +import com.ruoyi.common.utils.uuid.UUID; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Objects; +import java.util.Optional; + +import static cn.hutool.core.util.IdUtil.fastSimpleUUID; + +/** + * @author tottimctj + * @since 2020-12-28 + */ + +@Component +public class CosUtils { + + + @Autowired + COSCredentials cosCredentials; + + @Autowired + ClientConfig clientConfig; + + + // 指定要上传到的存储桶 + + final String bucketName = "winery-1257413599"; + String SPECIAL_CHARACTERS = "[`~! @#$%^&*()+=|{}':;',//[//]<>/?~!@#¥%……&*()_——+|{}【】‘;:”“’。,、?]"; + + public String upload(MultipartFile file) { + + + // 指定要上传到 COS 上对象键 + String key = ReUtil.replaceAll(StrUtil.trim(fastSimpleUUID() + Optional.of(file.getOriginalFilename()).orElse(StrUtil.EMPTY)), SPECIAL_CHARACTERS, StrUtil.EMPTY); + // 生成 cos 客户端。 + COSClient cosClient = new COSClient(cosCredentials, clientConfig); + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(file.getContentType()); + metadata.setContentLength(file.getSize()); + file.getContentType(); + try { + PutObjectResult putObjectResult = cosClient.putObject(bucketName, key, file.getInputStream(), metadata); + file.getInputStream().close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + + cosClient.shutdown(); + } + return key; + + } + + public void getFile(String fileName, HttpServletResponse response) { + + COSClient cosClient = new COSClient(cosCredentials, clientConfig); + GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileName); + // 限流使用的单位是bit/s, 这里设置下载带宽限制为 10MB/s + getObjectRequest.setTrafficLimit(80 * 1024 * 1024); + + COSObject cosObject = cosClient.getObject(getObjectRequest); + + response.setContentType(cosObject.getObjectMetadata().getContentType()); + response.setContentLengthLong(cosObject.getObjectMetadata().getContentLength()); + OutputStream os = null; + try { + os = response.getOutputStream(); + while (true) { + int line = cosObject.getObjectContent().read(); + if (line == -1) { + break; + } else { + os.write(line); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // 关闭输入流 + cosClient.shutdown(); + } + + + +} diff --git a/ruoyi-framework/pom.xml b/ruoyi-framework/pom.xml index 969197a61..24452726b 100644 --- a/ruoyi-framework/pom.xml +++ b/ruoyi-framework/pom.xml @@ -19,15 +19,15 @@ - + org.springframework.boot spring-boot-starter-web - - - spring-boot-starter-tomcat - org.springframework.boot - - + + + spring-boot-starter-tomcat + org.springframework.boot + + @@ -37,10 +37,10 @@ - - - - + + + + @@ -79,6 +79,7 @@ ruoyi-system + \ No newline at end of file diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index ce58c2148..c563d8081 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -107,6 +107,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter ).permitAll() .antMatchers("/profile/**").anonymous() .antMatchers("/common/download**").anonymous() + .antMatchers("/common/file**").anonymous() .antMatchers("/common/download/resource**").anonymous() .antMatchers("/swagger-ui.html").anonymous() .antMatchers("/swagger-resources/**").anonymous() diff --git a/ruoyi-ui/src/components/UploadImage/index.vue b/ruoyi-ui/src/components/UploadImage/index.vue index 3941006a2..0be6c928d 100644 --- a/ruoyi-ui/src/components/UploadImage/index.vue +++ b/ruoyi-ui/src/components/UploadImage/index.vue @@ -1,68 +1,74 @@ - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + diff --git a/ruoyi-ui/src/components/UploadImageMultiple/index.vue b/ruoyi-ui/src/components/UploadImageMultiple/index.vue new file mode 100644 index 000000000..9b9e7e0d8 --- /dev/null +++ b/ruoyi-ui/src/components/UploadImageMultiple/index.vue @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + 点击上传 + 只能上传jpg/png文件,且不超过500kb + + + + + + + diff --git a/ruoyi-ui/src/utils/ruoyi.js b/ruoyi-ui/src/utils/ruoyi.js index 549d284cb..81d436f55 100644 --- a/ruoyi-ui/src/utils/ruoyi.js +++ b/ruoyi-ui/src/utils/ruoyi.js @@ -98,7 +98,8 @@ export function selectDictLabels(datas, value, separator) { // 通用下载方法 export function download(fileName) { - window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true; + // window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true; + window.location.href = baseURL + "/common/file?fileName=" + encodeURI(fileName) } // 字符串格式化(%s ) diff --git a/ruoyi-ui/src/views/winery/winery_goods/index.vue b/ruoyi-ui/src/views/winery/winery_goods/index.vue index 72a028645..2abbab800 100644 --- a/ruoyi-ui/src/views/winery/winery_goods/index.vue +++ b/ruoyi-ui/src/views/winery/winery_goods/index.vue @@ -21,21 +21,21 @@ - + - + - + 新增 + >新增 + 修改 + >修改 + 删除 + >删除 + 导出 + >导出 + - + - + - - - - - - - - + + + + + + + + 修改 + >修改 + 删除 + >删除 + @@ -134,30 +140,34 @@ - + - + - + - + - - + + - - + + + + + - - + + + - +