<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.multipart.maxSize" value="10701096" /><!-- 最大上传文件大小限制 --> <package name="he" namespace="/hello" extends="struts-default"> <action name="h1_*" class="com.zero.HelloWorldAction" method="{1}"> <result name="success">/WEB-INF/jsp/hello.jsp</result> </action> </package> </struts>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <form action="<%=request.getContextPath()%>/hello/h1_execute" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="aimage"> <input type="submit" value="上传" /> </form> ${message} ${pageContext.request.contextPath} </html>这里<%=request.getContextPath()%>${pageContext.request.contextPath}的值均是项目名称/Struts2。
import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class HelloWorldAction { private File aimage;// 必须与jsp中的一致 private String aimageFileName;// 想要获取文件名,则必须要按照xxxFileName命名 private String aimageContentType;// 获取文件类型,也必须按照xxxContentType命名 public File getAimage() { return aimage; } public void setAimage(File aimage) { this.aimage = aimage; } public String getAimageFileName() { return aimageFileName; } public void setAimageFileName(String aimageFileName) { this.aimageFileName = aimageFileName; } public String upload() { return "success"; } public String getAimageContentType() { return aimageContentType; } public void setAimageContentType(String aimageContentType) { this.aimageContentType = aimageContentType; } public String execute() throws Exception { String realpath = ServletActionContext.getServletContext().getRealPath( "/WEB-INF/upload"); System.out.println(realpath); if (aimage != null) { File savefile = new File(new File(realpath), aimageFileName); if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs(); FileUtils.copyFile(aimage, savefile); ActionContext.getContext().put("message", "上传成功--" + aimageContentType); } return "success"; } }
import java.io.File; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class HelloWorldAction { private List<File> images;// 必须与jsp中的一致,定义为数组[]或List类型均可 private String[] imagesFileName;// 想要获取文件名,则必须要按照xxxFileName命名,定义为数组[]或List类型均可 private String[] imagesContentType;// 获取文件类型,定义为数组[]或List类型均可 public String execute() throws Exception { String realpath = ServletActionContext.getServletContext().getRealPath( "/WEB-INF/upload"); System.out.println(realpath); if (images != null) { File savedir = new File(realpath); if (!savedir.exists()) { savedir.mkdirs(); } for (int i = 0; i < images.size(); i++) { File savefile = new File(new File(realpath), imagesFileName[i]); FileUtils.copyFile(images.get(i), savefile); } ActionContext.getContext().put("message", "上传成功"); } return "success"; } public List<File> getImages() { return images; } public void setImages(List<File> images) { this.images = images; } public String[] getImagesFileName() { return imagesFileName; } public void setImagesFileName(String[] imagesFileName) { this.imagesFileName = imagesFileName; } public String[] getImagesContentType() { return imagesContentType; } public void setImagesContentType(String[] imagesContentType) { this.imagesContentType = imagesContentType; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <form action="<%=request.getContextPath()%>/hello/h1_execute" method="post" enctype="multipart/form-data"> 文件1:<input type="file" name="images"> <br /> 文件2:<input type="file" name="images"> <br /> 文件3:<input type="file" name="images"> <br /> <input type="submit" value="上传" /> </form> ${message} ${pageContext.request.contextPath} </html>