Java(maven web app),获取资源文件夹中文件的完整文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7184186/
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
Java (maven web app), getting full file path for file in resources folder?
提问by Rick
I'm working with a project that is setup using the standard Maven directory structure so I have a folder called "resources" and within this I have made a folder called "fonts" and then put a file in it. I need to pass in the full String
file path (of a file that is located, within my project structure, at resources/fonts/somefont.ttf
) to an object I am using, from a 3rd party library, as below, I have searched on this for a while but have become a bit confused as to the proper way to do this. I have tried as below but it isn't able to find it. I looked at using ResourceBundle
but that seemed to involve making an actual File
object when I just need the path to pass into a method like the one below (don't have the actual method call in front of me so just giving an example from my memory):
我正在处理一个使用标准 Maven 目录结构设置的项目,因此我有一个名为“resources”的文件夹,在其中我创建了一个名为“fonts”的文件夹,然后将一个文件放入其中。我需要将完整的String
文件路径(位于我的项目结构中的文件resources/fonts/somefont.ttf
)传递给我正在使用的对象,来自 3rd 方库,如下所示,我已经搜索了一段时间,但有对执行此操作的正确方法感到有些困惑。我已尝试如下,但无法找到它。我看着 usingResourceBundle
但这似乎涉及制作一个实际的File
对象,当我只需要传递到如下所示的方法的路径时(在我面前没有实际的方法调用,所以只是从我的记忆中举一个例子) :
FontFactory.somemethod("resources/fonts/somefont.ttf");
I had thought there was a way, with a project with standard Maven directory structure to get a file from the resource folder without having to use the full relative path from the class / package. Any advice on this is greatly appreciated.
我曾想过有一种方法,可以使用具有标准 Maven 目录结构的项目从资源文件夹中获取文件,而无需使用类/包中的完整相对路径。对此的任何建议都非常感谢。
I don't want to use a hard-coded path since different developers who work on the project have different setups and I want to include this as part of the project so that they get it directly when they checkout the project source.
我不想使用硬编码路径,因为参与该项目的不同开发人员有不同的设置,我想将其作为项目的一部分包含在内,以便他们在签出项目源代码时直接获得它。
This is for a web application (Struts 1.3 app) and when I look into the exploded WAR file (which I am running the project off of through Tomcat), the file is at:
这是一个 Web 应用程序(Struts 1.3 应用程序),当我查看分解的 WAR 文件(我通过 Tomcat 运行该项目)时,该文件位于:
<Exploded war dir>/resources/fonts/somefont.ttf
回答by BobG
Code:
代码:
import java.io.File;
import org.springframework.core.io.*;
public String getFontFilePath(String classpathRelativePath) {
Resource rsrc = new ClassPathResource(classpathRelativePath);
return rsrc.getFile().getAbsolutePath();
}
In your case, classpathRelativePath would be something like "/resources/fonts/somefont.ttf".
在您的情况下,classpathRelativePath 将类似于“/resources/fonts/somefont.ttf”。
回答by user1129947
You can use the below mentioned to get the path of the file:
您可以使用下面提到的来获取文件的路径:
String fileName = "/filename.extension"; //use forward slash to recognize your file
String path = this.getClass().getResource(fileName).toString();
use/pass the path to your methods.
使用/传递方法的路径。
回答by Alexandre Ogrodovski
URL resource = getServletContext().getResource("/WEB-INF/classes/fonts/somefont.ttf");
String relativePath = resource.getPath();
回答by Ryan Stewart
If your resources directory is in the root of your war, that means resources/fonts/somefont.ttf would be a "virtual path" where that file is available. You can get the "real path"--the absolute file system path--from the ServletContext. Note (in the docs) that this only works if the WAR is exploded. If your container runs the app from the war file without expanding it, this method won't work.
如果您的资源目录位于War的根目录中,则意味着资源/字体/somefont.ttf 将是该文件可用的“虚拟路径”。您可以从 ServletContext获取“真实路径”——绝对文件系统路径。请注意(在文档中)这仅在 WAR 爆炸时才有效。如果您的容器从 war 文件运行应用程序而不展开它,则此方法将不起作用。
回答by Fazal
You can look up the answer to the question on similar lines which I had Loading XML Files during Maven Test run
您可以在我在 Maven 测试运行期间加载 XML 文件的类似行上查找问题的答案
The answer given by BobG should work. Though you need to keep in mind that path for the resource file is relative to path of the current class. Both resources and java source files are in classpath
BobG 给出的答案应该有效。尽管您需要记住资源文件的路径是相对于当前类的路径。资源和java源文件都在类路径中