Dar*_*oBB 0 java file vaadin java-io
当我保存文件时,我使用:
File file = new File(filename);
Run Code Online (Sandbox Code Playgroud)
但是,由于我不再具有写入文件夹的权限,我宁愿将其保存到内存中,然后将文件读取到 FileOutputStream。
我读过我可以用这种方法将文件保存到内存中:
new ByteArrayOutputStream();
Run Code Online (Sandbox Code Playgroud)
整个代码会是什么样子?上传完成后我不知道如何正确编写它。
编辑:我正在使用 Vaadins 上传插件:
public File file;
public OutputStream receiveUpload(String filename,
String mimeType) {
// Create upload stream
FileOutputStream fos = null; // Stream to write to
file = null;
if(StringUtils.containsIgnoreCase(filename, ".csv")){
try {
file = new File(filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
new Notification("Error", e.getMessage(), Notification.Type.WARNING_MESSAGE)
.show(Page.getCurrent());
return null;
}
} else {
new Notification("Document is not .csv file", Notification.Type.WARNING_MESSAGE)
.show(Page.getCurrent());
return null;
}
return fos; // Return the output stream to write to
}
Run Code Online (Sandbox Code Playgroud)
public OutputStream receiveUpload(String filename,
String mimeType) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
return byteArrayOutputStream;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法获取写入流中的内容:
byte[] dataWrittenInTheOutputStream = byteArrayOutputStream.toByteArray();
Run Code Online (Sandbox Code Playgroud)
或者您可以将内容写入另一个 OutputStream:
byteArrayOutputStream.writeTo(System.out);
Run Code Online (Sandbox Code Playgroud)
或者:
file = new File(filename);
fos = new FileOutputStream(file);
byteArrayOutputStream.writeTo(fos);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24126 次 |
| 最近记录: |