Java servlet 如何获取 servlet 外部文件的绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/558502/
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
How a servlet can get the absolute path to a file outside of the servlet?
提问by WolfmanDragon
We have been using System.getProperties("user.dir") to get the location of a properties file. Now that it has been deployed on Tomcat(via servlet), the System call is giving the location as tomcat and not at the location at where the properties file exist.
我们一直在使用 System.getProperties("user.dir") 来获取属性文件的位置。现在它已经部署在 Tomcat 上(通过 servlet),系统调用将位置作为 tomcat 而不是属性文件所在的位置。
How can we call the the properties file dynamically?
我们如何动态调用属性文件?
Given:
鉴于:
- Tomcat is not the only way the app will be deployed
- We have no control on where the app may be placed.
- Relative paths will not work as that Vista is being used and Vista breaks relative paths.
- This must work on all OS, including(but not limited to) Linux, XP and Vista.
- EDITI implied this, but in case I was not clear enough, I have no way of knowing the path String.
- Tomcat 并不是部署应用程序的唯一方式
- 我们无法控制应用程序的放置位置。
- 相对路径将不起作用,因为正在使用 Vista 并且 Vista 破坏了相对路径。
- 这必须适用于所有操作系统,包括(但不限于)Linux、XP 和 Vista。
- 编辑我暗示了这一点,但如果我不够清楚,我无法知道路径字符串。
采纳答案by Thorbj?rn Ravn Andersen
You must have a way of knowing the path of the property file which you can then wrap in a File and pass to the load() method of your properties object.
您必须有办法知道属性文件的路径,然后您可以将其包装在 File 中并传递给属性对象的 load() 方法。
If you run inside a Tomcat service you are not running as the user you installed it as,so you cannot derive the home directory. You most likely need to hardcode SOMETHING then.
如果您在 Tomcat 服务中运行,则您不是以安装它的用户身份运行,因此您无法导出主目录。那么你很可能需要硬编码一些东西。
Edit: The property file is relative to the application. See http://www.exampledepot.com/egs/java.lang/ClassOrigin.htmlfor an example of how to get to the file name for the bytecode for a given class. You should be able to continue from there.
编辑:属性文件是相对于应用程序的。有关如何获取给定类的字节码的文件名的示例,请参见http://www.exampledepot.com/egs/java.lang/ClassOrigin.html。您应该可以从那里继续。
Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation(); // file:/c:/almanac14/examples/
You should be aware that some security managers disallow this.
您应该知道一些安全管理员不允许这样做。
回答by John Topley
Take a look at ServletContext
's getResourceand getResourceAsStreammethods.
看看ServletContext
的getResource和getResourceAsStream方法。
回答by neesh
You can place your property files in your class path and read them as an input stream using something like:
您可以将属性文件放在类路径中,并使用以下内容将它们作为输入流读取:
ClassName.class.getResourceAsStream(filePath + fileName);
ClassName.class.getResourceAsStream(filePath + fileName);
Where filePath is the relative path of the file from the class path root and fileName is the name of the file.
其中 filePath 是文件从类路径根目录的相对路径,fileName 是文件的名称。
If you need to get the absolute path to a file you read as a resource, you can do some thing like this:
如果需要获取作为资源读取的文件的绝对路径,可以执行以下操作:
ClassName.class.getResource(filePath + fileName).getPath()
ClassName.class.getResource(filePath + fileName).getPath()
回答by Akrikos
Is it possible to put the config file within the classpath and reference it via something like springs ClassPathResource?
是否可以将配置文件放在类路径中并通过 springs ClassPathResource之类的东西引用它?
You should be able to use it like this:
你应该能够像这样使用它:
ClassPathResource foo = new ClassPathResource("file.name");
Where file.name exists somewhere in the classpath at the lowest level such as:
其中 file.name 存在于最低级别的类路径中的某处,例如:
- /webapps/WEB-INF/classes/
- the base of one of your jar files
- /webapps/WEB-INF/classes/
- 您的 jar 文件之一的基础
回答by Akrikos
I think ClassName.class.getResourceAsStream()will work for you. The comments on that method point you at ClassLoader.getResource(), which tells you how to specify a file in your classpath.
我认为ClassName.class.getResourceAsStream()会为你工作。该方法的注释指向ClassLoader.getResource(),它告诉您如何在类路径中指定文件。
Something like this should work:
这样的事情应该工作:
InputStream foo = ClassName.class.getResourceAsStream("file.name");
Where file.name is at the base of your classpath somewhere. If file.name is in the com.foo.bar package, you would use "/com/foo/bar/file.name"
file.name 位于类路径的底部。如果 file.name 在 com.foo.bar 包中,您将使用“/com/foo/bar/file.name”
回答by Rocket Surgeon
Another approach may be to pass the variable value to the JVM with -D parameter. That way you can tie your code to the same variable name and then pass different values at the time of start-up. I haven't tried this, but i'm thinking it should work for an app deployed in Tomcat as well, if you modify startCatalina script to pass -D parameter to JVM
另一种方法可能是使用 -D 参数将变量值传递给 JVM。这样您就可以将代码绑定到相同的变量名,然后在启动时传递不同的值。我还没有尝试过这个,但我认为它也适用于部署在 Tomcat 中的应用程序,如果你修改 startCatalina 脚本以将 -D 参数传递给 JVM
回答by toolkit
You could store the location of your properties file in JNDI?
您可以将属性文件的位置存储在 JNDI 中吗?
This should be portable across Tomcat, and for Java EE Application Servers.
这应该可以跨 Tomcat 和 Java EE 应用服务器移植。
回答by david a.
If I understood the question correclty, another alternative might be to call ServletContext.getRealPath()
on a jsp or a static file in your app and derive the path from the result.
如果我理解正确的问题,另一种选择可能是调用ServletContext.getRealPath()
应用程序中的 jsp 或静态文件并从结果中导出路径。
It implies webapp deployed as "expanded" - i.e. not as a compressed war file - but that is what most appservers do anyway.
这意味着 webapp 部署为“扩展” - 即不是作为压缩的战争文件 - 但无论如何大多数应用服务器都是这样做的。
回答by JosefB
We had the same requirements. The simplest thing that worked so far was to just store a basedir property in one of the property files we were already loading. Then define a method like getExternalDocPath(String path).
我们有同样的要求。到目前为止,最简单的方法是将 basedir 属性存储在我们已经加载的属性文件之一中。然后定义一个像 getExternalDocPath(String path) 这样的方法。
This allowed us to extend the docbase of Tomcat (which only Tomcat 7 supports?). Out on the web someone posted a class that actually extends Tomcat's docbase to allow multiple paths, i.e., "alpha;baker;charlie".
这允许我们扩展 Tomcat 的文档库(只有 Tomcat 7 支持?)。在网上有人发布了一个类,它实际上扩展了 Tomcat 的文档库以允许多个路径,即“alpha;baker;charlie”。