Commit 80c7d50e authored by yanzg's avatar yanzg

文件处理

parent cc72aa91
...@@ -135,6 +135,11 @@ ...@@ -135,6 +135,11 @@
<artifactId>yzg-util-db</artifactId> <artifactId>yzg-util-db</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.yanzuoguang</groupId>
<artifactId>yzg-util-mq</artifactId>
</dependency>
<dependency> <dependency>
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId> <artifactId>springfox-swagger-ui</artifactId>
......
package com.yanzuoguang.cloud.file;
import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueVo;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 删除目录
*
* @author 颜佐光
*/
@Component
public class YzgFileProcedure implements InitializingBean {
public static final String REMOVE_TEMP_FOLDER = "REMOVE_TEMP_FOLDER";
@Autowired
private MqService mqService;
/**
* 缓存24小时
*/
private MemoryCache<Boolean> folderCache = new MemoryCache<>(24 * 60 * 60);
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
mqService.createQueue(new QueueVo(REMOVE_TEMP_FOLDER));
}
public void removeFolder(String serverFolder) {
if (StringHelper.toBoolean(folderCache.get(serverFolder))) {
return;
}
// 获取当前时间
Date dt = new Date();
Date dtTo = DateHelper.addDay(dt, 2);
String dtToday = DateHelper.getToday(dtTo);
// 获取延迟时间
long time = DateHelper.getDateTime(dtToday).getTime() - dt.getTime();
// 发送删除临时目录的命令
mqService.message(new MessageVo(REMOVE_TEMP_FOLDER, serverFolder, time));
}
}
...@@ -25,6 +25,9 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -25,6 +25,9 @@ public class YzgFileServiceImpl implements YzgFileService {
@Autowired @Autowired
private YzgFileConfig fileConfig; private YzgFileConfig fileConfig;
@Autowired
private YzgFileProcedure procedure;
/** /**
* 压缩文件 * 压缩文件
* *
...@@ -57,6 +60,8 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -57,6 +60,8 @@ public class YzgFileServiceImpl implements YzgFileService {
String serverFolder = String.format("%s/%s", fileConfig.getServerUrl(), tempFolder); String serverFolder = String.format("%s/%s", fileConfig.getServerUrl(), tempFolder);
// 创建服务器路径 // 创建服务器路径
FileHelper.createDirectory(serverFolder); FileHelper.createDirectory(serverFolder);
// 删除目录
procedure.removeFolder(serverFolder);
// 设置保存文件的路径 // 设置保存文件的路径
String serverFile = String.format("%s/%s", serverFolder, saveFileName); String serverFile = String.format("%s/%s", serverFolder, saveFileName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment