java相对路径(websphere):
代码如下:
int saveUrlIndex = ProfileModel.class.getResource("").toString().lastIndexOf("WEB-INF");
String saveUrl = ProfileModel.class.getResource("").toString().substring(0,saveUrlIndex).replace("file:/", "") + Constants.IMG_SAVE_PATH;
其中:
ProfileModel: 是当前的类名
ProfileModel.class.getResource("") 是得到这个类编译后的地址.以file:/开头的地址
String saveUrl = ProfileModel.class.getResource("").toString().substring(0,saveUrlIndex)
是截取到"WEB-INF"的上级目录.
Constants.IMG_SAVE_PATH 是个常量,定义了跟"WEB-INF"同级的其他目录.
.........................................
tomcat跟wensphere不一样的只有用这个方法才可以.
以上代码只是笔记,为以后查看用.
代码如下:
int saveUrlIndex = ProfileModel.class.getResource("").toString().lastIndexOf("WEB-INF");
String saveUrl = ProfileModel.class.getResource("").toString().substring(0,saveUrlIndex).replace("file:/", "") + Constants.IMG_SAVE_PATH;
其中:
ProfileModel: 是当前的类名
ProfileModel.class.getResource("") 是得到这个类编译后的地址.以file:/开头的地址
String saveUrl = ProfileModel.class.getResource("").toString().substring(0,saveUrlIndex)
是截取到"WEB-INF"的上级目录.
Constants.IMG_SAVE_PATH 是个常量,定义了跟"WEB-INF"同级的其他目录.
.........................................
tomcat跟wensphere不一样的只有用这个方法才可以.
以上代码只是笔记,为以后查看用.
本文介绍了一种在WebSphere环境下设置Java应用中文件相对路径的方法。通过使用`ProfileModel.class.getResource()`来获取当前类的编译后地址,并进一步解析出目标目录路径。
539

被折叠的 条评论
为什么被折叠?



