Pradeep Simha
Pradeep Simha

Reputation: 18133

FileNotFoundException - Struts2 File Upload

Strange FileNotFoundException while uploading file using Struts2. This is a part of JSP:

<a:form action="/FileUploadServletAction.action" method="post" enctype="multipart/form-data">
<a:file name="fileUpload" label="File"/>
<a:submit/>

This is the execute() method, to copy uploaded file from temporary location to actual location:

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

This is my portion of struts.xml which configures above Action class:

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

But when I run I get this exception:

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

Can anyone let me know why Struts is not able to find temporary file which is created? Please let me know if you need additional information.

Upvotes: 4

Views: 2139

Answers (2)

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12993

I think you are missing getter and setter methods, I don't know have you defined or not?

JSP code:

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

Now, execute() method

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

Upvotes: 2

subash
subash

Reputation: 3115

try with inteceptor your action should like this..

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

Upvotes: 1

Related Questions