存储在 tomcat 目录中不是一个好主意,在部署期间可能会删除该目录
我建议 Redis 这样做 http://www.springsource.org/spring-data/redis ,或者如果您想存储在嵌入式数据库中,请在使用 windows 和 unix 的用户家中创建一个目录文件
System.getProperty("user.home");
如果在环境变量中设置了 CATALINA_HOME,你可能会得到 tomcat 主目录
由
System.getProperty("catalina.base");
或者你可以使用spring消息包使用绝对路径,所以你可以添加dir.properties文件并添加到spring config中:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/dir" />
<property name="defaultEncoding" value="UTF-8" />
在文件 dir.properties 中,添加路径:
storage:/var/lib/tomcat6/
或者,如果您有兴趣动态查找 Os,可以使用此代码获取特定目录:
public class FindOS {
private static final boolean osIsMacOsX;
private static final boolean osIsWindows;
private static final boolean osIsWindowsXP;
private static final boolean osIsWindows2003;
private static final boolean osIsWindowsVista;
private static final boolean osIsLinux;
static {
String os = System.getProperty("os.name");
if (os != null)
os = os.toLowerCase();
osIsMacOsX = "mac os x".equals(os);
osIsWindows = os != null && os.indexOf("windows") != -1;
osIsWindowsXP = "windows xp".equals(os);
osIsWindows2003 = "windows 2003".equals(os);
osIsWindowsVista = "windows vista".equals(os);
osIsLinux = os != null && os.indexOf("linux") != -1;
}
public static boolean isMacOSX() {
return osIsMacOsX;
}
public static boolean isWindows() {
return osIsWindows;
}
public static boolean isWindowsXP() {
return osIsWindowsXP;
}
public static boolean isWindows2003() {
return osIsWindows2003;
}
public static boolean isWindowsVista() {
return osIsWindowsVista;
}
public static boolean isLinux() {
return osIsLinux;
}
//TODO
String getHelperDirectory(){
if(isLinux())return "~/";
if(isWindows()) return "c:/";
if(osIsWindowsVista) return "c:/";
return null;
}