DecimalFormat df = new DecimalFormat("0.00");
@Override
public void run() {
//类的绝对路径
URL path = TimerClearFileUtil.class.getResource(TimerClearFileUtil.class.getSimpleName()+ ".class");
String str = path.toString();
int startIndex = str.indexOf("/",0)+1;
int endIndex = str.indexOf("/WEB-INF");
//项目绝对路径
String dirPath = str.substring(startIndex,endIndex);
//项目临时路径(绝对路径)
dirPath +="/"+XproductConstant.TMPDIR_PATH ;
try {
File file =new File("/"+dirPath);
if(file.exists()){
double size = recursionFileList(file);
//超出大小时清理
if(size>XproductConstant.TMPDIR_MAX_SIZE){
file.delete();
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
/**
* 返回文件夹的大小
* 单位:M
* @param f
* @return
* @throws Exception
*/
private double recursionFileList(File f) throws Exception
{
double size = 0;
File flist[] = f.listFiles();
for (int i = 0; flist!=null&&i < flist.length; i++) {
if (flist[i].isDirectory()){
size = size + recursionFileList(flist[i]);
}else{
size = size + ((double)new FileInputStream(flist[i]).available())/1024/1024;
}
}
return Double.parseDouble(df.format(size));
}