如何在servlet中获取本地文件 [英] How to get to a local file in servlet

查看:258
本文介绍了如何在servlet中获取本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地安装了Tomcat服务器,并将文本文件放在我的C盘中(c:\\\ myfile.txt)。

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

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

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

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

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

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

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

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.

任何建议都表示赞赏

推荐答案

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

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

将它放在类路径中,以便你可以通过 ClassLoader#getResourceAsStream()使用类路径相对路径:

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

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

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

Here filename.properties is 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/lib or 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).

你也可以把它放在一个以外的地方默认类路径并将其路径添加到appserver的类路径。在例如Tomcat中,您可以将其配置为 Tomcat / conf / catalina.properties 的shared.loader属性。

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.

2.)将它放在web文件夹(项目的web内容文件夹)中,以便您可以通过 ServletContext#getResourceAsStream()加载它,并使用webcontent-relative路径:

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:

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

请注意,我已证明将文件放在 / WEB-INF 文件夹,否则任何webbrowser都可以公开访问它。另请注意, ServletContext 位于任何 HttpServlet 类中,只能由继承的 GenericServlet#getServletContext访问()

Note that I have demonstrated to place the file in /WEB-INF folder, otherwise it would have been public accessible by any webbrowser. Also note that the ServletContext is in any HttpServlet class just accessible by the inherited GenericServlet#getServletContext().

3。)将它放在本地磁盘文件系统中的某处,这样就可以加载通常的 java.io 使用绝对本地磁盘文件系统路径的方式:

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

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

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

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

$ TOMCAT_HOME / conf目录中加载属性文件您将需要使用 java.io.File 对象访问它,因为类加载器(如 this.getClass()) .getClassLoader()。getResourceAsStream(...)只能从类路径加载文件(和类)(在 WEB-INF / classes WEB-INF / lib $ TOMCAT_HOME / lib )。

To load a property file from $TOMCAT_HOME/conf directory you will need to access it using a java.io.File object 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/lib or $TOMCAT_HOME/lib).

Tomcat的配置目录加载文件的最简单示例是:

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

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);

这篇关于如何在servlet中获取本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆