0

I'm trying to upload multiple files using the ExecuteAndWait Interceptor. I've done the file uploading part that works fine, but when I use execAndwait, I got the following error

java.io.FileNotFoundException: C:\Users\hp\AppData\Roaming\NetBeans\8.0.2\config\GF_4.1\domain1\generated\jsp\FileUpload\upload_454f573a_3a8d_4e15_ba96_aa9cb6f4486f_00000002.tmp (The system cannot find the file specified)

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
     <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="file" class="filepack.File_Upload">
            <interceptor-ref name="completeStack"></interceptor-ref>
             <interceptor-ref name="execAndWait">
           <param name="delay">1000</param>
        <param name="delaySleepInterval">50</param>
             </interceptor-ref>

            <result name="ok">result.jsp</result> 
            <result name="wait">wait.jsp</result>
             <result name="invalid.token">invalid_token.jsp</result>      
            <result name="fail">index.jsp</result>

        </action>      
    </package>

</struts>

Action

package filepack;

import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author hp
 */
public class File_Upload extends ActionSupport implements ServletRequestAware{


    public FileBean getFb() {
        return fb;
    }

    public void setFb(FileBean fb) {
        this.fb = fb;
    }

    FileBean fb;
    private HttpServletRequest hsr;

    public HttpServletRequest getHsr() {
        return hsr;
    }

    public void setHsr(HttpServletRequest hsr) {
        this.hsr = hsr;
    }


    FileDAO fd;

    public FileDAO getFd() {
        return fd;
    }

    public void setFd(FileDAO fd) {
        this.fd = fd;
    }

    public String execute() throws FileNotFoundException, IOException,
                                   ClassNotFoundException, SQLException{

    String filepath = hsr.getServletContext().getRealPath("/").concat("file");
      //  String filepath1 = hsr.getSession().getServletContext().getRealPath("/");
        System.out.println("image location :"+filepath);
       String doc= fb.getFile().getName().replace(".tmp", ".jpg");
      String filedb= filepath+"\\"+doc;
       fb.setFiledb(doc);

      String imgpath = hsr.getServletContext().getRealPath("/").concat("Images");
      String img= fb.getImage().getName().replace(".tmp", ".jpg");
       String imagedb= imgpath+"\\"+img;
      //String filedb2= doc;

    fb.setImagedb(img);
    System.out.print("file db:"+fb.getFiledb());

       FileInputStream fin= new FileInputStream(fb.getFile());
       FileOutputStream os= new FileOutputStream(filedb);
        int i=0;
       while((i=fin.read())!=-1){
       os.write(i);      
       }  
       os.close();

        FileInputStream fin1= new FileInputStream(fb.getImage());
       FileOutputStream os1= new FileOutputStream(imagedb);

       int j=0;
         while((j=fin1.read())!=-1){
       os1.write(j);      
       } 
         os1.close();
       fd=new FileDAO();
        setFd(fd);
       if(fd.fileupload(fb).equals("fileupload")){
           return "ok";


       }



        return "error";

    }


    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.hsr=hsr;
    }

}

wait.jsp

<html>
    <head>
     <meta http-equiv="refresh" content="5"/>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>L O A D I N G......!!</h1>
    </body>
</html>

Note: I've found that my code is accepting only one file upload either file or image.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
sufiyan ali
  • 71
  • 1
  • 1
  • 9
  • Your image upload code is totally avoiding the Struts2 conventions. In Struts2 it is a lot **easier** to upload multiple files. [Refer to this answer for a complete example](http://stackoverflow.com/a/17212916/1654265). When you've made it working in the right way, try adding the execAndWait. – Andrea Ligios Nov 04 '15 at 09:19
  • @Andrea- i already mention that i dont have any issue in uploading multiple file except implementing execandwait – sufiyan ali Nov 04 '15 at 10:56
  • You mentioned it, but I'm telling you that you are doing it in a very weird, wrong and unproductive way. If you don't change it now, you will use this way for your next projects too, and coworkers looking your code won't be able to work on it immediately, nor efficiently. Just use it *right*, no matter if it *works*. Follow best practices (or well, at least... practices) :| – Andrea Ligios Nov 04 '15 at 10:57

0 Answers0