如何在java类里获取tomcat容器的绝对路径=在servlet中this.getServletContext().getRealPath("")这个方法
我现在要做的是将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);
}
我的源码:
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);
}
...全文
相关推荐
JAVA获取各种路径总结
()` - `javax.servlet.ServletConfig.getServletContext()`#### Java类中获取路径1.
JAVA中获取各种路径
Servlet中的路径获取#### (1) 获取Web应用根目录:`request.getSession().getServletContext().getRealPath("")`此方法返回Web应用的根目录路径
Java,JSP,Servlet获取当前工程路径(绝对路径)问题解析
本文将详细介绍如何在不同的环境中(如JSP、Servlet、普通Java类)获取当前项目的绝对路径。#### 一、JSP中的路径获取方法1.
java绝对路径和相对路径
- 示例:`String path = getServletContext().getRealPath("/")`#### 三、获取相对路径相对路径是指相对于当前目录的路径。
java中相对路径与绝对路径的问题
,使用 `servletConfig.getServletContext().getRealPath("/")` 来获取当前 web 实际运用的绝对路径。


Web 开发
