在上传文件时,会把在war包所在的jboss或者comcat服务器中会生成一个该文件的临时拷贝,并且命名为upload__30fc891_139a068fa26__7ffd_00000005.tmp等形式,这就要考虑到如何把这个临时文件在文件上传后把他删除,有几种获取文件路径的方法,下面分别给出一下:
public static void main(String[] args) throws Exception {
System.out.println(Thread.currentThread().getContextClassLoader()
.getResource(""));
System.out.println(TestPath.class.getClassLoader().getResource(""));
System.out.println(ClassLoader.getSystemResource(""));
// System.out.println(TestPath.class.getResource(""));
System.out.println(TestPath.class.getResource("/")); // Class文件所在路径
System.out.println(new File("/").getAbsolutePath());
System.out.println(System.getProperty("user.dir"));
System.out.println(System.getProperty("user.home"));
System.out.println( request.getSession().getServletContext().getRealPath(
“/”);
}
HttpSession session=request.getSession();
ServletContext application=session.getServletContext();
String ServerRealPath=application.getRealPath("/");
这种方式在本地启动应用时,生成的临时文件为:StoreLocation=D:\Program Files\jboss-tee\jboss\server\default\work\jboss.web\localhost\_\upload__17c67ac4_1399f155857__7ffb_00000014.tmp
D:\Program Files\jboss-tee\jboss\server\default\ 是我本地jboss服务器的安装地址。
在日常下时,临时文件在:
StoreLocation=/home/admin/riskm/.default/work/Catalina/localhost/_/upload__30fc891_139a068fa26__7ffd_00000002.tmp, size=162099bytes, isFormField=false
在日常下通过上面这种方式获取文件路径的方式得到的是:
/home/admin/riskm/.default/deploy/riskm.war/
也就是war包所在的服务器中的安装目录地址,在本地时运行应用时也是这样。