检索这些 IDataOrganizationNode 集合的文件大小
Retrieving filesize for the collection of these IDataOrganizationNode
我想获取 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 获取文件?还请解释一下你是怎么找到你的 idea/solution.
因为 pContainer.getDestination()
给了我要迭代的所有节点的 "base" URL
,我只是继续那个,从结果字符串并用一个前导斜杠替换多个斜杠。到目前为止,这在 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;
}
可能有更好的方法来做到这一点,但至少它给了我摄取(上传)的文件大小。
我想获取 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 获取文件?还请解释一下你是怎么找到你的 idea/solution.
因为 pContainer.getDestination()
给了我要迭代的所有节点的 "base" URL
,我只是继续那个,从结果字符串并用一个前导斜杠替换多个斜杠。到目前为止,这在 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;
}
可能有更好的方法来做到这一点,但至少它给了我摄取(上传)的文件大小。