From 30c8d9c0effbe3a0924906060c0327f00ef504e7 Mon Sep 17 00:00:00 2001 From: yupeng <roc__yu@163.com> Date: 星期三, 05 三月 2025 14:28:23 +0800 Subject: [PATCH] feat: 新增文件表、新的文件上传接口 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java | 185 +++++++++++------ ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysFileMapper.java | 16 + ruoyi-admin/src/main/resources/application-test.yml | 4 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java | 124 +++++++++++ ruoyi-system/src/main/java/com/ruoyi/system/model/TFile.java | 81 ++++++++ ruoyi-admin/src/main/java/com/ruoyi/web/core/config/FileUploaderConfig.java | 67 ++++++ ruoyi-common/src/main/java/com/ruoyi/common/config/FileUploadConfig.java | 2 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysFileServiceImpl.java | 42 ++++ ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java | 6 ruoyi-system/src/main/java/com/ruoyi/system/service/SysFileService.java | 17 + ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/SysFileController.java | 22 ++ 11 files changed, 494 insertions(+), 72 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java index 0702deb..dad3be4 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/COSController.java @@ -1,19 +1,31 @@ package com.ruoyi.web.controller.api; +import com.alibaba.fastjson2.JSON; +import com.ruoyi.common.config.FileUploadConfig; import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.framework.web.service.TokenService; +import com.ruoyi.system.model.TFile; +import com.ruoyi.system.service.SysFileService; +import com.ruoyi.system.service.impl.SysFileServiceImpl; 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 lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.OutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URLEncoder; /** * @author HJL @@ -23,11 +35,113 @@ @RestController @RequestMapping("/cos") @Api(tags = "公共-文件上传") +@Slf4j public class COSController { @Resource private TencentCosUtil tencentCosUtil; + @Autowired + SysFileService sysFileService; + + @Autowired + FileUploadConfig fileUploadConfig; + + @Autowired + TokenService tokenService; + + public String getLocalUrlPrefix(){ + ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()); + if (servletRequestAttributes==null || servletRequestAttributes.getRequest()==null){ + return fileUploadConfig.getFileUrlPrefix(); + } + HttpServletRequest request = servletRequestAttributes.getRequest(); + StringBuffer url = new StringBuffer(); + url.append(request.getScheme()).append("://") + .append(request.getServerName()) + .append((request.getServerPort() == 80 ? "" : ":" + request.getServerPort())) + .append(request.getContextPath()); + return url.toString(); + } + + public String getLocalFileUrlPrefix(String fileId){ + String token = tokenService.getLoginUser().getToken(); + StringBuffer url = new StringBuffer(); + url.append(getLocalUrlPrefix()) + .append("/cos/get/").append(fileId).append("&s=").append(URLEncoder.encode(token)) + ; + return url.toString(); + } + + + + + + + public static void failResponse(String message){ + ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()); + HttpServletResponse response = servletRequestAttributes.getResponse(); + String failResult = JSON.toJSONString(R.fail(message)); + //设置编码格式 + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json;charset=UTF-8"); + PrintWriter pw = null; + try { + pw = response.getWriter(); + pw.write(failResult); + pw.flush(); + } catch (IOException e) { + log.error("io异常"); + }finally { + if (pw!=null) { + pw.close(); + } + } + + } + + + @GetMapping("get/{fileId}") + public void get(@PathVariable("fileId") String fileid,@RequestParam("s") String s){ + if (StringUtils.isEmpty(fileid)){ + failResponse("文件ID不能为空"); + return; + } + if (StringUtils.isEmpty(s)){ + failResponse("token不能为空"); + return; + } + TFile file = sysFileService.getById(fileid); + if (file==null){ + failResponse("图片不存在"); + return; + } + tencentCosUtil.download(file); + } + + /** + * + * @param file + * @param folder 上传到cos的文件目录:如/contract/ + * @return + */ + @PostMapping("/uploadnew") + @ApiOperation(value = "文件上传,带上传目录,返回文件ID", tags = "公共-文件上传") + @ApiImplicitParams({ + @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) + }) + public R<TFile> upload(@RequestParam("file") MultipartFile file, @RequestParam("folder") String folder) { + TFile tFile = tencentCosUtil.upload(file,folder); + tFile.setFileUrl(getLocalFileUrlPrefix(tFile.getId())); + return R.ok(tFile); + } + + /** + * + * @param file + * @param + * @return + */ @PostMapping("/upload") @ApiOperation(value = "文件上传", tags = "公共-文件上传") @ApiImplicitParams({ diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/SysFileController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/SysFileController.java new file mode 100644 index 0000000..b213e84 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/SysFileController.java @@ -0,0 +1,22 @@ +package com.ruoyi.web.controller.api; + + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * <p> + * 文件列表 前端控制器 + * </p> + * + * @author yupeng + * @since 2025-03-05 + */ +@RestController +@RequestMapping("/sys-file") +public class SysFileController { + + + +} + diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java index 1d02134..d74cf21 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/TencentCosUtil.java @@ -1,20 +1,23 @@ package com.ruoyi.web.controller.tool; +import cn.hutool.core.date.DateUtil; 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.HttpProtocol; import com.qcloud.cos.model.COSObject; -import com.qcloud.cos.model.GetObjectRequest; import com.qcloud.cos.model.ObjectMetadata; import com.qcloud.cos.model.PutObjectResult; import com.qcloud.cos.region.Region; -import com.qcloud.cos.utils.IOUtils; import com.ruoyi.common.utils.WebUtils; -import org.springframework.beans.factory.annotation.Value; +import com.ruoyi.system.model.TFile; +import com.ruoyi.system.service.SysFileService; +import com.ruoyi.system.service.impl.SysFileServiceImpl; +import com.ruoyi.web.core.config.FileUploaderConfig; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @@ -25,44 +28,17 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.Base64; +import java.util.Date; import java.util.UUID; + +import static cn.hutool.core.date.DatePattern.NORM_DATE_FORMAT; /** * @author HJL */ @Component +@Slf4j 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实例 @@ -70,16 +46,65 @@ * * @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); + + @Autowired + COSClient cosClient; + + @Autowired + FileUploaderConfig cosConfig; + + @Autowired + SysFileService sysFileService; + + + /** + * 上传文件,并存入sys_file,返回文件的主键ID + * @param file + * @param folder + * @return + */ + public TFile upload(MultipartFile file, String folder){ + try { + // 获取上传的文件的输入流 + InputStream inputStream = file.getInputStream(); + // 避免文件覆盖,获取文件的原始名称,如123.jpg,然后通过截取获得文件的后缀,也就是文件的类型 + + String originalFilename = file.getOriginalFilename(); + + //获取文件的类型 + String fileType = originalFilename.substring(originalFilename.lastIndexOf(".")); + //使用UUID工具 创建唯一名称,放置文件重名被覆盖,在拼接上上命令获取的文件类型 + String fileName = UUID.randomUUID() + fileType; + + String name = (StringUtils.isNotEmpty(folder)? + folder+"/" + :"/default/") + DateUtil.format(new Date(),NORM_DATE_FORMAT)+"/"+fileName; + // 指定文件上传到 COS 上的路径,即对象键。最终文件会传到存储桶名字中的images文件夹下的fileName名字 + String key = cosConfig.getLocation()+"/" + fileName; + // 创建上传Object的Metadata + ObjectMetadata objectMetadata = new ObjectMetadata(); + // - 使用输入流存储,需要设置请求长度 + objectMetadata.setContentLength(inputStream.available()); + // - 设置缓存 + objectMetadata.setCacheControl("no-cache"); + // - 设置Content-Type + objectMetadata.setContentType(fileType); + //上传文件 + cosClient.putObject(cosConfig.getBucketName(), key, inputStream, objectMetadata); + TFile tFile = new TFile(); + tFile.setFileName(name); + tFile.setRealName(originalFilename); + tFile.setFileType(fileType); + tFile.setUrl(cosConfig.getRootSrc()+key); + tFile.setContentType(file.getContentType()); + tFile.setFileSize(file.getSize()); + sysFileService.save(tFile); + return tFile; + } catch (Exception e) { + log.error("上传文件发生异常",e); + // 发生IO异常、COS连接异常等,返回空 + return null; + } } /** @@ -99,7 +124,7 @@ //使用UUID工具 创建唯一名称,放置文件重名被覆盖,在拼接上上命令获取的文件类型 String fileName = UUID.randomUUID() + fileType; // 指定文件上传到 COS 上的路径,即对象键。最终文件会传到存储桶名字中的images文件夹下的fileName名字 - String key = location+"/" + fileName; + String key = cosConfig.getLocation()+"/" + fileName; // 创建上传Object的Metadata ObjectMetadata objectMetadata = new ObjectMetadata(); // - 使用输入流存储,需要设置请求长度 @@ -109,14 +134,13 @@ // - 设置Content-Type objectMetadata.setContentType(fileType); //上传文件 - PutObjectResult putResult = getCosClient().putObject(bucketName, key, inputStream, objectMetadata); + PutObjectResult putResult = cosClient.putObject(cosConfig.getBucketName(), key, inputStream, objectMetadata); // 创建文件的网络访问路径 - String url = rootSrc + key; - //关闭 cosClient,并释放 HTTP 连接的后台管理线程 - getCosClient().shutdown(); + String url = cosConfig.getRootSrc() + key; return url; } catch (Exception e) { e.printStackTrace(); + log.error("上传文件发生异常",e); // 发生IO异常、COS连接异常等,返回空 return null; } @@ -129,20 +153,20 @@ */ public void downLoadFile(String file) { HttpServletResponse response = WebUtils.response(); - String replace = file.replace(rootSrc, ""); + String replace = file.replace(cosConfig.getRootSrc(), ""); response.setHeader("Access-Control-Expose-Headers","File-Type"); COSCredentials cred = new BasicCOSCredentials( - secretId, - secretKey); + cosConfig.getSecretId(), + cosConfig.getSecretKey()); // 2.1 设置存储桶的地域(上文获得) - Region region = new Region(bucketAddr); + Region region = new Region(cosConfig.getBucketAddr()); 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(); + InputStream inputStream = cosClient.getObject(cosConfig.getBucketName(), replace).getObjectContent(); ServletOutputStream outputStream = response.getOutputStream(); // 6. 处理输入流,例如读取内容或保存到本地文件 // 这里仅作示例,实际应用中需要根据需求处理输入流 @@ -154,26 +178,24 @@ } } catch (Exception e) { e.printStackTrace(); - } finally { - // 7. 关闭输入流 - cosClient.shutdown(); + log.error("下载文件发生异常",e); } } public String downLoadFileImg(String file) { byte[] data = null; - String replace = file.replace(rootSrc, ""); + String replace = file.replace(cosConfig.getRootSrc(), ""); COSCredentials cred = new BasicCOSCredentials( - secretId, - secretKey); + cosConfig.getSecretId(), + cosConfig.getSecretKey()); // 2.1 设置存储桶的地域(上文获得) - Region region = new Region(bucketAddr); + Region region = new Region(cosConfig.getBucketAddr()); 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(); + InputStream inputStream = cosClient.getObject(cosConfig.getBucketName(), replace).getObjectContent(); ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); // 6. 处理输入流,例如读取内容或保存到本地文件 byte[] buffer = new byte[1024]; @@ -185,10 +207,41 @@ data = swapStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); + log.error("下载图片发生异常",e); } finally { - // 7. 关闭输入流 - cosClient.shutdown(); } return Base64.getEncoder().encodeToString(data); } + + public void download(TFile file) { + HttpServletResponse response = WebUtils.response(); + COSCredentials cred = new BasicCOSCredentials( + cosConfig.getSecretId(), + cosConfig.getSecretKey()); + // 2.1 设置存储桶的地域(上文获得) + Region region = new Region(cosConfig.getBucketAddr()); + ClientConfig clientConfig = new ClientConfig(region); + // 2.2 使用https协议传输 + clientConfig.setHttpProtocol(HttpProtocol.https); + COSClient cosClient = new COSClient(cred, clientConfig); + // 5. 下载文件并获取输入流 + COSObject object = cosClient.getObject(cosConfig.getBucketName(), file.getUrl()); + try ( + InputStream is = object.getObjectContent(); + OutputStream os = response.getOutputStream() + ) { + response.setContentType(file.getContentType() + ";charset=utf-8"); + String filename = new String(file.getRealName().getBytes("UTF-8"), "ISO-8859-1"); + response.addHeader("Content-Disposition", "attachment;filename=" + filename); + response.addHeader("Content-Length", "" + file.getFileSize()); + int len = 0; + byte[] buffer = new byte[2048]; + while ((len = is.read(buffer)) > 0) { + os.write(buffer, 0, len); + } + os.flush(); + } catch (IOException e) { + log.error("读取cos图片发生异常", e); + } + } } \ No newline at end of file diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/FileUploaderConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/FileUploaderConfig.java new file mode 100644 index 0000000..d87ab0d --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/FileUploaderConfig.java @@ -0,0 +1,67 @@ +package com.ruoyi.web.core.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.http.HttpProtocol; +import com.qcloud.cos.region.Region; +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Data +public class FileUploaderConfig { + + /** + * 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; + + @Value("${file.url.prefix}") + private String fileUrlPrefix; + + + @Bean + public COSClient cosClient() { + // 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); + } + + +} diff --git a/ruoyi-admin/src/main/resources/application-test.yml b/ruoyi-admin/src/main/resources/application-test.yml index 54159c4..40b121a 100644 --- a/ruoyi-admin/src/main/resources/application-test.yml +++ b/ruoyi-admin/src/main/resources/application-test.yml @@ -18,7 +18,7 @@ # 开发环境配置 server: # 服务器的HTTP端口,默认为8080 - port: 8081 + port: 8080 servlet: # 应用的访问路径 context-path: / @@ -199,6 +199,8 @@ qrLocation: /file/qrCode/ accessPath: /file/ allowExt: .jpg|.png|.gif|.jpeg|.doc|.docx|.apk|.MP4|.mp4|.pdf|.PDF + url: + prefix: http://localhost:${server.port}${server.servlet.context-path} wx: conf: appId: wxe91f1af7638aa5dd diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/FileUploadConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/FileUploadConfig.java index 3a8b0ed..32c0b2a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/config/FileUploadConfig.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/FileUploadConfig.java @@ -18,4 +18,6 @@ private String allowExt; private String location; private String qrLocation; + private String fileUrlPrefix; + } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java index a7a640b..90ab01b 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java @@ -204,6 +204,12 @@ refreshToken(loginUser); } } + + + public boolean verifyToken(String token) + { + return true; + } /** * 小程序验证令牌有效期,相差不足20分钟,自动刷新缓存 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysFileMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysFileMapper.java new file mode 100644 index 0000000..f5e0298 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysFileMapper.java @@ -0,0 +1,16 @@ +package com.ruoyi.system.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.ruoyi.system.model.TFile; + +/** + * <p> + * 文件列表 Mapper 接口 + * </p> + * + * @author yupeng + * @since 2025-03-05 + */ +public interface SysFileMapper extends BaseMapper<TFile> { + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/model/TFile.java b/ruoyi-system/src/main/java/com/ruoyi/system/model/TFile.java new file mode 100644 index 0000000..93be19a --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/model/TFile.java @@ -0,0 +1,81 @@ +package com.ruoyi.system.model; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * <p> + * 文件列表 + * </p> + * + * @author yupeng + * @since 2025-03-05 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@TableName("t_file") +@ApiModel(value="t_file对象", description="文件列表") +public class TFile implements Serializable { + + private static final long serialVersionUID = 1L; + + @TableId("id") + private String id; + + @ApiModelProperty(value = "文件分类,0.用户文件 1.钉钉文件") + @TableField("type") + private Integer type; + + @TableField("content_type") + private String contentType; + + @ApiModelProperty(value = "文件类型(后缀)") + @TableField("file_type") + private String fileType; + + @ApiModelProperty(value = "文件真实名称") + @TableField("real_name") + private String realName; + + @ApiModelProperty(value = "文件名称") + @TableField("file_name") + private String fileName; + + @ApiModelProperty(value = "文件大小") + @TableField("file_size") + private Long fileSize; + + @TableField("create_time") + private LocalDateTime createTime; + + @ApiModelProperty(value = "地址路径或者url") + @TableField("url") + private String url; + + @ApiModelProperty(value = "上传人ID") + @TableField("creator_id") + private String creatorId; + + @ApiModelProperty(value = "是否需要校验权限") + @TableField("need_auth") + private Boolean needAuth; + + @ApiModelProperty(value = "是否有效") + @TableField("valid") + private Boolean valid; + /** + * 返回文件对象时自动组装文件访问路径 + */ + @TableField(exist = false) + @ApiModelProperty("文件访问路径") + private String fileUrl; + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysFileService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysFileService.java new file mode 100644 index 0000000..63b9bce --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysFileService.java @@ -0,0 +1,17 @@ +package com.ruoyi.system.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.ruoyi.system.model.TFile; + +/** + * <p> + * 文件列表 服务类 + * </p> + * + * @author yupeng + * @since 2025-03-05 + */ +public interface SysFileService extends IService<TFile> { + + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysFileServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysFileServiceImpl.java new file mode 100644 index 0000000..5cd94fd --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysFileServiceImpl.java @@ -0,0 +1,42 @@ +package com.ruoyi.system.service.impl; + +import com.alibaba.fastjson2.JSON; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.ruoyi.common.config.FileUploadConfig; +import com.ruoyi.common.core.domain.R; +import com.ruoyi.system.mapper.SysFileMapper; +import com.ruoyi.system.model.TFile; +import com.ruoyi.system.service.SysFileService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.token.TokenService; +import org.springframework.stereotype.Service; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URLEncoder; + +/** + * <p> + * 文件列表 服务实现类 + * </p> + * + * @author yupeng + * @since 2025-03-05 + */ +@Service +@Slf4j +public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, TFile> implements SysFileService { + + @Autowired + FileUploadConfig fileUploadConfig; + + + + + +} -- Gitblit v1.7.1