如何获取 WebContent 文件夹中文件的真实路径?
我需要获取 WebContent 目录中文件的真实路径,以便我使用的框架可以访问该文件。它只接受 String 文件作为属性,所以我需要在 WebContent 目录中获取该文件的真实路径。
我使用Spring框架,所以解决方案应该可以在Spring中制定。
I need to get real path for file in my WebContent directory, so that framework that I use can access that file. It only takes String file as attribute, so I need to get the real path to this file in WebContent directory.
I use Spring Framework, so solution should be possible to make in Spring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您在 servlet 中需要此功能,请使用 getServletContext().getRealPath("/filepathInContext")!
If you need this in a servlet then use
getServletContext().getRealPath("/filepathInContext")
!getServletContext().getRealPath("") - 如果内容可从 .war 存档中获取,则此方法将不起作用。 getServletContext() 将为 null。
在这种情况下我们可以使用另一种方式来获取真实路径。这是获取属性文件 C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties 的路径的示例:
getServletContext().getRealPath("") - This way will not work if content is being made available from a .war archive. getServletContext() will be null.
In this case we can use another way to get real path. This is example of getting a path to a properties file C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties:
你必须告诉java将路径从你的电脑更改为你的java项目,这样
如果你使用 spring 使用:
但是如果你使用 servlet 只需使用
,路径将是
/webapp/images/
:)you must tell java to change the path from your pc into your java project so
if you use spring use :
but if you use servlets just use
so the path will be
/webapp/images/
:)在这种情况下,我倾向于提取所需的内容作为资源 (MyClass.getClass().getResourceAsStream()),将其作为文件写入临时位置,并使用该文件进行其他调用。
这样,我就不必费心处理仅包含在 jar 中或位于某个位置(取决于我当前使用的 Web 容器)的内容。
In situations like these I tend to extract the content I need as a resource (MyClass.getClass().getResourceAsStream()), write it as a file to a temporary location and use this file for the other call.
This way I don't have to bother with content that is only contained in jars or is located somewhere depending on the web container I'm currently using.
包含请求作为参数。 Spring 在调用映射方法时将传递请求对象
Include the request as a parameter. Spring will then pass the request object when it calls the mapped method
您可以使用 Spring Resource 接口(尤其是 ServletContextResource):
You could use the Spring Resource interface (and especially the ServletContextResource): http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/core/io/Resource.html
此方法使用资源加载器获取应用程序中文件的绝对路径,然后向上移动几个文件夹到应用程序的根文件夹。不需要 servlet 上下文!如果您的 WEB-INF 文件夹中有“web.xml”,这应该可以工作。请注意,您可能需要考虑仅将其用于开发,因为这种类型的配置通常最好存储在应用程序外部。
This approach uses the resource loader to get the absolute path to a file in your app, and then goes up a few folders to the app's root folder. No servlet context required! This should work if you have a "web.xml" in your WEB-INF folder. Note that you may want to consider using this solely for development, as this type of configuration is usually best stored externally from the app.
我的解决方案:..../webapps/mydir/ (../webapps/ROOT/../mydir/)
my solve for: ..../webapps/mydir/ (..../webapps/ROOT/../mydir/)
当您也想在开发和生产级别中使用 arff.txt 时,请尝试使用此选项
try to use this when you want to use arff.txt in your development and production level too