Tomcat部署文件路径(用于Java I / O进程) [英] Tomcat deployment file paths (for Java I/O process)
问题描述
在我的项目(然后 WAR
文件)内的文件中实现一个简单的文件I / O。属性
包含路径的文件(源),现在我需要在服务器中 之前,当我将文件包含在 WAR
文件中时,我可以使用Tomcat服务器访问它们路径
/ home / my_username / tomcat7 / webapps / my_project / WEB-INF / classes /
$但是现在这些文件不在 WAR
文件内,我只是把它们毗邻 webapps
文件夹。
我的Tomcat 7文件夹的结构是:
tomcat7
| ----> bin
| ----> conf
| ---->日志
| ---->资源*
| ----> temp
| ----> webapps
| ---- \ ----> springapp.war *
| ---->工作
其中 / resources
是我的文件夹需要在 WAR
文件I / O内。
我的 .properties
文件有:
dir.server = home / my_username / tomcat7 / resources
//或
dir.server = / home / my_username / tomcat7 / resources
但错误显示(从我的 catalina.out
file):
java.io.FileNotFoundException: home / my_username / tomcat7 / resources / my_file.file(没有这样的文件或目录)
在java.io.FileInputStream.open(本机方法)
在java.io.FileInputStream。< init>( FileInputStream.java:120)
在java.io.FileInputStream。< init>(FileInputStream.java:79)
在weka.core.SerializationHelper.read(SerializationHelper.java:270)
是否有正确的方式来声明文件路径,或者是否需要更改文件I / O的实现?
解决定案您获得 FileNotFoundException
的原因是因为部署的 war
必须在另一个系统或类似的东西,不包含相同的确切路径。您应该在程序中使用这一行代码。
String tomcatPath = System.getProperty(catalina.base)
这将返回到该特定系统中tomcat文件夹的路径。因此,如果您部署了 war
的系统对tomcat具有不同的路径,则将返回该路径。然后,您可以通过将字符串附加到路径来访问资源文件夹中的属性文件。
String tomcatPath = System.getProperty catalina.base);
String propertiesFilePath = tomcatPath +/ resources;
要访问特定文件,您可以使用代码:
String filePath = propertiesFilePath +my_file.file;
你应该看看:
希望它有帮助..:)
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?
解决方案 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.. :)
这篇关于Tomcat部署文件路径(用于Java I / O进程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!