繁体   English   中英

FileNotFoundException - Struts2文件上传

[英]FileNotFoundException - Struts2 File Upload

使用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无法找到创建的临时文件? 如果您需要其他信息,请与我们联系。

我认为你缺少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;
} 

尝试使用inteceptor你的动作应该是这样的..

 <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM