2017-05-09 22 views
0

我想获得的摄取的文件的大小KITDM(上tomcat7)的集合,我可以遍历这样的:检索文件大小为这些IDataOrganizationNode

ICollectionNode root = pContainer.getFileTree().getRootNode(); 
IDataOrganizationNode dataSubTree = Util.getNodeByName(root, Constants.STAGING_DATA_FOLDER_NAME);//Constant is "data" 
ICollectionNode coll = (ICollectionNode) dataSubTree; 
for (IDataOrganizationNode n : coll.getChildren()) { 
    System.out.print(n.getName() + ": " 
     + n.getAttributes().toString() 
     + " (" 
     + n.getTransientNodeId().getDigitalObjectId().getStringRepresentation() 
     + ")" 
     + "; "); 
} 

在本地存储,我可以很容易地使用

File path = new File(stringForPath); 

然后访问属性与isDirectory(),length等积累一些文件的实际大小。

但是,我如何从IDataOrganizationNode获得文件?请也解释你如何得到想法/解决方案。

回答

0

由于pContainer.getDestination()给我所有我将迭代的节点的“base”URL,我只是继续那个,从结果字符串中删除前缀“file:”,并用多个斜杠替换多个斜杠。到目前为止,这在GNU/Linux上是有效的,对于其他系统我需要查看结果URL。

URL.toString()然后由该勒死到File

public static File getFileFromURLOrFileString(String fn) { 
    File f = new File(fn); 
    URI u = null; 
    int i = 0; 
    if (! f.exists()) { 
     while (fn.indexOf("file:") >= 0) { 
      i = fn.indexOf("file:"); 
      System.out.println(" n: " + fn + " – " + i + " – " + ((i >= 0) ? fn.substring(i) : "")); 
      if (i == 0) { 
       fn = fn.replaceFirst("file:",""); 
      } else { 
       fn = fn.substring(i); 
      } 
     } 
     while (fn.indexOf("//") >= 0) { 
      fn = fn.replaceAll("//","/"); 
     } 
     //System.out.println(" n: " + fn + " – " + fn.indexOf("//")); 
     u = (new File(fn)).toURI(); 
     f = new File(u); 
    } 
    return f; 
} 

最后,我可以通过文件走,可能子目录来积累自己的尺寸:

private static boolean isLink(File f) { 
    Path p = Paths.get(f.toString()); 
    return Files.isSymbolicLink(p); 
} 
private static long usedSpace(File path) //throws FileExistsException 
{ 
    long size = 0l; 
    if (path == null) { 
     System.out.println("ERROR: No Files in " + path); 
     System.out.println("exists :" + path.exists()); 
     System.out.println("isDir :" + path.isDirectory()); 
     System.out.println("isFile :" + path.isFile()); 
     System.exit(1); 
    } 
    if (isLink(path)) { 
     return 0; 
    } 
    int c = 0; 
    try { 
     c = path.listFiles().length; 
    } catch (Exception e) { 
     System.out.println("path : " + path); 
     System.out.println("link : " + isLink(path)); 
     System.out.println("file : " + path.isFile()); 
     System.out.println("dir : " + path.isDirectory()); 
     System.out.println("list : " + path.listFiles()); 
     System.out.println("count: " + c); 
     e.printStackTrace(); 
    } 
    if (c == 0) { 
     return 0; 
    } 
    for (File file : path.listFiles()) { 
     if (file.isDirectory()) { 
      size += usedSpace(file); 
     } else { 
      try { 
       if (isLink(file)) { 
        //+=0 
       } else {//file.isFile() … 
       //} else if(Files.isRegularFile(link)) {// had e.g. sockets and a pipe 
        //    System.out.println(file.getName() + " " + file.length()); 
        size += file.length(); 
       } 
      } catch(NullPointerException e) { 
       System.out.println(file.toString() 
         + "\t" + e.getStackTrace()); 
      } 
     } 

    } 
    return size; 
} 

也许还有更漂亮方法来做到这一点,但至少它给了我摄取(上传)的文件大小。