获取Eclipse项目的绝对路径(在使用tomcat服务器的Web应用程序中) [英] getting eclipse project absolute path (in web application using tomcat server)

查看:130
本文介绍了获取Eclipse项目的绝对路径(在使用tomcat服务器的Web应用程序中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Eclipse项目的绝对路径: 所以我写这个

i'm trying to get the absolute path of my eclipse project : so i write this;

   File file= new File("");
   System.out.println(file.getCanonicalPath());
   projectPath=file.getCanonicalPath();

所以当我在作为Java应用程序C:\Documents and Settings\Administrateur\Bureau\ready code\JavaServerFacesProject

so it gives me the right path when i execute it in a class as java application C:\Documents and Settings\Administrateur\Bureau\ready code\JavaServerFacesProject

但是当我尝试在我的Web应用程序中使用它时,它会给出以下信息:

But when i try to use it in my web application it gives this :

C:\Documents and Settings\Administrateur\Bureau\eclipse\eclipse

所以任何想法怎么做 非常感谢

So any idea how to do it Many thanks

推荐答案

从Web应用程序读取文件是一个典型的陷阱,每当您这样做时,都会遭受痛苦.

Reading a file from a web application is a classical pitfall, whenever you do this you will suffer.

不认为Web应用程序(尤其是Java EE)将文件系统用于读/写操作,它们依赖于容器(在您的情况下为Tomcat)知道如何获取所需资源的事实.

Web apps (especially Java EE) are not thought out to use the file-system for read/write operation, they rely on the fact that the container (in your case Tomcat) knows how to get the needed resources.

所以基本的建议是:不要这样做.

So the basic advice is : don't do it.

但是,由于我基本上讨厌这种答案,所以我会给你一些建议.

But since I basically hate this kind of answers I will give you some advice.

从不使用工作目录

您永远都不知道您的工作目录在哪里,而且在任何生产系统中,您常常不知道该Web应用程序无权在该工作目录上进行写.

You never know where your working directory is, and, more often then not, in any production system, the web-app has no right to write on the working directory.

例如,如果将Web应用程序部署到tomcat服务器,并在Windows计算机上作为服务运行,则会发现您的工作目录为\Windows\System32

For example, if you deploy your webapp to a tomcat server, run as a service on a windows machine, you'll find that your working directory is \Windows\System32

例如,您真的不想在其中写入一些上传的文件...

You really don't want to write some uploaded files there for example...

您有几种选择,我更喜欢在web-xml中设置一个路径,并可能从服务器配置中覆盖它(使用上下文).

You have a few option, what I prefer is to set a path into the web-xml and possibly override it from server configuration (using context).

第二个选项(甚至更好)是将路径保存到Web应用程序访问的db表中.

Second option, even better is, to save a path into a db-table accessed by the web app.

例如:

在您设置的web.xml中

in the web.xml you set

<context-param>
  <description>Uploaded files directory</description>
  <param-name>file-storage</param-name>
  <param-value>c:\app\storage\uploaded</param-value>
</context-param>

然后在server.xml中(或者您可以使用上下文目录并在其中放置带有以下代码的文件),您可以在上下文中覆盖此设置.

Then in the you server.xml (or you could use the context dir and place there a file with the following code) you override this setting in context.

<Context
    <Parameter
      name="file-storage"
      value="E:\app\storage\uploaded"
      type="java.lang.String"
      override="false" />
</Context>

查看tomcat文档

第三种选择,在稍微快乐的情况下,您只想编写一些临时文件,有一个webapp工作目录可以作为名为servlet上下文参数的方式访问: javax.servlet.context.tempdir

Third option, in the slightly happier situation, you want to write just some temporary file, there is the webapp working dir accessible as a servlet context param named : javax.servlet.context.tempdir

如果我在哪里,我会去数据库表.

If I where you I would go for the database table.

所有这些复杂性是因为您可以在tomcat的不同实例上(甚至在不同的机器上,甚至在不同的Web应用程序上)拥有同一个应用程序的多个实例,因此没有简单的方法可以使相对"路径就是所有情况.

All this complexity is because you can have multiple instance of the same app on different instances of tomcat, even on different machines, and even different web application into the same instance, so there is no easy way to make a 'relative' path is all situations.

大多数Web应用程序都决定在有必要时对db上的数据进行序列化(使用lob或类似的对象),或者依靠某种服务(FTP,cif,nfs ...).

Most web-app resolve to serialize data on db (using lobs or similar objects), whenever there is such necessity, or to rely on some kind of service (FTP, cifs, nfs ...).

这篇关于获取Eclipse项目的绝对路径(在使用tomcat服务器的Web应用程序中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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