2013-12-19 32 views
0

接收上傳的文件,我想返回一個文件表示上傳的文件我推翻了receiveUpload梅索德後:如何從一個OutputStream獲得要恢復的文件上傳的文件

/** 
    * Invoked when a new upload arrives. 
    * 
* @param filename 
*   the desired filename of the upload, usually as specified 
*   by the client. 
* @param mimeType 
*   the MIME type of the uploaded file. 
* @return Stream to which the uploaded file should be written. 
*/ 

public OutputStream receiveUpload(String filename, String mimeType);` 

那麼什麼是最好的方式返回一個new File對象。

+0

請顯示您嘗試的代碼不只是簽名。 – sunleo

回答

1

這取決於你想要做什麼。方法receiveUpload()Receiver接口的一部分。您需要提供Vaadin可用於寫入上傳數據的outputStream

您可能並不總是想要寫入文件。也許你想寫入一個數據庫或只是寫入一個字節數組,以便進一步處理數據。

假設您想要寫入文件,您只需在磁盤上創建一個文件並將FileOutputStream返回到此文件。

要創建一個臨時文件,您可以使用此:

File file = File.createTempFile(fileName, ".tmp"); 
    return new FileOutputStream(file); 

但是,如果你想要寫一個特定的位置,可以直接向填充文件路徑,例如:

File file = new File("/path/to/my/file/storage/" + filename); 
    return new FileOutputStream(file); 

欲瞭解更多信息,看看Book of Vaadin