From f59c253c73b3bfeb605e043688a76f8946e7349e Mon Sep 17 00:00:00 2001 From: xuhy <3313886187@qq.com> Date: 星期三, 12 二月 2025 13:49:52 +0800 Subject: [PATCH] 文件上传 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/interceptor/MybatisInterceptor.java | 4 ruoyi-applet/src/main/resources/application-prod.yml | 8 + ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/COSController.java | 55 +++++++++++ ruoyi-system/src/main/resources/mapper/system/TContractMapper.xml | 2 ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java | 187 +++++++++++++++++++++++++++++++++++++ ruoyi-applet/src/main/resources/application-test.yml | 8 + 6 files changed, 261 insertions(+), 3 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/interceptor/MybatisInterceptor.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/interceptor/MybatisInterceptor.java index d739523..816f77a 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/interceptor/MybatisInterceptor.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/interceptor/MybatisInterceptor.java @@ -54,7 +54,7 @@ //注入创建时间 if ("createTime".equals(field.getName())) { field.setAccessible(true); - field.set(parameter, LocalDateTime.now()); + field.set(parameter, new Date()); field.setAccessible(false); } } catch (Exception e) { @@ -77,7 +77,7 @@ } if ("updateTime".equals(field.getName())) { field.setAccessible(true); - field.set(parameter, LocalDateTime.now()); + field.set(parameter, new Date()); field.setAccessible(false); } } catch (Exception e) { diff --git a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/COSController.java b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/COSController.java new file mode 100644 index 0000000..3d7d593 --- /dev/null +++ b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/COSController.java @@ -0,0 +1,55 @@ +package com.ruoyi.web.controller.api; + +import com.ruoyi.common.core.domain.R; +import com.ruoyi.web.controller.tool.TencentCosUtil; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; + +/** + * @author HJL + * @version 1.0 + * @since 2024-05-30 9:27 + */ +@RestController +@RequestMapping("/cos") +@Api(tags = "公共-文件上传") +public class COSController { + + @Resource + private TencentCosUtil tencentCosUtil; + + @PostMapping("/upload") + @ApiOperation(value = "文件上传", tags = "公共-文件上传") + @ApiImplicitParams({ + @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) + }) + public R<String> upload(@RequestParam("file") MultipartFile file) { + String url = tencentCosUtil.upLoadFile(file); + return R.ok(url, url); + } + @PostMapping("/downloadImg") + @ApiOperation(value = "文件下载", tags = "公共-文件下载") + @ApiImplicitParams({ + @ApiImplicitParam(value = "文件url", name = "url", dataType = "String", required = true) + }) + public String downloadImg(@RequestParam("url") String url) { + return tencentCosUtil.downLoadFileImg(url); + } + @PostMapping("/download") + @ApiOperation(value = "文件下载", tags = "公共-文件下载") + @ApiImplicitParams({ + @ApiImplicitParam(value = "文件url", name = "url", dataType = "String", required = true) + }) + public void download(@RequestParam("url") String url) { + tencentCosUtil.downLoadFile(url); + } +} diff --git a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java new file mode 100644 index 0000000..f86298b --- /dev/null +++ b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java @@ -0,0 +1,187 @@ +package com.ruoyi.web.controller.tool; + +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.http.HttpProtocol; +import com.qcloud.cos.model.ObjectMetadata; +import com.qcloud.cos.model.PutObjectResult; +import com.qcloud.cos.region.Region; +import com.ruoyi.common.utils.WebUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.Base64; +import java.util.UUID; + +/** + * @author HJL + */ +@Component +public class TencentCosUtil { + + /** + * COS的SecretId + */ + @Value("${cos.client.accessKey}") + private String secretId; + /** + * COS的SecretKey + */ + @Value("${cos.client.secretKey}") + private String secretKey; + /** + * 文件上传后访问路径的根路径,后面要最佳文件名字与类型 + */ + @Value("${cos.client.rootSrc}") + private String rootSrc; + /** + * 上传的存储桶的地域 + */ + @Value("${cos.client.bucketAddr}") + private String bucketAddr; + /** + * 存储桶的名字,是自己在存储空间自己创建的,我创建的名字是:qq-test-1303****** + */ + @Value("${cos.client.bucket}") + private String bucketName; + /** + * 文件存放位置 + */ + @Value("${cos.client.location}") + private String location; + + /** + * 1.调用静态方法getCosClient()就会获得COSClient实例 + * 2.本方法根据永久密钥初始化 COSClient的,官方是不推荐,官方推荐使用临时密钥,是可以限制密钥使用权限,创建cred时有些区别 + * + * @return COSClient实例 + */ + private COSClient getCosClient() { + // 1 初始化用户身份信息(secretId, secretKey)。 + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); + // 2.1 设置存储桶的地域(上文获得) + Region region = new Region(bucketAddr); + ClientConfig clientConfig = new ClientConfig(region); + // 2.2 使用https协议传输 + clientConfig.setHttpProtocol(HttpProtocol.https); + // 生成 cos 客户端 + return new COSClient(cred, clientConfig); + } + + /** + * 只要调用静态方法upLoadFile(MultipartFile multipartFile)就可以获取上传后文件的全路径 + * + * @param file + * @return 返回文件的浏览全路径 + */ + public String upLoadFile(MultipartFile file) { + try { + // 获取上传的文件的输入流 + InputStream inputStream = file.getInputStream(); + // 避免文件覆盖,获取文件的原始名称,如123.jpg,然后通过截取获得文件的后缀,也就是文件的类型 + String originalFilename = file.getOriginalFilename(); + //获取文件的类型 + String fileType = originalFilename.substring(originalFilename.lastIndexOf(".")); + //使用UUID工具 创建唯一名称,放置文件重名被覆盖,在拼接上上命令获取的文件类型 + String fileName = UUID.randomUUID() + fileType; + // 指定文件上传到 COS 上的路径,即对象键。最终文件会传到存储桶名字中的images文件夹下的fileName名字 + String key = location+"/" + fileName; + // 创建上传Object的Metadata + ObjectMetadata objectMetadata = new ObjectMetadata(); + // - 使用输入流存储,需要设置请求长度 + objectMetadata.setContentLength(inputStream.available()); + // - 设置缓存 + objectMetadata.setCacheControl("no-cache"); + // - 设置Content-Type + objectMetadata.setContentType(fileType); + //上传文件 + PutObjectResult putResult = getCosClient().putObject(bucketName, key, inputStream, objectMetadata); + // 创建文件的网络访问路径 + String url = rootSrc + key; + //关闭 cosClient,并释放 HTTP 连接的后台管理线程 + getCosClient().shutdown(); + return url; + } catch (Exception e) { + e.printStackTrace(); + // 发生IO异常、COS连接异常等,返回空 + return null; + } + } + + /** + * 下载文件 + * @param file + * @return + */ + public void downLoadFile(String file) { + HttpServletResponse response = WebUtils.response(); + String replace = file.replace(rootSrc, ""); + response.setHeader("Access-Control-Expose-Headers","File-Type"); + COSCredentials cred = new BasicCOSCredentials( + secretId, + secretKey); + // 2.1 设置存储桶的地域(上文获得) + Region region = new Region(bucketAddr); + ClientConfig clientConfig = new ClientConfig(region); + // 2.2 使用https协议传输 + clientConfig.setHttpProtocol(HttpProtocol.https); + COSClient cosClient = new COSClient(cred, clientConfig); + try { + // 5. 下载文件并获取输入流 + InputStream inputStream = cosClient.getObject(bucketName, replace).getObjectContent(); + ServletOutputStream outputStream = response.getOutputStream(); + // 6. 处理输入流,例如读取内容或保存到本地文件 + // 这里仅作示例,实际应用中需要根据需求处理输入流 + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + // 处理读取到的数据 + outputStream.write(buffer, 0, len); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + // 7. 关闭输入流 + cosClient.shutdown(); + } + } + public String downLoadFileImg(String file) { + byte[] data = null; + String replace = file.replace(rootSrc, ""); + COSCredentials cred = new BasicCOSCredentials( + secretId, + secretKey); + // 2.1 设置存储桶的地域(上文获得) + Region region = new Region(bucketAddr); + ClientConfig clientConfig = new ClientConfig(region); + // 2.2 使用https协议传输 + clientConfig.setHttpProtocol(HttpProtocol.https); + COSClient cosClient = new COSClient(cred, clientConfig); + try { + // 5. 下载文件并获取输入流 + InputStream inputStream = cosClient.getObject(bucketName, replace).getObjectContent(); + ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); + // 6. 处理输入流,例如读取内容或保存到本地文件 + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) != -1) { + // 处理读取到的数据 + swapStream.write(buffer, 0, len); + } + data = swapStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + // 7. 关闭输入流 + cosClient.shutdown(); + } + return Base64.getEncoder().encodeToString(data); + } +} \ No newline at end of file diff --git a/ruoyi-applet/src/main/resources/application-prod.yml b/ruoyi-applet/src/main/resources/application-prod.yml index 47a6c8f..b652f22 100644 --- a/ruoyi-applet/src/main/resources/application-prod.yml +++ b/ruoyi-applet/src/main/resources/application-prod.yml @@ -206,3 +206,11 @@ accessKeySecret: 0SRb6XGkciQDPWn2rYqbJtq2qRMDY8 signName: "四川金达通信工程" templateCode: "SMS_293985284" +cos: + client: + accessKey: AKIDCF5EF2c0DE1e5JK8r4EGJF4mNsMgp26x + secretKey: lLl184rUyFOOE0d5KNGC3kmfNsCWk4GU + bucket: xzgttest-1305134071 + bucketAddr: ap-chengdu + rootSrc: https://xzgttest-1305134071.cos.ap-chengdu.myqcloud.com/ + location: xizang diff --git a/ruoyi-applet/src/main/resources/application-test.yml b/ruoyi-applet/src/main/resources/application-test.yml index 355f277..53c6be7 100644 --- a/ruoyi-applet/src/main/resources/application-test.yml +++ b/ruoyi-applet/src/main/resources/application-test.yml @@ -205,3 +205,11 @@ accessKeySecret: 0SRb6XGkciQDPWn2rYqbJtq2qRMDY8 signName: "四川金达通信工程" templateCode: "SMS_293985284" +cos: + client: + accessKey: AKIDCF5EF2c0DE1e5JK8r4EGJF4mNsMgp26x + secretKey: lLl184rUyFOOE0d5KNGC3kmfNsCWk4GU + bucket: xzgttest-1305134071 + bucketAddr: ap-chengdu + rootSrc: https://xzgttest-1305134071.cos.ap-chengdu.myqcloud.com/ + location: xizang diff --git a/ruoyi-system/src/main/resources/mapper/system/TContractMapper.xml b/ruoyi-system/src/main/resources/mapper/system/TContractMapper.xml index 9728d27..eb9f2e8 100644 --- a/ruoyi-system/src/main/resources/mapper/system/TContractMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/TContractMapper.xml @@ -80,7 +80,7 @@ select t1.*,t2.contract_number as contractNumber,t3.resident_name as residentName,t3.phone as phone from t_bill t1 left join t_contract t2 on t1.contract_id = t2.id - left join t_resident t3 on t2.tenant_id = t3.id + left join t_tenant t3 on t2.tenant_id = t3.id where t2.id = #{query.id} and (t1.pay_fees_status = 1 or t1.pay_fees_status = 4) AND t1.disabled = ${@com.ruoyi.common.enums.DisabledEnum@NO.getCode()} -- Gitblit v1.7.1