相关疑难解决方法(0)

如何使用相对于应用程序的文件路径在服务器上创建文件?

在我的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)

java file-upload spring-boot vaadin-spring-boot

5
推荐指数
1
解决办法
3428
查看次数