/**
* @param relativePath : 相对classpath路径
*/
public static File getAbsoluteFileInWebContainer(String relativePath) {
File srcFile;
// classpath : {somedirectory}/target/classes/
String os = System.getProperty("os.name");
String classpath = null;
if (os.toLowerCase().startsWith("win")) { // windows
classpath = ConcreteFileUtil.class.getResource("/").getPath().replaceFirst("/", "");
} else { // other
classpath = ConcreteFileUtil.class.getResource("/").getPath();
}
if (!(srcFile = new File(classpath.concat(relativePath))).exists()) {
srcFile = new File(classpath.replaceAll("WEB-INF/classes", "").concat(relativePath));
}
return srcFile.exists() ? srcFile : null;
}
Example:
ConcreteFileUtil.getAbsoluteFileInWebContainer("dict/figures/three-kingdoms-figures.dict"));