使用Spring获取WEB-INF下的文件

发布于 2024-12-21 03:13:52 字数 418 浏览 4 评论 0原文

我正在尝试使用 Spring 在 WEB-INF/classes 文件夹下获取一个文件。

<bean id="myBean" class="path.to.MyBean" >
    <property name="myFile">
        <value>file:WEB-INF/classes/myFile.foo</value>
    </property>
</bean>

但是,当我从 MyBean 内部打印出 myFile 的文件路径时,它是:

c:\\apache\bin\WEB-INF\classes\myFile.foo

所以它没有使用相对部署路径。

我做错了什么吗?

I am trying to get a File under my WEB-INF/classes folder using Spring.

<bean id="myBean" class="path.to.MyBean" >
    <property name="myFile">
        <value>file:WEB-INF/classes/myFile.foo</value>
    </property>
</bean>

However, when I print out the myFile's File Path from inside MyBean it is:

c:\\apache\bin\WEB-INF\classes\myFile.foo

So it is not using the relative deployment path.

Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-12-28 03:13:52

尝试:

 <value>classpath:WEB-INF/classes/myFile.foo</value> 

 <value>classpath:myFile.foo</value> 

Try:

 <value>classpath:WEB-INF/classes/myFile.foo</value> 

or

 <value>classpath:myFile.foo</value> 
狼亦尘 2024-12-28 03:13:52

使用:

<property name="myFile" value="/myFile.foo"/>

使用以下方式读取它:

InputStream is = getClass().getResourceAsStream(myFile);

myFile是一个String

另一种方法是将文件存储在与MyBean/WEB-INF/classes/path/to/myFile.foo)包相同的目录中并读取以下方式:

<property name="myFile" value="myFile.foo"/>

请注意,没有前导 / - 路径相对于当前类(getClass() 调用)。

Use:

<property name="myFile" value="/myFile.foo"/>

and read it using:

InputStream is = getClass().getResourceAsStream(myFile);

myFile is a String.

Another approach is to store the file in the same directory as the package of MyBean (/WEB-INF/classes/path/to/myFile.foo) and read it in the following way:

<property name="myFile" value="myFile.foo"/>

Notice there is no leading / - the path is relative to the current class (getClass() invocation).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文