关于虚拟路径和绝对路径(Tomcat)

来源:互联网 发布:c语言输出buffer内容 编辑:程序博客网 时间:2024/06/06 03:16


server.xml相关配置如下:

 

request.getServletPath()得到访问路径------------------>/index.jsp

request.getContextPath()得到虚拟目录名字------------>/anyName

this.getServletContext().getContextPath() 也是得到虚拟目录名字-------------->/anyName

this.getServletContext().getRealPath("/")得到当前虚拟目录下对应的真实路径------>C:\zzzz\

注:this.getServletContext()就是一个application对象


利用jspsmart上传文件时,如果使用绝对路径保存,则代码如下:

 String path = this.getServletContext().getRealPath("/"); path = path.replace('\\', '/') + "files/" + file.getFileName(); file.saveAs(path, SmartUpload.SAVE_AUTO);
其中,files文件夹是建在WebRoot目录下的,上传完之后在D:\myEclipse\.metadata\.me_tcat7\webapps

\LoginDemo(web应用名)\files中可看到上传的文件,但在myeclipse的WebRoot目录下的files文件夹中看不到上传的文件。郁闷,新手,不知所以然。。。有知道的欢迎留言告知!!!