注意点
private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的 private String imageFileName;// 上传输入域FileName 文件名 private String imageContentType;// 上传文件的MIME类型
单个文件
1 package cn.itcast.action; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.io.Serializable;10 11 import javax.servlet.ServletContext;12 13 import org.apache.commons.io.FileUtils;14 import org.apache.struts2.ServletActionContext;15 16 import com.opensymphony.xwork2.ActionContext;17 import com.opensymphony.xwork2.ActionSupport;18 19 public class UploadAction1 extends ActionSupport implements Serializable {20 21 private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的22 private String imageFileName;// 上传输入域FileName 文件名23 private String imageContentType;// 上传文件的MIME类型24 25 public File getImage() {26 return image;27 }28 29 30 31 public void setImage(File image) {32 this.image = image;33 }34 35 36 37 public String getImageFileName() {38 return imageFileName;39 }40 41 42 43 public void setImageFileName(String imageFileName) {44 this.imageFileName = imageFileName;45 }46 47 48 49 public String getImageContentType() {50 return imageContentType;51 }52 53 54 55 public void setImageContentType(String imageContentType) {56 this.imageContentType = imageContentType;57 }58 59 60 61 public String execute(){62 System.out.println(imageContentType);63 try {64 //处理实际的上传代码65 //找到存储文件的真实路径66 // System.out.println(imageFileName);67 ServletContext sc = ServletActionContext.getServletContext();68 String storePath = sc.getRealPath("/files");69 //构建输入输出流70 // OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);71 // InputStream in = new FileInputStream(image);72 // byte b[] = new byte[1024];73 // int len = -1;74 // while((len=in.read(b))!=-1){75 // out.write(b, 0, len);76 // }77 // out.close();78 // in.close();79 80 FileUtils.copyFile(image, new File(storePath,imageFileName));81 82 ActionContext.getContext().put("message", "上传成功!");83 return SUCCESS;84 } catch (Exception e) {85 e.printStackTrace();86 return ERROR;87 }88 }89 }
jsp中
1 26
多个文件上传
1 package cn.itcast.action; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.io.Serializable;10 11 import javax.servlet.ServletContext;12 13 import org.apache.commons.io.FileUtils;14 import org.apache.struts2.ServletActionContext;15 16 import com.opensymphony.xwork2.ActionContext;17 import com.opensymphony.xwork2.ActionSupport;18 19 public class UploadAction2 extends ActionSupport implements Serializable {20 21 private File[] images;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的22 private String[] imagesFileName;// 上传输入域FileName 文件名23 private String[] imagesContentType;// 上传文件的MIME类型24 25 26 27 public File[] getImages() {28 return images;29 }30 31 32 33 public void setImages(File[] images) {34 this.images = images;35 }36 37 38 39 public String[] getImagesFileName() {40 return imagesFileName;41 }42 43 44 45 public void setImagesFileName(String[] imagesFileName) {46 this.imagesFileName = imagesFileName;47 }48 49 50 51 public String[] getImagesContentType() {52 return imagesContentType;53 }54 55 56 57 public void setImagesContentType(String[] imagesContentType) {58 this.imagesContentType = imagesContentType;59 }60 61 62 63 public String execute(){64 try {65 66 if(images!=null&&images.length>0){67 ServletContext sc = ServletActionContext.getServletContext();68 String storePath = sc.getRealPath("/files");69 for(int i=0;i
jsp中
1 27
struts.xml中配置
设置文件上传大小
1
12 3 5/success.jsp 46 8/success.jsp 7