注意点
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 <body> 2 <form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data"> 3 文件:<input type="file" name="image"/><br/> 4 <input type="submit" value="上传"/> 5 </form> 6 </body>
多个文件上传
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<images.length;i++) 70 FileUtils.copyFile(images[i], new File(storePath,imagesFileName[i])); 71 } 72 ActionContext.getContext().put("message", "上传成功!"); 73 return SUCCESS; 74 } catch (Exception e) { 75 e.printStackTrace(); 76 return ERROR; 77 } 78 } 79 }
jsp中
1 <body> 2 <form action="${pageContext.request.contextPath}/upload/upload2.action" method="post" enctype="multipart/form-data"> 3 文件1:<input type="file" name="images"/><br/> 4 文件2:<input type="file" name="images"/><br/> 5 <input type="submit" value="上传"/> 6 </form> 7 </body>
struts.xml中配置
设置文件上传大小
1 <constant name="struts.multipart.maxSize" value="52428800"></constant>
1 <package name="upload" namespace="/upload" extends="mypackage"> 2 <action name="upload1" class="cn.itcast.action.UploadAction1" method="execute"> 3 <result name="success">/success.jsp</result> 4 </action> 5 <action name="upload2" class="cn.itcast.action.UploadAction2" method="execute"> 6 <result name="success">/success.jsp</result> 7 </action> 8 </package>
原文:http://www.cnblogs.com/friends-wf/p/3766374.html