我正在尝试使用 ExecuteAndWait 拦截器上传多个文件。我已经完成了可以正常工作的文件上传部分,但是当我使用 execAndwait 时,出现以下错误
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(系统找不到指定的文件)
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;
}
}
等待.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>
注意:我发现我的代码只接受一个文件上传文件或图像。