使用struts2中的execandwait拦截器上传多个文件

时间:2015-11-04 08:53:34

标签: java file-upload struts2 interceptor struts2-interceptors

我尝试使用ExecuteAndWait Interceptor上传多个文件。我已经完成了文件上传部分工作正常,但是当我使用execAndwait时,我收到以下错误

  

java.io.FileNotFoundException:   C:\ Users \用户马力\应用程序数据\漫游\的NetBeans \ 8.0.2 \配置\ GF_4.1 \ domain1的\生成\ JSP \文件上传\ upload_454f573a_3a8d_4e15_ba96_aa9cb6f4486f_00000002.tmp   (系统找不到指定的文件)

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>

动作

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>

注意:我发现我的代码只接受一个文件上传文件或图片。

0 个答案:

没有答案