2014-06-13 45 views
2

我有扩展属性user.MYATTR如何在Java中读取扩展文件属性?

当我运行命令文件:getfattr fileName -d,我得到这个:

user.MYATTR="attribute_value" 

我的操作系统是红帽企业Linux服务器版本6.4(圣地亚哥)

但是当我正在运行我的Java代码

LOG.debug("readExtentedAttribute(path={},name={}", path, name); 

    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); 

    ByteBuffer buffer = ByteBuffer.allocate(view.size(name)); 
    view.read(name, buffer); 
    buffer.flip(); 
    String value = Charset.defaultCharset().decode(buffer).toString(); 
    LOG.trace("extended attribute value = {}", value); 

我收到异常

java.nio.file.FileSystemException: /opt/myfolder/myfile: Unable to get size of extended attribute 'user.MYATTR': No data available 
    at sun.nio.fs.LinuxUserDefinedFileAttributeView.size(LinuxUserDefinedFileAttributeView.java:141) ~[na:1.7.0_25] 
    at sk.tempest.cda.impex.service.FileServiceImpl.readExtentedAttribute(FileServiceImpl.java:39) ~[impex-impl-0.0.1-SNAPSHOT.jar!/:na] 
    at sk.tempest.cda.impex.service.TapeMediumServiceImpl.hasDissemFlag(TapeMediumServiceImpl.java:189) ~[impex-impl-0.0.1-SNAPSHOT.jar!/:na] 
    at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source) ~[na:na] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25] 
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25] 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at com.sun.proxy.$Proxy85.hasDissemFlag(Unknown Source) ~[na:na] 
    at sk.tempest.cda.impex.service.TapeStorageServiceImpl.registerNewMedia(TapeStorageServiceImpl.java:158) ~[impex-impl-0.0.1-SNAPSHOT.jar!/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25] 
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25] 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at com.sun.proxy.$Proxy87.registerNewMedia(Unknown Source) ~[na:na] 
    at sk.tempest.cda.impex.service.WorkerServiceImpl.checkNewMedia(WorkerServiceImpl.java:46) ~[impex-impl-0.0.1-SNAPSHOT.jar!/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25] 
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25] 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:97) ~[spring-aop-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) ~[na:1.7.0_25] 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) ~[na:1.7.0_25] 
    at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25] 

回答

1

最后我发现,当我想读取属性user.MYATTR,我必须使用名称MYATTR。

我只想提我发现了什么有趣的现象,那可能会导致mystakes,我想提醒你:)

我的文件中有这两个属性:

user.MYATTR1 user.somethingElse.MYATTR2

当我使用view.list()方法中列出的属性,我看到只有这一个(没有用户): MYATTR1

当我想读取属性值,我必须使用属性的名称,而不,所以对于上述的属性是“用户”: MYATTR1 或 somethingElse.MYATTR2

+0

中所述使用了'Files.getFileStore()'将其标记为已回答,请 –

2

你读过docs的类UserDefinedFileAttributeView

仔细阅读给出了一些提示,为什么你的代码无法正常工作。

例如,您可以检查:

...这FileAttributeView不打算使用其中的 属性值的大小比Integer.MAX_VALUE大...

或者检查,如果您安装了安全管理器:

...在def的情况下至少,当安装 安全管理器时, 访问用户定义的属性的所有方法都需要 RuntimePermission(“accessUserDefinedAttributes”)权限。 ...

,或尝试另一种方法来获得一个属性:

...如果属性需要文件的动态访问时, getAttribute方法可用于读取属性值。该属性值作为字节数组(byte [])返回。 ...

也许任何提示帮助你,祝你好运! :-)

EDIT

下面是一个例子代码,检查,如果文件系统提供了用户定义的文件属性,并将它们打印(上更迭):

Path file = Paths.get("filename.ext"); 

// check that user defined attributes are supported by the file system 
FileStore store = file.getFileStore(); 
if (!store.supportsFileAttributeView("xattr")) { 
    System.err.format("UserDefinedFileAttributeView not supported on %s\n", store); 
    System.exit(-1); 

} 
UserDefinedFileAttributeView view = file.getFileAttributeView(UserDefinedFileAttributeView.class); 

// list user defined attributes 
if (args.length == 1) { 
    System.out.println(" Size Name"); 
    System.out.println("-------- --------------------------------------"); 
    for (String name: view.list()) { 
     System.out.format("%8d %s\n", view.size(name), name); 
    } 
} 

完整的源代码herethis page连接你的问题代码是;-)

+0

'file.getFileStore();'是不是为我工作。我按照https://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html#store – Brian