1.创建项目。
2.导入开发包。
3.配置web.xml.
配置内容就是配置struct2的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
4.编写上传文件的Action类:--首先我再webroot文件夹下建了一个upload的文件夹(项目源码中,上传的文件在服务器的该文件夹下)
public class FileUpAction {
//定义好下边的三个字段属性,其中类型跟文件名的前缀一样,file是前台接收的input框的name值跟该字段名一样,并生成get、set方法
//File类型,跟前台的name一致
private File abc;
//上传的文件类型
private String abcContentType;
//上传的文件名--原本的文件名
private String abcFileName;
//这是一个文件上传的类,可以直接是使用
private void doCopyFile(File srcFile, File destFile) throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination ‘" + destFile + "‘ exists but is a directory");
}
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel input = null;
FileChannel output = null;
HttpServletRequest request = ServletActionContext.getRequest();
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
input = fis.getChannel();
output = fos.getChannel();
long size = input.size();
long pos = 0;
long count = 0;
while (pos < size) {
//count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
count = size - pos > 1024*1024*30 ? 1024*1024*30 : size - pos;
long percent = Math.round((double)pos/size*100);
request.getSession().setAttribute("percent", percent);
pos += output.transferFrom(input, pos, count);
}
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(fis);
}
}
//写一个方法执行文件上传
public void upload() throws Exception{
String path = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(new File(path),abcFileName);
doCopyFile(abc, destFile);
}
public File getAbc() {
return abc;
}
public void setAbc(File abc) {
this.abc = abc;
}
public String getAbcContentType() {
return abcContentType;
}
public void setAbcContentType(String abcContentType) {
this.abcContentType = abcContentType;
}
public String getAbcFileName() {
return abcFileName;
}
public void setAbcFileName(String abcFileName) {
this.abcFileName = abcFileName;
}
}
5.配置structs.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性指定上传的开发模式 -->
<constant name="struts.devMode" value="false" />
<!-- 该属性指定上传文件的临时保存路径 -->
<constant name="struts.multipart.saveDir" value="c:/upload" />
<!--上传文件的大小限制 -->
<constant name="struts.multipart.maxSize" value="99999999999" />
<!-- http://127.0.0.1:8080/0613/eg2Action_upload.action -->
<package name="demo01Mg" extends="struts-default">
<!-- 上传配置 -->
<action name="fileup" class="com.demo.FileUpAction" method="upload">
</action>
</package>
</struts>
6.前台去提交上传文件,我再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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!--enctype="multipart/form-data"一定要有,定义的类型。input的type是filen,name就是FileUpAction中的file属性的名字,一定要一致-->
<form action="fileup.action" method="post" enctype="multipart/form-data">
<input type="file" name="abc" /><br /> <input type="submit" id="btn"
value="提交" />
</form>
</body>
</html>
完成上边的就可以启动服务器去访问该项目,《《《《《注意提交后的文件到服务器的uoload文件夹中去了,你工作空间的项目文件夹下没有上传的文件