Java 如何在我的 WebContent 文件夹中获取文件的真实路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2786426/
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 can I get real path for file in my WebContent folder?
提问by newbie
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.
我需要在我的 WebContent 目录中获取文件的真实路径,以便我使用的框架可以访问该文件。它只需要字符串文件作为属性,所以我需要在 WebContent 目录中获取该文件的真实路径。
I use Spring Framework, so solution should be possible to make in Spring.
我使用 Spring Framework,因此应该可以在 Spring 中创建解决方案。
采纳答案by Shyam
If you need this in a servlet then use getServletContext().getRealPath("/filepathInContext")
!
如果您需要在 servlet 中使用它,请使用getServletContext().getRealPath("/filepathInContext")
!
回答by tangens
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.
在这种情况下,我倾向于将我需要的内容提取为资源 (MyClass.getClass().getResourceAsStream()),将其作为文件写入临时位置,然后将此文件用于其他调用。
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.
这样我就不必理会仅包含在 jar 中或位于某个位置的内容,具体取决于我当前使用的 Web 容器。
回答by objects
Include the request as a parameter. Spring will then pass the request object when it calls the mapped method
将请求作为参数包含在内。Spring 会在调用映射方法时传递请求对象
@RequestMapping .....
public String myMethod(HttpServletRequest request) {
String realPath = request.getRealPath("/somefile.txt");
...
回答by Thomas Vanstals
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
您可以使用 Spring Resource 接口(尤其是 ServletContextResource):http: //static.springsource.org/spring/docs/current/javadoc-api/org/springframework/core/io/Resource.html
回答by Brad Parks
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.
这种方法使用资源加载器获取应用程序中文件的绝对路径,然后向上移动几个文件夹到应用程序的根文件夹。不需要 servlet 上下文!如果您的 WEB-INF 文件夹中有“web.xml”,这应该可以工作。请注意,您可能需要考虑仅将其用于开发,因为这种类型的配置通常最好从应用程序外部存储。
public String getAppPath()
{
java.net.URL r = this.getClass().getClassLoader().getResource("web.xml");
String filePath = r.getFile();
String result = new File(new File(new File(filePath).getParent()).getParent()).getParent();
if (! filePath.contains("WEB-INF"))
{
// Assume we need to add the "WebContent" folder if using Jetty.
result = FilenameUtils.concat(result, "WebContent");
}
return result;
}
回答by A Kunin
getServletContext().getRealPath("") - This way will not work if content is being made available from a .war archive. getServletContext() will be null.
getServletContext().getRealPath("") - 如果内容从 .war 存档中可用,这种方式将不起作用。getServletContext() 将为空。
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:
在这种情况下,我们可以使用另一种方式来获取真实路径。这是获取属性文件 C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties 路径的示例:
// URL returned "/C:/Program%20Files/Tomcat%206.0/webapps/myapp/WEB-INF/classes/"
URL r = this.getClass().getResource("/");
// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
String decoded = URLDecoder.decode(r.getFile(), "UTF-8");
if (decoded.startsWith("/")) {
// path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
decoded = decoded.replaceFirst("/", "");
}
File f = new File(decoded, "somefile.properties");
回答by Shaaban Ebrahim
try to use this when you want to use arff.txt in your development and production level too
当您也想在开发和生产级别使用 arff.txt 时,请尝试使用它
String path=getServletContext().getRealPath("/WEB-INF/files/arff.txt");
回答by Raslan
you must tell java to change the path from your pc into your java project so if you use spring use :
您必须告诉 java 将路径从您的 pc 更改为您的 java 项目,因此如果您使用 spring,请使用:
@Autowired
ServletContext c;
String UPLOAD_FOLDEdR=c.getRealPath("/images");
but if you use servlets just use
但是如果你使用 servlets 就使用
String UPLOAD_FOLDEdR = ServletContext.getRealPath("/images");
so the path will be /webapp/images/
:)
所以路径将是/webapp/images/
:)