创建文件并存储在 Java Web 应用程序文件夹中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2970643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:45:36  来源:igfitidea点击:

Create a file and store in Java web application folder

javaspring-mvc

提问by GigaPr

I would like to create an xml file and store in a folder within my spring Mvc web application.

我想创建一个 xml 文件并存储在我的 spring Mvc Web 应用程序中的一个文件夹中。

I can get the root of my application with request.getContextPath()

我可以得到我的应用程序的根 request.getContextPath()

but

how do i get the application's relative path so it will work on any machine indipendently by the location of the application's folder?

我如何获得应用程序的相对路径,以便它可以根据应用程序文件夹的位置在任何机器上独立运行?

Like C:/folder/folder/MYAPPLICATIONROOTFOLDER

喜欢 C:/folder/folder/MYAPPLICATIONROOTFOLDER

回答by Will Hartung

You want to do this.

你想这样做。

First, you need to get the ServletContext. I don't know how this is done in Spring MVC, but it's there somewhere.

首先,您需要获取 ServletContext。我不知道这是如何在 Spring MVC 中完成的,但它在某处。

Then you can do:

然后你可以这样做:

ServletContext ctx = getServletContextFromSpringSomehow();
String path = ctx.getRealPath("/folder/filename.txt");
FileWriter fw = new FileWriter(path);

The key here is ServletContext.getRealPath. It gives you the local file system path of a resource from within your webapp. Observer that you use "/" here, as it's a URL, not a file name. The container will give you a valid file name in return. Note, this only works if your container explodes your WAR, or you deploy an exploded WAR. If the WAR is NOT exploded, you will get a null back from the container.

这里的关键是 ServletContext.getRealPath。它为您提供 web 应用程序中资源的本地文件系统路径。观察者,您在这里使用“/”,因为它是一个 URL,而不是一个文件名。作为回报,容器会给你一个有效的文件名。请注意,这仅在您的容器爆炸 WAR 或部署爆炸 WAR 时才有效。如果 WAR 没有爆炸,您将从容器中获得一个空值。

Also note, this WILL work for non-existent files. The container does not check for the actual existence of the file. But it will be up to you to actually create any missing intermediate directories, etc.

另请注意,这将适用于不存在的文件。容器不检查文件的实际存在。但是实际创建任何丢失的中间目录等取决于您。

Finally, of course, that even if you get a file path back, doesn't mean you can actually write to that path. That's a OS permission issue outside of the scope of the container.

最后,当然,即使您返回文件路径,也不意味着您实际上可以写入该路径。这是容器范围之外的操作系统权限问题。

回答by dhable

One solution is to bundle the XML with the clases in the JAR/WAR and then use the getResourceAsStream() to leverage the ClassLoader to locate the file.

一种解决方案是将 XML 与 JAR/WAR 中的类捆绑在一起,然后使用 getResourceAsStream() 来利用 ClassLoader 来定位文件。

If I put the file foo.xml with the classes in com/stackoverflow/example, I could then locate the resources from objects in that bundle with

如果我将文件 foo.xml 与 com/stackoverflow/example 中的类一起放置,那么我可以从该包中的对象中找到资源

InputStream is = MyClass.getResourceAsStream( "com/stackoverflow/example" );

InputStream is = MyClass.getResourceAsStream( "com/stackoverflow/example" );

and from here process the file with a XML parser or whatever else you wanted to do to read the file.

并从这里使用 XML 解析器或其他任何您想要读取文件的方式处理文件。