收到上传的文件后,我想返回一个代表上传文件的文件,我覆盖了 receiveUpload 方法:
/**
* 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);`
Run Code Online (Sandbox Code Playgroud)
那么返回new File对象的最佳方式是什么。
这取决于您想做什么。该方法receiveUpload()是接口的一部分Receiver。您需要提供outputStreamVaadin 可以用来写入上传数据的一个。
您可能并不总是想写入文件。也许您想写入数据库或只是写入字节数组以进一步处理数据。
假设您想写入一个文件,您只需在磁盘上创建一个文件并将其返回FileOutputStream到该文件。
要创建临时文件,您可以使用以下命令:
File file = File.createTempFile(fileName, ".tmp");
return new FileOutputStream(file);
Run Code Online (Sandbox Code Playgroud)
但如果你想写入特定位置,你可以直接提供填充文件路径,例如:
File file = new File("/path/to/my/file/storage/" + filename);
return new FileOutputStream(file);
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请参阅瓦丁之书。
| 归档时间: |
|
| 查看次数: |
6851 次 |
| 最近记录: |