簡體   English   中英

如何將FileResource加載到字節數組中?

[英]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.

因此,您可以輕松地對FileInputStream進行操作以讀取FileResource的內容

例如在Java 8中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM