对webapp下上传文件夹中的文件按时间排序展示到页面中

来源:互联网 发布:epub drm 破解软件 编辑:程序博客网 时间:2024/05/23 11:32
项目前端是是VUE+element
js的代码
$.ajax({
   type: "POST",
         url: baseURL + 'sys/collectionLog/list',
      data: JSON.stringify(vm.job),
         contentType: "application/json",
      success: function(r){
       console.log(r)
       vm.tableData=r.map.list;
       vm.job.pageTotle=parseInt(r.map.total);
       vm.count=r.map.count;
   }
  });
后台:

/**
  * 直接查询文件展现列表
  * @param params
  * @param request
  * @return
  */
 @SuppressWarnings({ "rawtypes", "unchecked" })
 @RequestMapping("/list")
 public R list(@RequestBody CollectionLog collectionLog, HttpServletRequest request) {
  if(collectionLog.getCurrentPage()==null){ //当前页
   collectionLog.setCurrentPage("1");;
  }
  if(collectionLog.getPageSize()==null){//每页条数
   collectionLog.setPageSize("10");
  }
  String jobName = collectionLog.getJobName();// 转换大写
  if (jobName != null && jobName != "") {// 条件查询的参数
   jobName = jobName.toUpperCase();
  }
  //Query query = new Query(params);
  List list = new ArrayList();
  String logPath = request.getSession().getServletContext().getRealPath("/collectionlog/");
  String filePath = logPath.replace("\\", "/");
  File f = new File(filePath);
  if (!f.exists()) {
   System.out.println(filePath + " not exists");
   return null;
  }
  File fa[] = f.listFiles();
  if(fa.length!=0){
   ArraySort(fa, 0, fa.length - 1);//按照日志文件日期从大到小排序
  }
  int total = fa.length;
  int j=0;
  for (int i = 0; i < fa.length; i++) {
   Map map = new HashMap<>();
   File fs = fa[i];
   long time = fs.lastModified();// 返回文件最后修改时间,是以个long型毫秒数
   String creatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));// 文件创建时间,hh12时制,HH24时制
   String fileName = fs.getName();
   String newJobName = fileName.substring(fileName.indexOf(0) + 1, fileName.indexOf("_")).toUpperCase();// 截取字符串,转成大写
   if (jobName != null && jobName != "") {
    if (newJobName.toUpperCase().contains(jobName)) {
     map.put("fileName", fileName);
     map.put("creatTime", creatTime);
     map.put("newJobName", fileName.substring(fileName.indexOf(0) + 1, fileName.indexOf("_")));
     map.put("num", ++j);
     list.add(map);
     map.remove(fileName);// 不移除key的话后面的value会覆盖,key-value用过销毁
     map.remove(creatTime);
     map.remove(newJobName);
    }
   } else {
    map.put("fileName", fileName);
    map.put("creatTime", creatTime);
    map.put("newJobName", fileName.substring(fileName.indexOf(0) + 1, fileName.indexOf("_")));
    map.put("num", ++j);
    list.add(map);
    map.remove(fileName);// 不移除key的话后面的value会覆盖,key-value用过销毁
    map.remove(creatTime);
    map.remove(newJobName);
   }
  }
  //分页
  int length ;
  if(list.size()>Integer.parseInt(collectionLog.getCurrentPage())*Integer.parseInt(collectionLog.getPageSize())){
   length=Integer.parseInt(collectionLog.getCurrentPage())*Integer.parseInt(collectionLog.getPageSize());
  }else{
   length=list.size();
  }
  List newList = new ArrayList<>();
  for(int i=(Integer.parseInt(collectionLog.getCurrentPage())-1)*Integer.parseInt(collectionLog.getPageSize());i<length;i++){
   newList.add(list.get(i));
  }
  Map<String,Object> map = new HashMap<>();
  map.put("list", newList);
  map.put("count", total);
  map.put("total", list.size());
  return R.ok().put("map", map);
 }
 // 快速排序方法,按日期排序
 private static void ArraySort(File[] fa, int lowIndex, int highIndex) {
  if (lowIndex >= highIndex) {
   return;// 相同表示全部拍完
  }
  int i = lowIndex;
  int j = highIndex;
  long key = fa[i].lastModified();
  long newKey = stringToLong(key);//必须从date转到String在转到long才能比较
  boolean flag = true;//标志位
  while (i != j) {
   if (flag) {
    if (newKey < stringToLong(fa[j].lastModified())) {
     swap(fa, i, j);
     flag = false;
    } else {
     j--;
    }
   } else {
    if (newKey > stringToLong(fa[i].lastModified())) {
     swap(fa, i, j);
     flag = true;
    } else {
     i++;
    }
   }
  }
  ArraySort(fa,lowIndex,j-1);//前半段数组进行排序,直到排完  
  ArraySort(fa,i+1,highIndex);//后半段数组进行排序,直到排完。
 }
 // 交换数组元素
 private static void swap(File[] fa, int low, int high) {
  File temp = fa[low];
  fa[low] = fa[high];
  fa[high] = temp;
 }
 //转化方法
 private static long stringToLong(long key){
  String creatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(key));// 文件创建时间,hh12时制,HH24时制
  String newTime = creatTime.replace("-", "").replace(":", "").replace(" ", "");
  long key1 = Long.parseLong(newTime);
  return key1;
 }
阅读全文
0 0
原创粉丝点击