java 如何在servlet中获取本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25236036/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:39:21  来源:igfitidea点击:

How to get to a local file in servlet

javatomcatservlets

提问by Snake

I setup Tomcat server locally and I placed my text file in my C drive (c:\test\myfile.txt).

我在本地设置了 Tomcat 服务器并将我的文本文件放在我的 C 驱动器 (c:\test\myfile.txt) 中。

In my servlet, I specify the exact path to the file to read it. I successfully do that.

在我的 servlet 中,我指定了文件的确切路径以读取它。我成功地做到了。

My question is, where should I place the txt file before deployment and how can I navigate to it to read it? I did a test and ran on my local Tomcat

我的问题是,在部署之前我应该​​把 txt 文件放在哪里,我如何导航到它来阅读它?我做了一个测试并在我本地的Tomcat上运行

System.out.println("Working Directory = " +
              System.getProperty("user.dir"));

and it showed me the Eclipse folder! maybe because I run tomcat through eclipse. So I am sure there is either a spcific folder for data or maybe I should create one somewhere in the file system but I need to the relative path.

它向我展示了 Eclipse 文件夹!也许是因为我通过 eclipse 运行 tomcat。所以我确定要么有一个特定的数据文件夹,要么我应该在文件系统的某处创建一个,但我需要相对路径。

Any suggestion is appreciated

任何建议表示赞赏

回答by Ankur Singhal

It's your choice. There are basically three ways:

这是你的选择。基本上有以下三种方式:

Put it in the classpath, so that you can load it by ClassLoader#getResourceAsStream()with a classpath-relative path:

将它放在类路径中,以便您可以ClassLoader#getResourceAsStream()使用类路径相对路径加载它:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));

Here filename.propertiesis supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. Webapp/WEB-INF/lib, Webapp/WEB-INF/classes, Appserver/libor JRE/lib. If the propertiesfile is webapp-specific, best is to place it in WEB-INF/classes. If you're developing a project in an IDE, you can also drop it in src folder (the project's source folder).

这里filename.properties应该放置在 webapp 的默认类路径覆盖的根之一中,例如Webapp/WEB-INF/lib, Webapp/WEB-INF/classes,Appserver/libJRE/lib。如果属性文件是特定于 webapp 的,最好将它放在WEB-INF/classes. 如果您在 IDE 中开发项目,您还可以将其放在 src 文件夹(项目的源文件夹)中。

You can alternatively also put it somewhere outside the default classpath and add its path to the classpath of the appserver. In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties.

您也可以将其放在默认类路径之外的某个位置,并将其路径添加到应用程序服务器的类路径中。例如,在 Tomcat 中,您可以将其配置为Tomcat/conf/catalina.properties.

2.) Put it somewhere in web folder (the project's web content folder), so that you can load it by ServletContext#getResourceAsStream()with a webcontent-relative path:

2.) 将它放在 web 文件夹(项目的 web 内容文件夹)中的某个位置,以便您可以ServletContext#getResourceAsStream()使用 webcontent 相对路径加载它:

Properties properties = new Properties();
properties.load(getServletContext().getResourceAsStream("/WEB-INF/filename.properties"));

Note that I have demonstrated to place the file in /WEB-INFfolder, otherwise it would have been public accessible by any webbrowser. Also note that the ServletContextis in any HttpServletclass just accessible by the inherited GenericServlet#getServletContext().

请注意,我已演示将文件放在/WEB-INF文件夹中,否则任何网络浏览器都可以公开访问它。另请注意,ServletContext位于任何HttpServlet只能由继承的GenericServlet#getServletContext().

3.) Put it somewhere in local disk file system so that you can load it the usual java.ioway with an absolute local disk file system path:

3.) 将它放在本地磁盘文件系统中的某个位置,以便您可以java.io使用绝对本地磁盘文件系统路径以通常的方式加载它:

Properties properties = new Properties();
properties.load(new FileInputStream("/absolute/path/to/filename.properties");

here are many different ways but it depends on your needs:

这里有许多不同的方法,但这取决于您的需求:

To load a property file from $TOMCAT_HOME/conf directoryyou will need to access it using a java.io.Fileobject since the class loader (as in this.getClass().getClassLoader().getResourceAsStream(...)is just able to load files (and classes) from your class path (under WEB-INF/classes, WEB-INF/libor $TOMCAT_HOME/lib).

要从您加载属性文件,$TOMCAT_HOME/conf directory您需要使用java.io.File对象访问它,因为类加载器(如 inthis.getClass().getClassLoader().getResourceAsStream(...)只能从您的类路径(在WEB-INF/classesWEB-INF/lib或下)加载文件(和类$TOMCAT_HOME/lib)。

The easiest example to load a file from the Tomcat's config directorywould be:

从 加载文件的最简单示例Tomcat's config directory是:

File configDir = new File(System.getProperty("catalina.base"), "conf");
File configFile = new File(configDir, "myconfig.properties");
InputStream stream = new FileInputStream(configFile);
Properties props = new Properties();
props.load(stream);

回答by prashant thakre

Place your config file under your webapp WEB-INF/classes folder and read like this in code

将您的配置文件放在您的 webapp WEB-INF/classes 文件夹下,并在代码中读取这样的内容

InputStream is= 
   YourClassName.class.getResourceAsStream("myfile.txt");

You can also try these other option if you want in JSP.

如果你想在 JSP 中,你也可以尝试这些其他选项。

String myfile=
  application.getRealPath("myfile.txt"));

or

String myfile =
  getServletContext().getRealPath("myfile.txt"));