在struts2中想要实现文件上传必须要遵守一定的规则,注意是必须,接收文件的名字,属性必须按照规范命名,不然会出现接收不到的现象:
1:首先在index中创建上传表单,表单设置为文件上传格式:
<form action="${pageContext.request.contextPath}/demoUp/upLoad.action" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br>
<input type="submit" value="上传">
</form>
2:编写upload上传处理action
3:在struts.xml中注册action,注意使用文件上传必须使用el表达式获取项目路径+action的路径()
${pageContext.request.contextPath}/demoUp/upLoad.action
调整文件上传大小可以在struts中修改默认设置:
<constant name="struts.multipart.maxSize" value="1000000000"/>
表单处理action,表单校验分为所有方法校验和单个方法校验,如果要对action中的所有方法校验只需要复写validate方法即可,方法内部添加具体的校验方法,如果要对单个方法校验,需要创建一个validate+要校验的方法,例如要校验update方法,则需要创建一个validateUpdate方法来校验,另外要在action中创建input视图:
1:首先在index中创建上传表单,表单设置为文件上传格式:
<form action="${pageContext.request.contextPath}/demoUp/upLoad.action" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br>
<input type="submit" value="上传">
</form>
2:编写upload上传处理action
package com.leige.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UpLoadAction extends ActionSupport {
//命名规范必须是上传文件的name名称
private String imageFileName;//接收文件名字规范是 文件名字+FileName
private File image;//接收文件,必须为和表单中文件对应
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String upLoad(){
ActionContext context=ActionContext.getContext();
//获取当前项目路径
String path=ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(path);
File saveDir=new File(path);
//如果路径不存在则创建
if(!saveDir.exists())saveDir.mkdirs();
try {
FileUtils.copyFile(image, new File(saveDir,imageFileName));
} catch (IOException e) {
// TODO Auto-generated catch block
context.put("message", "上传失败");
System.out.println(e);
return "message";
}
context.put("message", "上传成功");
return "message";
}
}
3:在struts.xml中注册action,注意使用文件上传必须使用el表达式获取项目路径+action的路径()
${pageContext.request.contextPath}/demoUp/upLoad.action
调整文件上传大小可以在struts中修改默认设置:
<constant name="struts.multipart.maxSize" value="1000000000"/>
多文件上传要使用文件数组接收,同样文件的名称也要是String数组,命名规范不变
二:表单校验
1:准备表单
<form action="${pageContext.request.contextPath}/demoUp/validate_update.action" method="post" >
姓名: <input type="text" name="name"><br>
电话:<input type="text" name="phone"><br>
<input type="submit" value="提交">
</form>
<s:fielderror></s:fielderror><!-- -用来显示错误信息 -->
表单处理action,表单校验分为所有方法校验和单个方法校验,如果要对action中的所有方法校验只需要复写validate方法即可,方法内部添加具体的校验方法,如果要对单个方法校验,需要创建一个validate+要校验的方法,例如要校验update方法,则需要创建一个validateUpdate方法来校验,另外要在action中创建input视图:
package com.leige.action;
import java.util.regex.Pattern;
import org.apache.struts2.components.FieldError;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ValidateAction extends ActionSupport {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String add(){
return "input";
}
public String update(){
//返回的参数是input
return "input";
}
public void validateUpdate() {//只校验update方法
// TODO Auto-generated method stub
//校验name
if(name==null||name.trim().equals(""))
this.addFieldError("name", "用户名不可为空");
else if(name.length()<3||name.length()>13){
this.addFieldError("name", "用户名长度必须在3-13之间");
}
//校验电话号码
if(phone==null||phone.trim().equals(""))
this.addFieldError("phone", "电话不可为空");
else if(!Pattern.matches("^1[358]\\d{9}$",phone)){
this.addFieldError("phone", "电话格式不规范");
}
}
/*
*
如果直接复写validate方法会对action中所有方法进行校验
* 如果想对单个方法进行校验可以这样命名 :validate+方法名(首字母大写)
* @Override
public void validate() {
// TODO Auto-generated method stub
//校验name
if(name==null||name.trim().equals(""))
this.addFieldError("name", "用户名不可为空");
else if(name.length()<3||name.length()>13){
this.addFieldError("name", "用户名长度必须在3-13之间");
}
//校验电话号码
if(phone==null||phone.trim().equals(""))
this.addFieldError("phone", "电话不可为空");
else if(!Pattern.matches("^1[358]\\d{9}$",phone)){
this.addFieldError("phone", "电话格式不规范");
}
}*/
}