一、建立項目
項目名稱: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>
<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目錄下建立頁面
/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部落格,原文連結: