1.新建fileUpload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>上传页面</title> </head> <body> <s:form namespace="/sys" action="FileUpload" enctype="multipart/form-data" method="post"> <s:file name="upload"></s:file> <s:submit value="提交"></s:submit> </s:form> </body> </html>
2.配置struts.xml
<package name="sys" namespace="/sys" extends="default"> <action name="*" class="web.{1}Action"> <result>/success.jsp</result> </action> </package>
3.新建class FileUploadAction.class
package web; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.servlet.ServletContext; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.Action; public class FileUploadAction implements ServletContextAware{ private File upload; private String uploadFileName; private String uploadContentType; private ServletContext servletContext; public String execute() throws IOException { //输入流 FileInputStream fis = null; //输出流 FileOutputStream fos = null; //获得upload的绝对路径 String path = servletContext.getRealPath("upload"); //将图片名称带上当前时间,避免图片名称重复 this.setUploadFileName(System.currentTimeMillis() + uploadFileName); //图片的绝对路径 path = path + "/" + uploadFileName; try { fis = new FileInputStream(upload); fos = new FileOutputStream(path); byte[] bytes = new byte[1024]; while ((fis.read(bytes)) != -1) { fos.write(bytes); } } finally { if (fos != null) fos.close(); if (fis != null) fis.close(); } return Action.SUCCESS; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } @Override public void setServletContext(ServletContext context) { // TODO Auto-generated method stub this.servletContext = context; } }
4.新建成功页面 success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> </head> <body> <img alt="" src="upload/<s:property value="uploadFileName"/>"/> <s:debug/> 上传成功. <br> </body> </html>
5.发布,启动tomcat,访问fileUpload.jsp,上传图片,成功后跳转至success.jsp,并显示刚刚上传的图片