@WebListener // 在此注明以下类是监听器
public class SessionListener implements HttpSessionListener {public void sessionCreated(HttpSessionEvent arg0) {
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
// 获取security用户认证信息
String employeeNo = SecurityContextHolder.getContext().getAuthentication().getName();
String path = new StringBuilder().append(CommonConstants.STR_TAS_MSGTASKHOME).append(File.separator)
.append(CommonConstants.STR_TAS_TASK).append(File.separator)
.append(CommonConstants.STR_TAS_TEMPORARY_UPLOAD).append(File.separator).append(employeeNo).append(File.separator).toString();
String employeeNopath = new StringBuilder().append(CommonConstants.STR_TAS_MSGTASKHOME).append(File.separator)
.append(CommonConstants.STR_TAS_TASK).append(File.separator)
.append(CommonConstants.STR_TAS_TEMPORARY_UPLOAD).append(File.separator).toString();
delAllFile(path);
File file = new File(employeeNopath);
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
temp = new File(employeeNopath + tempList[i]);
if (temp.getName().equals(employeeNo)) {
temp.delete();
}
}
}// 删除指定文件夹下所有文件
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}// 删除文件夹
// param folderPath 文件夹完整绝对路径
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
}
详细解决方案
在spring boot 中 ,在session触发器中,当session注销时,删除某个文件夹及文件夹中的文件
热度:34 发布时间:2024-02-06 08:29:09.0
相关解决方案
- Spring MVC开发模式,怎么取得新增的id
- spring 表单对象绑定有关问题 String与Long的转换
- spring+quartz定时器有关问题
- spring @Scope("prototype")注解更新有关问题,寻求帮助
- Spring MVC是不是可以完全取代Struts
- Spring 中 packagesToScan有关问题
- Spring MVC中点击旋钮没反应
- Servlet Session 购物车,该怎么解决
- spring 事务 aop transactionManager,该怎么解决
- Servlet Session 购物车解决办法
- 求SSM分页 struts +spring+mybatis 给小弟我发个学习学习吧 多谢大神们
- spring placeholderConfig的有关问题
- spring 事宜 aop transactionManager
- Spring 和 hibernate如何配置事物
- hibernate与此同时使用多数据源?spring+hibernate
- Spring 动态代理,该怎么解决
- java spring mvc,该怎么解决
- spring mvc 接收对象数组解决办法
- Spring Data 的有关问题
- spring mvc3 使用jackson返回json时候无限循环
- struts+spring+hibernate 开发,在action层调用service层接口,结果替null 注入失败,需高手帮忙
- 生手求教,dwr+struts+spring 页面跳提示框error
- spring IOC 原理解决方法
- session 覆盖的有关问题 求高手
- Could not deserialize session data解决方法
- response.write(session.getAttribute("BranchName")提示异常
- JSP上if(trim(session.getAttribute("grade"))== "0") 多谢!
- uploadify3.1下传 spring+struts2+hibernate框架中取值的有关问题
- spring 事务配置不回滚的有关问题。
- session.invalidate()无效解决办法