Java - Java Web 应用程序中文件的相对路径 [英] Java - Relative path of a file in a java web application
问题描述
我想从 Java Web 应用程序读取文件.我不想给出文件的绝对路径.我只想将该文件放在我的 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屋!