一、創建項目
?? ?項目名稱:demoupload
二、添加jar包
?? ?commons-fileupload-1.2.2.jar
?? ?commons-io-2.0.1.jar
?? ?commons-lang3-3.1.jar
?? ?freemarker-2.3.19.jar
?? ?javassist-3.11.0.GA.jar
?? ?ognl-3.0.5.jar
?? ?struts2-core-2.3.4.1.jar
?? ?xwork-core-2.3.4.1.jar
三、在web.xml文件中配置過濾器
?? ?<?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>struts2</filter-name>
?? ??? ??? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
?? ??? ?</filter>
?? ??? ?<filter-mapping>
?? ??? ??? ?<filter-name>struts2</filter-name>
?? ??? ??? ?<url-pattern>/*</url-pattern>
?? ??? ?</filter-mapping>
?? ??? ?<welcome-file-list>
?? ??? ??? ?<welcome-file>index.jsp</welcome-file>
?? ??? ?</welcome-file-list>
?? ?</web-app>
四、在WebRoot目錄下創建upload目錄用于存放上傳文件
?? ?/upload
五、添加核心配置文件
?? ?1.在項目中創建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄下添加配置文件
?? ??? ?配置文件名稱:struts.xml
?? ??? ?配置文件內容:
?? ??? ?<?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.devMode" value="true" />
?? ??? ? ?? ?<!-- 配置全局文件大小 -->
?? ??? ? ?? ?<constant name="struts.multipart.maxSize" value="21851335"></constant>
?? ??? ? </struts>
六、在WEB-INF下創建pages目錄
?? ?/pages
?? ?在pages目錄下創建頁面
?? ?/pages
?? ??? ?/error.jsp
?? ??? ?/success.jsp
七、創建Action
?? ?1.在src目錄下創建包
?? ??? ?cn.jbit.demoupload.web.action
?? ?2.在包下創建Action
?? ??? ?Action名稱:UploadAction.java
?? ??? ?Action內容:
?? ??? ?public class UploadAction extends ActionSupport {
?? ??? ??? ?private File upload;//文件對象
?? ??? ??? ?private String uploadContentType;//文件類型
?? ??? ??? ?private String uploadFileName;//文件名稱
?? ??? ??? ?public String saveFile(){
?? ??? ??? ??? ?ServletContext servletContext = ServletActionContext.getServletContext();
?? ??? ??? ??? ?String path = servletContext.getRealPath("/upload");
?? ??? ??? ??? ?System.out.println(path+"路徑");
?? ??? ??? ??? ?File newFile = new File(path,uploadFileName);
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?FileUtils.copyFile(upload, newFile);
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return SUCCESS;
?? ??? ??? ?}
?? ??? ??? ?public File getUpload() {
?? ??? ??? ??? ?return upload;
?? ??? ??? ?}
?? ??? ??? ?public void setUpload(File upload) {
?? ??? ??? ??? ?this.upload = upload;
?? ??? ??? ?}
?? ??? ??? ?public String getUploadContentType() {
?? ??? ??? ??? ?return uploadContentType;
?? ??? ??? ?}
?? ??? ??? ?public void setUploadContentType(String uploadContentType) {
?? ??? ??? ??? ?this.uploadContentType = uploadContentType;
?? ??? ??? ?}
?? ??? ??? ?public String getUploadFileName() {
?? ??? ??? ??? ?return uploadFileName;
?? ??? ??? ?}
?? ??? ??? ?public void setUploadFileName(String uploadFileName) {
?? ??? ??? ??? ?this.uploadFileName = uploadFileName;
?? ??? ??? ?}
?? ??? ?}
?? ?3.在struts核心配置文件中添加配置
?? ??? ?<package name="default" namespace="/" extends="struts-default">
?? ? ?? ??? ?<action name="*_uploadAction" class="cn.jbit.demoupload.web.action.UploadAction" method="{1}">
?? ? ?? ??? ??? ?<interceptor-ref name="defaultStack">
?? ? ?? ??? ??? ??? ?<param name="fileUpload.maximumSize">20480</param>
?? ? ?? ??? ??? ??? ?<param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param>
?? ? ?? ??? ??? ??? ?<param name="fileUpload.allowedExtensions">jpg,gif</param>
?? ? ?? ??? ??? ?</interceptor-ref>
?? ? ?? ??? ??? ?<result name="success">/WEB-INF/pages/success.jsp</result>
?? ? ?? ??? ??? ?<result name="input">/WEB-INF/pages/error.jsp</result>
?? ? ?? ??? ?</action>
?? ? ?? ?</package>
八、創建頁面
?? ?頁面名稱:index.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>My JSP 'index.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>
?? ? ?
?? ?? <body>
?? ???? <form action="${pageContext.request.contextPath }/saveFile_uploadAction.action" method="post" enctype="multipart/form-data">
?? ??? ??? ?上傳文件:
?? ??? ??? ?<input type="file" name="upload"/>
?? ??? ??? ?<input type="submit" value="提交"/>
?? ???? </form>
?? ?? </body>
?? ?</html>
?? ?項目名稱:demoupload
二、添加jar包
?? ?commons-fileupload-1.2.2.jar
?? ?commons-io-2.0.1.jar
?? ?commons-lang3-3.1.jar
?? ?freemarker-2.3.19.jar
?? ?javassist-3.11.0.GA.jar
?? ?ognl-3.0.5.jar
?? ?struts2-core-2.3.4.1.jar
?? ?xwork-core-2.3.4.1.jar
三、在web.xml文件中配置過濾器
?? ?<?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>struts2</filter-name>
?? ??? ??? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
?? ??? ?</filter>
?? ??? ?<filter-mapping>
?? ??? ??? ?<filter-name>struts2</filter-name>
?? ??? ??? ?<url-pattern>/*</url-pattern>
?? ??? ?</filter-mapping>
?? ??? ?<welcome-file-list>
?? ??? ??? ?<welcome-file>index.jsp</welcome-file>
?? ??? ?</welcome-file-list>
?? ?</web-app>
四、在WebRoot目錄下創建upload目錄用于存放上傳文件
?? ?/upload
五、添加核心配置文件
?? ?1.在項目中創建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄下添加配置文件
?? ??? ?配置文件名稱:struts.xml
?? ??? ?配置文件內容:
?? ??? ?<?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.devMode" value="true" />
?? ??? ? ?? ?<!-- 配置全局文件大小 -->
?? ??? ? ?? ?<constant name="struts.multipart.maxSize" value="21851335"></constant>
?? ??? ? </struts>
六、在WEB-INF下創建pages目錄
?? ?/pages
?? ?在pages目錄下創建頁面
?? ?/pages
?? ??? ?/error.jsp
?? ??? ?/success.jsp
七、創建Action
?? ?1.在src目錄下創建包
?? ??? ?cn.jbit.demoupload.web.action
?? ?2.在包下創建Action
?? ??? ?Action名稱:UploadAction.java
?? ??? ?Action內容:
?? ??? ?public class UploadAction extends ActionSupport {
?? ??? ??? ?private File upload;//文件對象
?? ??? ??? ?private String uploadContentType;//文件類型
?? ??? ??? ?private String uploadFileName;//文件名稱
?? ??? ??? ?public String saveFile(){
?? ??? ??? ??? ?ServletContext servletContext = ServletActionContext.getServletContext();
?? ??? ??? ??? ?String path = servletContext.getRealPath("/upload");
?? ??? ??? ??? ?System.out.println(path+"路徑");
?? ??? ??? ??? ?File newFile = new File(path,uploadFileName);
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?FileUtils.copyFile(upload, newFile);
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return SUCCESS;
?? ??? ??? ?}
?? ??? ??? ?public File getUpload() {
?? ??? ??? ??? ?return upload;
?? ??? ??? ?}
?? ??? ??? ?public void setUpload(File upload) {
?? ??? ??? ??? ?this.upload = upload;
?? ??? ??? ?}
?? ??? ??? ?public String getUploadContentType() {
?? ??? ??? ??? ?return uploadContentType;
?? ??? ??? ?}
?? ??? ??? ?public void setUploadContentType(String uploadContentType) {
?? ??? ??? ??? ?this.uploadContentType = uploadContentType;
?? ??? ??? ?}
?? ??? ??? ?public String getUploadFileName() {
?? ??? ??? ??? ?return uploadFileName;
?? ??? ??? ?}
?? ??? ??? ?public void setUploadFileName(String uploadFileName) {
?? ??? ??? ??? ?this.uploadFileName = uploadFileName;
?? ??? ??? ?}
?? ??? ?}
?? ?3.在struts核心配置文件中添加配置
?? ??? ?<package name="default" namespace="/" extends="struts-default">
?? ? ?? ??? ?<action name="*_uploadAction" class="cn.jbit.demoupload.web.action.UploadAction" method="{1}">
?? ? ?? ??? ??? ?<interceptor-ref name="defaultStack">
?? ? ?? ??? ??? ??? ?<param name="fileUpload.maximumSize">20480</param>
?? ? ?? ??? ??? ??? ?<param name="fileUpload.allowedTypes">image/pjpeg,image/gif</param>
?? ? ?? ??? ??? ??? ?<param name="fileUpload.allowedExtensions">jpg,gif</param>
?? ? ?? ??? ??? ?</interceptor-ref>
?? ? ?? ??? ??? ?<result name="success">/WEB-INF/pages/success.jsp</result>
?? ? ?? ??? ??? ?<result name="input">/WEB-INF/pages/error.jsp</result>
?? ? ?? ??? ?</action>
?? ? ?? ?</package>
八、創建頁面
?? ?頁面名稱:index.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>My JSP 'index.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>
?? ? ?
?? ?? <body>
?? ???? <form action="${pageContext.request.contextPath }/saveFile_uploadAction.action" method="post" enctype="multipart/form-data">
?? ??? ??? ?上傳文件:
?? ??? ??? ?<input type="file" name="upload"/>
?? ??? ??? ?<input type="submit" value="提交"/>
?? ???? </form>
?? ?? </body>
?? ?</html>
? ??本文轉自 ?素顏豬 ?51CTO博客,原文鏈接:? ??