检索这些IDataOrganizationNode

时间:2017-05-09 10:39:24

标签: java file tomcat7 filesize

我想在KITDM中获取摄取文件的大小(在tomcat7上)我可以像这样迭代:

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获取文件?还请解释你是如何得到你的想法/解决方案。

1 个答案:

答案 0 :(得分:0)

由于pContainer.getDestination()给了我" base"我将迭代的所有节点的URL,我只是继续那个,删除前缀"文件:"从生成的String中,只用一个前导替换多个斜杠。到目前为止,这适用于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;
}

可能有更好的方法可以做到这一点,但至少它给了我摄取(上传)的文件大小。