2017-05-09 1 views
0

Ich mag würde die Größe der aufgenommenen Dateien in KITDM (auf tomcat7) bekommen ich so iterieren kann:Abrufen von Dateigröße für die Sammlung dieser 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() 
     + ")" 
     + "; "); 
} 

Auf lokalen Speicher ich leicht

können
File path = new File(stringForPath); 

und dann auf Eigenschaften mit isDirectory(), length usw. zugreifen, um die tatsächliche Größe einiger Dateien zu akkumulieren.

Aber wie bekomme ich vom IDataOrganizationNode in die Datei? Bitte erklären Sie auch, wie Sie zu Ihrer Idee/Lösung gekommen sind.

Antwort

0

Wie pContainer.getDestination() gibt mir die „Basis“ URL alle Knoten würde ich iterieren, ich auf, dass man nur ging, fiel das Präfix „file:“ aus dem resultierenden String und ersetzt mehr Hiebe mit nur ein einzigen führenden . Dies funktioniert bisher unter GNU/Linux, für andere Systeme müsste ich die resultierende URL sehen.

Die URL.toString() dann ein File durch diese Erstickten:

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; 
} 

Und schließlich kann ich die Datei und mögliche Unterverzeichnisse zu Fuß durch ihre Größe zu akkumulieren:

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; 
} 

Wahrscheinlich gibt es viel schöner Möglichkeiten, dies zu tun, aber zumindest gibt es mir die Dateigröße meiner Aufnahme (Upload).

Verwandte Themen