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

久未见 提交于 2020-01-06 08:03:50

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21858488/tomcat-deployment-file-paths-for-java-i-o-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!