导入jar包
/StrutsUpLoad/WebContent/WEB-INF/lib/asm-5.2.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/asm-commons-5.2.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/asm-tree-5.2.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/commons-dbutils-1.6.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/commons-fileupload-1.3.3.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/commons-io-2.5.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/commons-lang3-3.6.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/freemarker-2.3.26-incubating.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/javassist-3.20.0-GA.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/jsp-api.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/log4j-api-2.9.1.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/log4j-core-2.9.1.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/mysql-connector-java-5.1.40-bin.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/ognl-3.1.15.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/servlet-api.jar
/StrutsUpLoad/WebContent/WEB-INF/lib/struts2-core-2.5.16.jar
/StrutsUpLoad/WebContent/WEB-INF/load
/StrutsUpLoad/WebContent/WEB-INF/ScrutsJsps
/StrutsUpLoad/WebContent/WEB-INF/ScrutsJsps/hello.jsp
index/xml
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/file.action"
enctype="multipart/form-data" method="post">
<input type="file" name="model.imageFile"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
配置web。xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
配置Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="p1" extends="struts-default">
<action name="file" class="com.xinxue.Controller.FileUplOAD" >
<result>
/WEB-INF/ScrutsJsps/hello.jsp
</result>
</action>
</package>
</struts>
util 文件上传帮助类
package com.xinxue.Utils;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class FileModelUtils {
private File imageFile;
private String imageFileContentType;
private String imageFileFileName;
public File getImageFile() {
return imageFile;
}
public void setImageFile(File imageFile) {
this.imageFile = imageFile;
}
public String getImageFileContentType() {
return imageFileContentType;
}
public void setImageFileContentType(String imageFileContentType) {
this.imageFileContentType = imageFileContentType;
}
public String getImageFileFileName() {
return imageFileFileName;
}
public void setImageFileFileName(String imageFileFileName) {
this.imageFileFileName = imageFileFileName;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
public void HanderFileUpLoad()
{
try{
//得到上传文件的真实路径
String fileDirPath = ServletActionContext.getServletContext()
.getRealPath("/WEB-INF/load");
System.out.println(fileDirPath);//打印输出路径的真实地址
//将上面这个路劲放到文件夹中 然后需要做判断这个文件是否真实 存在 如果不存在 那么需要我们新建一个
File fileDir = new File(fileDirPath);
//如果这个文件不存在 那么就自动创建一个
if(!fileDir.exists())
{
fileDir.mkdirs();
}
//系统时间+截取文件的后缀名
File newFile = new File(fileDir,System.nanoTime() + getImageFileFileName().
substring(getImageFileFileName().lastIndexOf(".")));
//把临时存储的文件 拷贝 到我们新创建的文件中
FileUtils.copyFile(getImageFile(), newFile);
}catch(Exception e)
{
throw new RuntimeException(e.getMessage()+"failed");
}
}
}
model 调度室
package com.xinxue.Controller;
import com.opensymphony.xwork2.Action;
import com.xinxue.Utils.FileModelUtils;
public class FileUplOAD {
public FileModelUtils model = new FileModelUtils();
public String execute() 默认的执行方法 所以在xml里面没有;配方法
{
model.HanderFileUpLoad();
return Action.SUCCESS; 默认的返回成功的方法 所以在Struts.xml里面没有配返回的name
}
}