2014-06-13 82 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