【发布时间】:2016-05-23 08:15:45
【问题描述】:
当我保存文件时,我使用:
File file = new File(filename);
但是,由于我不再具有写入文件夹的权限,我宁愿将其保存到内存中,然后将文件读取到 FileOutputStream。
我已阅读我可以使用这种方法将文件保存到内存中:
new ByteArrayOutputStream();
整个代码会是什么样子?上传完成后,我无法弄清楚如何正确编写它。
编辑:我正在使用 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
}
【问题讨论】:
-
为什么需要将其“保存”到内存中?内容在“上传完成后”内存中(可能为
byte[]或String) -
“整个代码是什么样子的?” 试一试,如果您有具体的问题,请回来。
-
sharobn.. 我怎样才能收集这些内容?我的代码显示为 T.J.Crowder。问题是如何在不写入磁盘的情况下收集数据并获取 FileOutputStream 数据
-
你可以改变这个方法返回
return new ByteArrayOutputStream();。您可以像使用任何其他OutputStream一样使用ByteArrayOutputStream。