gpt4 book ai didi

java - 如何将 FileResource 加载到字节数组中?

转载 作者:行者123 更新时间:2023-12-01 16:56:53 25 4
gpt4 key购买 nike

我有一个名为 data.dat 的文件,我想将其加载到 Vaadin 中的 byte[] 中。这是一个数据文件,我需要加载然后根据用户的输入进行操作。

我尝试过:

String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
FileResource resource = new FileResource(new File(basepath + MY_DATA_FILE));

问题是我不知道如何操作资源来提取byte[]。我明白了lots of information for how to put images and so on to the UI components , how to stream a user generated PDF等等,但我似乎找不到任何有关如何加载自己的数据文件然后在代码中进行操作的示例...

最佳答案

从 vaadin 的文档中你可以得到:

java.io.File    getSourceFile() 
Gets the source file.

DownloadStream getStream() 
Gets resource as stream.

DownloadStream中:

java.io.InputStream getStream() 
Gets downloadable stream.

这样你就可以轻松地操作FileInputStream来读取FileResource的内容

例如在 Java 8 中:

byte[] bytes = Files.readAllBytes(Paths.get(resource.getSourceFile().getAbsolutePath()));

关于java - 如何将 FileResource 加载到字节数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31465974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com