在我的Vaadin spring启动应用程序中,我需要将文件上传到服务器并将其保存在那里.
将文件本地上传到我的桌面工作正常,但我想将应用程序部署到服务器,所以我想将上传的文件保存在相对于我的应用程序的基本路径的目录中.
我创建了一个名为"input_files"的文件夹作为根应用程序文件夹(applicationName - > input_files)的直接子项.上传文件时,应该将文件的内容写入此"input_files"文件夹中新创建的文件.
码:
class FileReceiver implements Upload.Receiver, Upload.SucceededListener {
private File file;
public OutputStream receiveUpload(String filename, String mimeType) {
String basePath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
FileOutputStream fileOutputStream;
try {
//this line would work:
//file = new File("/Users/username/Desktop/input_files/" + filename);
//this line does not work:
file = new File(basePath + "/input_files/" + filename);
fileOutputStream = new FileOutputStream(file);
labelFilename.setCaption(filename);
}
catch (final java.io.FileNotFoundException e) {
new Notification("Could not open file", e.getMessage(), Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());
return null;
}
return fileOutputStream;
}
public …Run Code Online (Sandbox Code Playgroud)