如何使用文件路径相对于应用程序在服务器上创建文件? [英] How to create file on server with file path relative to application?

查看:228
本文介绍了如何使用文件路径相对于应用程序在服务器上创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Vaadin spring启动应用程序中,我需要上传文件到服务器并保存在那里。

本地上传文件到我的桌面可以正常工作
但是我想将应用程序部署到一个服务器上,所以我想把上传的文件保存在一个相对于我的应用程序的基本路径的目录中。



我创建了一个名为input_files的文件夹作为根应用程序文件夹(applicationName - > input_files)的直接子项。当文件上传时,文件的内容应该被写入到这个input_files文件夹中新创建的文件中。



代码:

 类FileReceiver实现Upload.Receiver,Upload.SucceededListener {
private文件文件;
$ b $ public OutputStream receiveUpload(String filename,String mimeType){
String basePath = VaadinService.getCurrent()。getBaseDirectory()。getAbsolutePath();
FileOutputStream fileOutputStream;

try {
//这行可以工作:
// file = new File(/ Users / username / Desktop / input_files /+ filename);

//这行不起作用:
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());
返回null;
}
return fileOutputStream;
}

public void uploadSucceeded(Upload.SucceededEvent event){


};

重要的代码如下:

  file = new File(basePathOfApplication +/ input_files /+ filename); 

这行不行。在 input_files 文件夹内没有创建文件。



************* ******************* UPDATE ****************************** ****



屏幕上显示错误讯息:

指出:


上下文基础目录。通常,应用程序以具有应用程序目录的方式进行部署。对于Web应用程序,该目录是Web应用程序的根目录。在某些情况下,应用程序可能没有应用程序目录(例如在战争中运行的Web应用程序)。

所以, null ,这也会破坏你的代码。



恕我直言,最好使用另一个专用的上传目录可以通过应用程序配置来指定。或者使用一个知名的位置,比如 / tmp

另外,请确保首先创建父目录 file.getParentFile()。mkdirs()


In my Vaadin spring boot application, I need to upload files to the server and save them there.

Uploading files locally to my desktop works fine, But I want to deploy the application to a server, so I want to save the uploaded files in a directory relative to the base path of my application.

I created a folder called "input_files" as a direct child of the root application folder (applicationName -> input_files). When a file is uploaded, the content of the file is supposed to be written to a newly created file inside this "input_files" folder.

Code:

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 void uploadSucceeded(Upload.SucceededEvent event) {

  }
};

The important line of code is this:

file = new File(basePathOfApplication + "/input_files/" + filename);

This line does not work. No file is created inside the input_files folder.

********************************UPDATE**********************************

Error message displayed on the screen:

Error message in the console:

com.vaadin.server.UploadException: Upload failed
    at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:631) [vaadin-server-8.1.0.jar:8.1.0]
    at com.vaadin.server.communication.FileUploadHandler.handleFileUploadValidationAndData(FileUploadHandler.java:460) [vaadin-server-8.1.0.jar:8.1.0]
    at com.vaadin.server.communication.FileUploadHandler.doHandleSimpleMultipartFileUpload(FileUploadHandler.java:413) [vaadin-server-8.1.0.jar:8.1.0]
    at com.vaadin.server.communication.FileUploadHandler.handleRequest(FileUploadHandler.java:290) [vaadin-server-8.1.0.jar:8.1.0]
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1568) [vaadin-server-8.1.0.jar:8.1.0]
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:381) [vaadin-server-8.1.0.jar:8.1.0]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: com.vaadin.server.NoOutputStreamException: null
    at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:559) [vaadin-server-8.1.0.jar:8.1.0]
    ... 43 common frames omitted

解决方案

Make sure that your application can create files in the directory /private/var/.... Most probably, it can not. I don't know much about Vaadin, but the folder itself may be a "read-only" / "temporary" directory to store unpacked classes and data required for the application. This directory may not be intended to store any uploads and dynamic content.

The documentation states:

Returns the context base directory. Typically an application is deployed in a such way that is has an application directory. For web applications this directory is the root directory of the web applications. In some cases applications might not have an application directory (for example web applications running inside a war).

So, it may be even null, which will break you code too.

IMHO, it's better to use another, dedicated directory for uploads that can be specified via application config. Or use a well-know location, like /tmp.

Also, make sure to create parent directories first with file.getParentFile().mkdirs().

这篇关于如何使用文件路径相对于应用程序在服务器上创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆