Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

so it gives me the right path when i execute it in a class as java application C:Documents and SettingsAdministrateurBureau eady codeJavaServerFacesProject

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

C:Documents and SettingsAdministrateurBureaueclipseeclipse

So any idea how to do it Many thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
176 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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.

Never use the working directory

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.

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 WindowsSystem32

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

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

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

ex:

in the web.xml you set

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

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:appstorageuploaded"
      type="java.lang.String"
      override="false" />
</Context>

Check tomcat documentation

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...