当前位置:网站首页>文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
2022-07-31 05:48:00 【追风筝~】
系列文章:
文件 - 01 上传附件到服务器并保存信息到MySQL
文件 - 02 上传临时文件到服务器
文件 - 03 根据文件id获取下载链接
文件 - 04 根据文件下载链接下载文件
文件 - 05 根据文件Id下载文件
文件 - 06 根据文件id/图片id回显给前端
附件表:
CREATE TABLE `t_upload_file` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fileId` varchar(50) NOT NULL COMMENT '文件id',
`fileName` varchar(50) NOT NULL COMMENT '文件名',
`fileType` varchar(100) NOT NULL COMMENT '文件类型',
`fileSize` int(10) NOT NULL COMMENT '文件大小bytes',
`filePathSuffix` varchar(60) NOT NULL COMMENT '文件存储路径',
`fileUploader` varchar(32) DEFAULT NULL,
`uploadTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '文件上传时间',
`lastUsedTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '文件最近使用时间',
`neverExpire` tinyint(1) NOT NULL DEFAULT '1' COMMENT '文件是否永不过期,当为1时,则忽略expireTime',
`expireTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '文件过期时间',
PRIMARY KEY (`id`),
UNIQUE KEY `fileId` (`fileId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
1. FileController
@RestController
@ResponseBody
@ResponseResult
@Slf4j
@RequestMapping("/ngsoc/PORTAL/api/v1")
public class FileController {
@ApiOperation(value = "根据fileIds批量删除文件", notes = "根据fileIds批量删除文件及文件信息", httpMethod = "DELETE")
@DeleteMapping(value = "/files")
public void deleteFiles(
@ApiParam(value = "文件名列表", required = true) @RequestBody List<String> fileIds
) {
fileService.deleteFileByIds(fileIds);
}
}
2. FileServiceImpl
public interface IFileService {
/** * 根据文件id列表批量删除文件 * * @param fileIds 文件id列表 */
void deleteFileByIds(List<String> fileIds);
}
@Controller
@Slf4j
public class FileServiceImpl implements IFileService {
@Autowired
private IFileMapper fileDao;
@Autowired
private FileConfig fileConfig;
@Autowired
private RedisTemplate<String, String> stringRedisTemplate;
@Setter(onMethod_ = @Autowired)
private FileIdAuthorityServiceImpl fileIdAuthorityServiceImpl;
@Override
public void deleteFileByIds(List<String> fileIds) {
for (String fileId : fileIds) {
deleteFileById(fileId, true);
}
}
@Override
public void deleteFileById(String fileId, boolean checkAuth) {
// 删除文件 1. getFileInfo --> path --> delete file
// 2. clear mysql info
// 3. redis will expire automatically
// 4. 如果期间有根据token/id下载 就会出错
FileInfo fileInfo = this.getFileInfo(fileId);
if (fileInfo == null) {
return;
}
if (checkAuth) {
fileIdAuthorityServiceImpl.assertThisUserHaveAuthority(fileInfo);
}
String pathSuffix = fileInfo.getFilePathSuffix();
String storePath = fileConfig.getSavePath().concat(pathSuffix);
// 刪除服务器保存的文件
// 如果文件已经不存在,返回True
boolean delSucceed = FileUtil.del(storePath);
if (delSucceed) {
// 删除文件信息
fileDao.deleteFileInfo(fileId);
}
}
}
3. IFileMapper
@Mapper
@Repository
public interface IFileMapper {
/** * 根据fileId删除文件信息 * * @param fileId 文件id */
void deleteFileInfo(@Param("fileId") String fileId);
}
<delete id="deleteFileInfo" parameterType="String">
delete
from t_upload_file
where fileId = #{fileId} and <include refid="unexpiredFilter"/>
</delete>
边栏推荐
- nohup原理
- 运行 npm 会弹出询问 “你要如何打开这个文件?“
- Project exercise - memorandum (add, delete, modify, check)
- Moment.js常用方法
- ls的用法
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- 2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
- 深度解析 z-index
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 银河麒麟高级服务器v10 sp1 手动加载Raid卡驱动
猜你喜欢
简单谈谈Feign
数据库概论 - MySQL的简单介绍
Automatic translation software - batch batch automatic translation software recommendation
【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
Conditional statements of shell (test, if, case)
SSH远程管理
小实战项目之——吃货联盟订餐系统
服务器硬件及RAID配置实战
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
Log4net 思维导图
随机推荐
服务器硬件及RAID配置实战
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
批量翻译软件免费【2022最新版】
【Star项目】小帽飞机大战(八)
磁盘管理与文件系统
浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
2022.7.29 数组
How to use repeating-linear-gradient
Database Principles Homework 2 — JMU
自动翻译软件-批量批量自动翻译软件推荐
nohup principle
快速傅里叶变换(FFT)
03-SDRAM:写操作(突发)
进程和计划任务管理
Third-party library-store
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
TypeScript基本类型
外贸网站优化-外贸网站优化教程-外贸网站优化软件
《白帽子说Web安全》思维导图