1、新建一个WEB项目。
2、导入struts2所用的jar。
3、新建或复制struts2的配置文件(struts.xml)。
4、在web.xml文件中配置struts2功能。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>strut2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>strut2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5、文件上传的页面。(indes.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>Struts2 Common File Upload</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>Struts2 文件上传到服务器</h3> <form action="fileUpload" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="fileImage"/> <input type="submit" value="上传"/> </form> <s:fielderror/> </body> </html>
6、填写配置struts.xml文件的action。
<!DOCTYPE struts PUBLIC "-//Aoacge Siftware Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="com.upload.imageupload" extends="struts-default"> <action name="fileUpload" class="com.upload.imageupload.ImageFileUpload" method="execute"> <result name="success">upload.jsp</result> <result name="error">error.jsp</result> <!-- 动态设置savePath的属性值 --> <!-- <param name="savePath">/images</param> --> <interceptor-ref name="fileUpload"> <!-- 文件过滤 --> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> <!-- 文件大小, 以字节为单位 --> <param name="maximumSize">1024*20</param> </interceptor-ref> <!-- 默认拦截器必须放在fileUpload之后,否则无效 --> <interceptor-ref name="defaultStack" /> <result name="input">index.jsp</result> </action> </package> </struts>7、上传成功的页面。(upload.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> <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 图片上传成功! <br></br> <img src="${pageContext.request.contextPath}/<s:property value="'images/'+fileImageFileName"/>"> <s:debug></s:debug> </body> </html>
8、上传失败的页面。(error.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 图片上传失败! </body> </html>
9、上传中文的图片。
在tomcat的server.xml中加入URIEncoding="utf-8" (网页的编码是utf-8)
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8" redirectPort="8443" />