如何在java类里获取tomcat容器的绝对路径=在servlet中this.getServletContext().getRealPath("")这个方法

China_Java_zy 2013-01-22 01:54:39
我现在要做的是将servlet中dopost()方法中的代码移植到自己新创建的类里面,但是servlet获取tomcat容器的绝对路径在新建的类里面会报错,有什么方法可以等同于这个this.getServletContext().getRealPath("")方法,在新建的类里面使用
我的源码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Configuration cf = new Configuration();
String in = this.getClass().getClassLoader().getResource("FilePath.properties").getPath();

String url = cf.getValue(in, "key");//得到配置文件properties的路径并读取配置文件properties文件
File folder=new File(url);
File[] fileList = folder.listFiles();
//添加解压路径
String lsPath = this.getServletContext().getRealPath("/temp")+"\\";
String lsPath2 = this.getServletContext().getRealPath("/temp");
String a = "";
File fi = new File(lsPath);
List<String> listPath = new LinkedList<String>();

//找到zip压缩包文件
for(int i=0;i<fileList.length;i++){
String readFile = fileList[i].getName();
if(readFile.endsWith(".zip")){
if(!fi.exists()){
fi.mkdir();
}
//进行解压更新程序
//new Unzip().extZipFileList(fileList[i].getPath(), realPath);
new AntZip().unZip(fileList[i].getPath(), lsPath);
a=lsPath+"\\WEB-INF\\classes\\FilePath1.properties";
new AntZip().unZip(fileList[i].getPath(), cf.getValue(a, "key2"));
listPath.add(cf.getValue(a, "key2")+"\\WEB-INF\\classes\\FilePath1.properties");
// new DeleteFile();
// DeleteFile.delAllFile(lsPath);
// fileList[i].delete();

}
}
System.out.println("解压成功并zip压缩包被删除!!!");
TimeControl tc = new TimeControl();
tc.TimeTask(listPath);
}
...全文
379 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
suciver 2013-01-22
  • 打赏
  • 举报
回复
引用 1 楼 aiqinbird 的回复:
可以这样:XXXServlet.class.getResource("/").getPath();
+1
gqchen1981 2013-01-22
  • 打赏
  • 举报
回复
可以这样:XXXServlet.class.getResource("/").getPath();

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧