文件未找到异常 - Struts2 文件上传

4

使用Struts2上传文件时出现奇怪的FileNotFoundException。这是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;
}

这是我在struts.xml文件中配置上述Action类的部分内容:
<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找不到创建的临时文件?如果需要更多信息,请告诉我。

2个回答

2
我认为您缺少getter和setter方法,我不知道您是否已经定义了这些方法? JSP代码:
<form action="FileUploadServletAction" method="post" enctype="multipart/form-data">
  <label>File:</label><input type="file" name="userKey"/>
  <input type="image" src="images/login-btn.jpg" alt="submit" width="103" height="42"/>
</form>  

Action code:

//In FileUploadServletAction
private File userKey;  //file name which is on JSP
private String userKeyContentType;
private String userKeyFileName;  

//getter, setter  
public File getUserKey() 
{
    return userKey;
}

public void setUserKey(File userKey) 
{
    this.userKey = userKey;
}

public String getUserKeyFileName()
{
    return userKeyFileName;
} 

public String getUserKeyContentType() 
{
    return userKeyContentType;
}

public void setUserKeyContentType(String userKeyContentType)
{
    this.userKeyContentType = userKeyContentType;
}

public void setUserKeyFileName(String userKeyFileName)
{
    this.userKeyFileName = userKeyFileName;
}  

现在,execute() 方法。
//In FileUploadServletAction
public String execute() throws Exception{
 try {
      String filePath = request.getSession().getServletContext().getRealPath("/");           
      File fileToCreate = new File(filePath, this.userKeyFileName);
      FileUtils.copyFile(this.userKey, fileToCreate);
  } catch (Exception e) {
    e.printStackTrace();
    addActionError(e.getMessage());
    return INPUT;
 }

 return SUCCESS;
} 

嗨@Aniket,是的,我已经为所有字段定义了getter/setter。 - Pradeep Simha
@PradeepSimha: 你可以试一下我的代码吗?我有同样的需求,根据你的帖子进行了修改。对我而言它是有效的。 - Aniket Kulkarni

1

尝试使用拦截器,你的操作应该像这样...

 <action name="FileUploadServletAction"  class="com.test.FileUploadServletAction">
            <interceptor-ref name="fileUpload">
                <param name="maximunSize">1024000</param>
                <param name="allowedTypes">
                     your types
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="input">/jsp/upload.jsp</result>
            <result name="success">/jsp/upload.jsp</result>
            <result name="error">/jsp/error.jsp</result>
    </action>

我已经为了简单起见进行配置,但仍然遇到相同的异常。 - Pradeep Simha
尝试使用以下代码:**String path = httpServletRequest.getSession().getServletContext().getRealPath("/");**。创建一个HttpServletRequest对象及其getter和setter方法。这是文件找不到的原因。如果您想要自定义路径,请在web.xml中说明。 - subash
已添加,但问题仍然存在。 - Pradeep Simha
1
我的Action类链接:http://codeviewer.org/view/code:38d7,web.xml链接:http://codeviewer.org/view/code:38d8,struts.xml链接:http://codeviewer.org/view/code:38d9。因为我无法在此处发布完整文件。 - Pradeep Simha
是的,在fileUpload中我得到了正确的对象。它显示了临时文件的路径。 - Pradeep Simha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接