Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

spring - Tomcat deployment file paths (for Java I/O process)

I managed to implement a simple file I/O in my Spring project for files not inside my project (then WAR file) using a .properties file that contains the path (source) and now I need to do it in the server.

Before, when I included the files inside the WAR file, I can access them in the Tomcat server using the path

/home/my_username/tomcat7/webapps/my_project/WEB-INF/classes/

But now that the files are not inside the WAR file, I just put them adjacent to the webapps folder.

The structure of my Tomcat 7 folder is:

tomcat7
|----> bin
|----> conf
|----> logs
|----> resources*
|----> temp
|----> webapps
|--------> springapp.war*
|----> work

where /resources is the folder I need inside the WAR file I/O.

My .properties file has:

dir.server=home/my_username/tomcat7/resources
// or
dir.server=/home/my_username/tomcat7/resources

but an error shows (from my catalina.out file):

java.io.FileNotFoundException: /home/my_username/tomcat7/resources/my_file.file (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.io.FileInputStream.<init>(FileInputStream.java:79)
    at weka.core.SerializationHelper.read(SerializationHelper.java:270)

Is there a proper way to declare the file path or do I have to change my implementation of file I/O?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The reason you are getting a FileNotFoundException is because your deployed war must be on another system or something like that that that doesn't contain the same exact path. You should use this line of code in your program.

String tomcatPath = System.getProperty("catalina.base")

This will return you the path to the tomcat folder in that particular system. Hence if system on which you deployed the war has a different path for tomcat, that will be returned. You can then access the properties file in the resources folder by appending the string to the path.

String tomcatPath = System.getProperty("catalina.base");
String propertiesFilePath = tomcatPath + "/resources";

To access a particular file you can use the code:

String filePath = propertiesFilePath+"my_file.file";

You should take a look at :

How to get Tomcat installation directory using Java.

Hope it helps.. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...