java - FileNotFoundException - Struts2 文件上传

标签 java jsp struts2

使用 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;
}

这是我在 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 找不到创建的临时文件?如果您需要更多信息,请告诉我。

最佳答案

我认为您缺少 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>  

操作代码:

//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;
} 

关于java - FileNotFoundException - Struts2 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19923232/

相关文章:

java - Kivy 应用程序崩溃,没有非静态方法 openAPKExpansionInputStream

java - 从数据库到jsp View 页面的用户名和密码验证

java - tomcat中jsp项目文件夹放在哪里托管?

javascript - 在 Nashorn 中的特定上下文中执行函数

java - Struts 1 - 如何显示 ActionMessages

java - JSR-352 Batchlet ejb nullPointerException

java - POST 时不清除表单内容

java - Struts 2 HTTP 错误代码的全局异常映射

java - struts2-json-plugin JSONWriter 注入(inject)实战

java - 如何在struts2 + jfreechart中在同一页面上显示图表