Java - Java Web应用程序中文件的相对路径 [英] Java - Relative path of a file in a java web application
问题描述
我想从java Web应用程序中读取文件。我不想给出文件的绝对路径。我只是想把文件放在我的网络应用程序的某个目录中。
I want to read a file from a java web application. I don't want to give the absolute path of the file. I just want to put the file in some directory of my web application.
或
它可以被放置以及.war文件(打包的Web应用程序)。
It can be placed along with .war file (packaged web application).
为文件提供的相对路径。我尝试了 ./ filename.csv
但它没有用。
What relative path to give for the file. I tried ./filename.csv
but it didn't work.
========
更新了
================
Updated
========我将发送 WAR
文件(打包的Web应用程序)到我的客户端。此Web应用程序将读取一个文件(例如 SuppliedFile.csv
),该文件将由客户端复制到服务器。所以我需要一种机制(无论应用服务器是否会取消 WAR
)都可以使用,以便Web应用程序可以读取该文件。
I will deliver a WAR
file (packaged web application) to my client. This web application will read a file (lets say SuppliedFile.csv
) which will be copied to the server by the client. So I need a mechanism (that will work irrespective of whether the application server will unpak the WAR
or not) so that web application can read that file.
注意:
我没有在servlet中使用 SuppliedFile.csv
...我我在普通的Java类中使用它...
Note:
I am not using the SuppliedFile.csv
in a servlet... I am using it in a plain Java class...
推荐答案
你真的需要从文件中加载吗?如果你把它放在你的类(在WEB-INF / classes中),你可以使用类加载器获得一个InputStream:
Do you really need to load it from a file? If you place it along your classes (in WEB-INF/classes) you can get an InputStream to it using the class loader:
InputStream csv =
SomeClassInTheSamePackage.class.getResourceAsStream("filename.csv");
这篇关于Java - Java Web应用程序中文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!