2013年9月17日星期二

In Struts2 , by the following statement to get the true path , why so strange to get a result ?

String realpath = ServletActionContext.getServletContext (). GetRealPath ("/ images");

The result is :
D: \ java \. metadata \. plugins \ org.eclipse.wst.server.core \ tmp2 \ wtpwebapps \ Struts2 \ images

And

String realpath = ServletActionContext.getServletContext (). getRealPath ("/ images");

System.out.println (realpath);
File saveFile = new File (new File (realpath), imageFileName);


create a file when prompted null pointer exception:

java.lang.NullPointerException
java.io.File. (File.java: 334)
cn.dzr.action.FileUploadAction.execute (FileUploadAction.java: 44)
sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java: 57)
sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java: 43)
java.lang.reflect.Method.invoke (Method.java: 601)

I wanted to get a path that works for my tomcat directory .
------ Solution ---------------------------------------- ----
System.out.pring (System.dir);
like this
------ Solution --------------------------------- -----------
System.out.println (System.getProperty ("user.dir"));
is this , the current project path
------ Solution ------------------------------ --------------
in the end is the tomcat directory or project directory
------ For reference only -------------- -------------------------
issue is resolved , the original case of default , eclipse down here to generate a web project .

没有评论:

发表评论