想到的就是监听步骤如下:
1.配置web.xml
<listener> <listener-class>com.listener.InitListener</listener-class> </listener>
2.编写InitListener类
1 package com.listener; 2 3 import java.io.File; 4 5 import javax.servlet.ServletContextEvent; 6 import javax.servlet.ServletContextListener; 7 8 import com.seegot.util.PropertyUtil; 9 10 public class InitListener implements ServletContextListener { 11 12 @Override 13 public void contextDestroyed(ServletContextEvent arg0) { 14 // TODO Auto-generated method stub 15 16 } 17 18 @Override 19 public void contextInitialized(ServletContextEvent arg0) { 20 // TODO Auto-generated method stub 21 System.out.println("================>[ServletContextListener]自动加载启动开始."); 22 String resourceFilesPath = PropertyUtil.getProperty("tempZipPath"); 23 clearFiles(resourceFilesPath); 24 } 25 // 删除文件和目录 26 private static boolean clearFiles(String workspaceRootPath) { 27 File file = new File(workspaceRootPath); 28 if (file.exists()) { 29 deleteFile(file); 30 } 31 // resources 文件夹被删除后需新建 32 if (!file.exists() && workspaceRootPath.endsWith("resources")) { 33 return file.mkdir(); 34 } else if (!file.exists()) { 35 return true; 36 } 37 return false; 38 } 39 40 private static boolean deleteFile(File file) { 41 if (file.isDirectory()) { 42 File[] files = file.listFiles(); 43 for (int i = 0; i < files.length; i++) { 44 deleteFile(files[i]); 45 } 46 } 47 return file.delete(); 48 } 49 }