[英]How do I load a FileResource into a byte array?
我有一個名為data.dat
的文件,我想將其加載到Vaadin的byte[]
中。 這是一個數據文件,我需要加載它,然后根據用戶的輸入進行操作。
我試過了:
String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
FileResource resource = new FileResource(new File(basepath + MY_DATA_FILE));
問題是我不知道如何操縱resource
來提取byte[]
。 我看到了很多有關如何將圖像等放入UI組件 , 如何流傳輸用戶生成的PDF等信息,但是我似乎找不到任何有關如何加載自己的數據文件的示例。然后在代碼中操作...
從vaadin的文檔中,您有:
java.io.File getSourceFile()
Gets the source file.
要么
DownloadStream getStream()
Gets resource as stream.
在DownloadStream
:
java.io.InputStream getStream()
Gets downloadable stream.
因此,您可以輕松地對File
或InputStream
進行操作以讀取FileResource
的內容
例如在Java 8中:
byte[] bytes = Files.readAllBytes(Paths.get(resource.getSourceFile().getAbsolutePath()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.