写了一个Struts2 上传文件的小程序,文件会放在images这个folder 下面,
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
结果总是报错。 错误内容为:
严重: Servlet.service() for servlet [default] in context with path [/Strutstest] threw exception [java.io.FileNotFoundException: Source 'C:\Users\shi edison\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\work\Catalina\localhost\Strutstest\upload_11c9e7e7_15bc986eee5__8000_00000000.tmp'
does not exist] with root cause
java.io.FileNotFoundException: Source 'C:\Users\shi edison\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\work\Catalina\localhost\Strutstest\upload_11c9e7e7_15bc986eee5__8000_00000000.tmp' does not exist
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:767)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:731)
at example.UploadAction.execute(UploadAction.java:21)
错误的代码行数为: FileUtils.copyFile(uploadImage, new File(fileDir,uploadImageFileName))
可知是因为找不到filedir 就是存放的路径,可是我先前有代码去建造这个FOLDER.
if(!fileDir.exists()) fileDir.mkdirs();
按理说不应该没有问题的,百思不得其所。
后面是在WebContent folder 下面 WEB-INF 下面建造“images” 这个我需要使用的 folder后再运行程序就过了。
具体原理还需要探究。