繁体   English   中英

获取文件元数据字段

[英]getting file metadata fields

我使用此代码在图像文件中获取特定的用户定义的元数据:

File file = new File("C:\\image.jpg");
        Path path = FileSystems.getDefault().getPath(file.getPath(), "");
        BasicFileAttributes attr = null;
        try {
            attr = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        UserDefinedFileAttributeView view = Files
        .getFileAttributeView(path,UserDefinedFileAttributeView.class);
        String name = "UserDefinedField";

        ByteBuffer buf = null;
        try {
            buf = ByteBuffer.allocate(view.size(name));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            view.read(name, buf);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        buf.flip();
        String value = Charset.defaultCharset().decode(buf).toString();
        System.out.println(value);

但是我必须知道该字段,有没有办法列出所有元数据字段?

您可以使用list()检索文件的所有UserDefinedFileAttributes

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.List;

public class ReadData {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("someImage.jpg").toAbsolutePath();

        UserDefinedFileAttributeView fileAttributeView = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
        List<String> allAttrs = fileAttributeView.list();

        for (String att : allAttrs) {
            System.out.println("att = " + att);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM