index.jsp页面
<form-beans>
<form-bean name="fileForm" type="upload.FileForm"></form-bean>
</form-beans>
<action-mappings >
<action path="/upload" type = "upload.uploadAction" scope="request" name ="fileForm" > //写成“/upload.do”也会提示路径无效
<forward name="success" path="/success.jsp"></forward>
</action>
</action-mappings>
public ActionForward upload2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("++++++++++++++++++++++++++++");
FileForm fileForm = (FileForm)form;
FormFile file = fileForm.getFile();
System.out.println(file);
System.out.println(file.getFileName().toString());
System.out.println(file.getFileData());
//存储路径
String path = request.getSession().getServletContext().getRealPath("/upload");
File dir = new File(path);
if((!dir.exists())){dir.mkdir();}
//设置文件名称
String file1 = file.getFileName().toString();
System.out.println(file1.indexOf('.'));
System.out.println(file1.substring(file1.indexOf('.')+1));
String fileName=UUID.randomUUID().toString()+"."+file1.substring(file1.lastIndexOf('.')+1).toLowerCase();//uuid+源文件后缀
File savaFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(savaFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(file.getFileData());
bos.close();
fos.close();
return mapping.findForward("success");
}
}
//采用action中默认execute的方法
<body>
<form action = "/WebTest/upload.do" method ="post" enctype="multipart/form-data"> //刚开始的时候写成了“/upload”,出错,一直说路径无效
<input type = "file" name = "file" id = "file"/>
<input type = "submit" value = "提交">
</form>
</body>
<form action = "/WebTest/upload.do" method ="post" enctype="multipart/form-data"> //刚开始的时候写成了“/upload”,出错,一直说路径无效
<input type = "file" name = "file" id = "file"/>
<input type = "submit" value = "提交">
</form>
</body>
//采用分派action的方法
<body>
<form action = "/WebTest/upload.do" method ="post" enctype="multipart/form-data">
<input type = "text" name = "upload" id = "upload" value = "upload1"> //做为一个标志表明在分派action中的方法用哪一个方法
<input type = "file" name = "file" id = "file"/>
<input type = "submit" value = "提交">
</form>
</body>
<form action = "/WebTest/upload.do" method ="post" enctype="multipart/form-data">
<input type = "text" name = "upload" id = "upload" value = "upload1"> //做为一个标志表明在分派action中的方法用哪一个方法
<input type = "file" name = "file" id = "file"/>
<input type = "submit" value = "提交">
</form>
</body>
表单提交之后就跳转到了struts-config.xml,中的action配置中,然后跳转到action中execute或者分派action中的指定方法中执行上传。然后成功界面!
<form-beans>
<form-bean name="fileForm" type="upload.FileForm"></form-bean>
</form-beans>
<action-mappings >
<action path="/upload" type = "upload.uploadAction" scope="request" name ="fileForm" > //写成“/upload.do”也会提示路径无效
<forward name="success" path="/success.jsp"></forward>
</action>
</action-mappings>
//运用分派action的方法实现一个action类里面有多个action方法的配置文件,parameter属性的值是从页面传过来的,表示用action中的哪个方法
<struts-config>
<form-beans>
<form-bean name="fileForm" type="upload.FileForm"></form-bean>
</form-beans>
<action-mappings >
<action path="/upload" type = "upload.uploadAction" scope="request" name ="fileForm" parameter="upload">
<forward name="success" path="/success.jsp"></forward>
<forward name="input" path="/success.jsp"></forward>
</action>
</action-mappings>
</struts-config>
<form-beans>
<form-bean name="fileForm" type="upload.FileForm"></form-bean>
</form-beans>
<action-mappings >
<action path="/upload" type = "upload.uploadAction" scope="request" name ="fileForm" parameter="upload">
<forward name="success" path="/success.jsp"></forward>
<forward name="input" path="/success.jsp"></forward>
</action>
</action-mappings>
</struts-config>
action中的execute代码:
package upload;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
//此方法采用默认的执行action中的execute的方法。如果里面有多个action处理就需要修改页面和配置文件,子啊配置文件中添加一个parameter的属性,在页面添加一个用那一个action方法的标志
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
//此方法采用默认的执行action中的execute的方法。如果里面有多个action处理就需要修改页面和配置文件,子啊配置文件中添加一个parameter的属性,在页面添加一个用那一个action方法的标志
(1):默认方法
public class uploadAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("++++++++++++++++++++++++++++");
FileForm fileForm = (FileForm)form; //运用form表单将页面中的file赋值到FileForm
FormFile file = fileForm.getFile(); //FormFile的运用(Struts1)
System.out.println(file); //这里的file和file.getFileName()的值是一样的
System.out.println(file.getFileName().toString());
System.out.println(file.getFileData());
//存储路径
String path = request.getSession().getServletContext().getRealPath("/upload"); //这里是得到了.......WebTest/upload的路径里面
File dir = new File(path);
if((!dir.exists())){dir.mkdir();}
//设置文件名称
String fileName = file.getFileName().toString();
File savaFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(savaFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(file.getFileData());
bos.close();
fos.close();
return mapping.findForward("success");
}
}
public class uploadAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("++++++++++++++++++++++++++++");
FileForm fileForm = (FileForm)form; //运用form表单将页面中的file赋值到FileForm
FormFile file = fileForm.getFile(); //FormFile的运用(Struts1)
System.out.println(file); //这里的file和file.getFileName()的值是一样的
System.out.println(file.getFileName().toString());
System.out.println(file.getFileData());
//存储路径
String path = request.getSession().getServletContext().getRealPath("/upload"); //这里是得到了.......WebTest/upload的路径里面
File dir = new File(path);
if((!dir.exists())){dir.mkdir();}
//设置文件名称
String fileName = file.getFileName().toString();
File savaFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(savaFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(file.getFileData());
bos.close();
fos.close();
return mapping.findForward("success");
}
}
(2):用继承 DispatchAction的方法,实现一个action类中写多个action方法
public class uploadAction extends DispatchAction{
public ActionForward
upload1(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("++++++++++++++++++++++++++++");
FileForm fileForm = (FileForm)form;
FormFile file = fileForm.getFile();
System.out.println(file);
System.out.println(file.getFileName().toString());
System.out.println(file.getFileData());
//存储路径
String path = request.getSession().getServletContext().getRealPath("/upload");
File dir = new File(path);
if((!dir.exists())){dir.mkdir();}
//设置文件名称
String fileName = file.getFileName().toString();
File savaFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(savaFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(file.getFileData());
bos.close();
fos.close();
return mapping.findForward("success");
}
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("++++++++++++++++++++++++++++");
FileForm fileForm = (FileForm)form;
FormFile file = fileForm.getFile();
System.out.println(file);
System.out.println(file.getFileName().toString());
System.out.println(file.getFileData());
//存储路径
String path = request.getSession().getServletContext().getRealPath("/upload");
File dir = new File(path);
if((!dir.exists())){dir.mkdir();}
//设置文件名称
String fileName = file.getFileName().toString();
File savaFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(savaFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(file.getFileData());
bos.close();
fos.close();
return mapping.findForward("success");
}
public ActionForward upload2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("++++++++++++++++++++++++++++");
FileForm fileForm = (FileForm)form;
FormFile file = fileForm.getFile();
System.out.println(file);
System.out.println(file.getFileName().toString());
System.out.println(file.getFileData());
//存储路径
String path = request.getSession().getServletContext().getRealPath("/upload");
File dir = new File(path);
if((!dir.exists())){dir.mkdir();}
//设置文件名称
String file1 = file.getFileName().toString();
System.out.println(file1.indexOf('.'));
System.out.println(file1.substring(file1.indexOf('.')+1));
String fileName=UUID.randomUUID().toString()+"."+file1.substring(file1.lastIndexOf('.')+1).toLowerCase();//uuid+源文件后缀
File savaFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(savaFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(file.getFileData());
bos.close();
fos.close();
return mapping.findForward("success");
}
}
}
success界面:
<body>
<h1>success!</h1>
</body>
<h1>success!</h1>
</body>