我有一个使用 Apache Tomcat 6.0 的简单 Web 应用程序。我正在尝试从路径“resources/mysql.properties”读取属性文件。这里的“resources”文件夹在“src”文件夹之外。当我尝试将项目作为 Java 应用程序运行时,它工作正常。但是当我在服务器上运行它时它会抛出 FileNotFoundException。
这是我在控制台上运行的带有 main 的 Java 代码。
package com.jm.test;
import com.jm.util.PropertyUtil;
public class CodeTester {
public static void main(String[] args) {
System.out.println(PropertyUtil.getDBPropertyValue("driver"));
}
}
这是PropertyUtil.Java文件的代码。
/**
*
*/
package com.jm.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* @author Jaydeep Ranipa
*
*/
public class PropertyUtil {
public static final Logger log = Logger.getLogger(PropertyUtil.class);
private static final String resourceDir = "resources";
private PropertyUtil() {
}
private static Properties loadProperties(String fileName) throws IOException, FileNotFoundException {
System.out.println("filename: "+fileName);
InputStream fileStream = new FileInputStream(new File(fileName));
Properties props = new Properties();
props.load(fileStream);
return props;
}
public static String getDBPropertyValue(String key) {
Properties prop = null;
String value = "";
String fileName = "localmysql.properties";
try {
prop = loadProperties(resourceDir + "/" + fileName);
value = prop.getProperty(key);
if (value == null) {
throw new MissingResourceException("Property not found.", "DATABASE", key);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("properties file not found");
e.printStackTrace();
log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> not found.");
value = "Error occured.";
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("properties file reading failed");
log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> reading failed.");
value = "Error occured.";
} catch (MissingResourceException e) {
// TODO: handle exception
System.out.println("property not found");
log.error("Property <<<"+e.getKey()+">>> for <<<"+e.getClassName()+">>> not found.");
value = "Error occured.";
}
return value;
}
public static String getErrorMessage(String errorCode) {
Properties prop = null;
String message = "";
String fileName = "errormessage.properties";
try {
prop = loadProperties(resourceDir + "/" + fileName);
message = prop.getProperty(errorCode);
if (message == null) {
throw new MissingResourceException("Property not found.", "ERROR", errorCode)
}
} catch (FileNotFoundException e)
// TODO Auto-generated catch block
log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> not found.")
message = "Something went wrong.";
} catch (IOException e) {
// TODO Auto-generated catch bloc
log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> reading failed.");
message = "Something went wrong.";
} catch (MissingResourceException e) {
// TODO: handle exception
log.error("Property <<<"+e.getKey()+">>> for <<<"+e.getClassName()+">>> not found.");
message = "Something went wrong.";
}
return message
}
}
以下代码在通过网络服务器执行时给出了错误。
Class.forName(PropertyUtil.getDBPropertyValue("driver"));
con = DriverManager.getConnection(PropertyUtil.getDBPropertyValue("url"),
PropertyUtil.getDBPropertyValue("username"),
PropertyUtil.getDBPropertyValue("password"));
return con;
最佳答案
How to find the working folder of a servlet based application in order to load resources
查看此线程而不是:
InputStream fileStream = new FileInputStream(new File(fileName));
尝试:
InputStream fileStream = getServletContext().getResourceAsStream(fileName);
关于java - 在 Apache Tomcat 上运行时出现 FileNotFound 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40978172/