FileNotFoundException
使用 Struts2 上传文件时奇怪。这是 JSP 的一部分:
<a:form action="/FileUploadServletAction.action" method="post" enctype="multipart/form-data">
<a:file name="fileUpload" label="File"/>
<a:submit/>
这是 execute() 方法,用于将上传的文件从临时位置复制到实际位置:
public String execute() throws Exception{
try {
String filePath = "c:/foo";
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.fileUploadContentType);
FileUtils.copyFile(this.fileUpload, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
这是我在上面的 Action 类中配置的 struts.xml 部分:
<action name="FileUploadServletAction"
class="com.test.FileUploadServletAction">
<result name="input">/jsp/upload.jsp</result>
<result name="success">/jsp/upload.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
但是当我运行时,我得到了这个异常:
java.io.FileNotFoundException: Source 'E:\Foo\Projects\Foo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\FooProject\upload_1ec6cc50_75d7_482f_83be_fe4185999973_00000000.tmp' does not exist
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1074)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1038)
INFO: Removing file fileUpload E:\Foo\Projects\Foo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\FooProject\upload_1ec6cc50_75d7_482f_83be_fe4185999973_00000000.tmp
谁能告诉我为什么 Struts 无法找到创建的临时文件?如果您需要更多信息,请告诉我。