一、导入文件上传需要的jar
- commons-fileupload-1.3.1.jar
- commons-io-1.3.2.jar
- commons-logging-1.0.4.jar
- freemarker-2.3.22.jar
- ognl-3.0.6.jar
- struts2-core-2.3.24.jar
- xwork-core-2.3.24.jar
二、文件上传注意事项
(1) 在JSP中设定表单控件
<input type="file" name="upload">
(2) form表单的enctype必须写成multipart/form-data
<form action="${pageContext.request.contextPath}/up" method="post" enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" value="上传">
</form>
(3) form表单的method必须写成post
<form action="${pageContext.request.contextPath}/up" method="post" enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" value="上传">
</form>
(4) Action中定义属性
private File upload; // 上传的文件
private String uploadContentType; // 上传文件的mimeType类型
private String uploadFileName; // 上传文件的名称
三、示例代码
3.1文件上传Action
(1)单文件上传UploadAction
package com.struts2.fileupload.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File upload; // 上传的文件
private String uploadContentType; // 上传文件的mimeType类型
private String uploadFileName; // 上传文件的名称
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;
}
public void uploadFile() {
//获取文件保存的地址
String path = ServletActionContext.getServletContext().getRealPath("/upload"); // 得到upload目录在tomcat的绝对磁盘路径
// 创建一个服务器端文件
File dest = new File(path, uploadFileName);
try {
// 完成文件上传操作
FileUtils.copyFile(upload, dest);
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2) 多文件上传UploadManyAction
package com.struts2.fileupload.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadManyAction extends ActionSupport {
private File[] upload; // 上传的文件
private String[] uploadContentType; // 上传文件的mimeType类型
private String[] uploadFileName; // 上传文件的名称
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;
}
public void uploadFile() {
String path = ServletActionContext.getServletContext().getRealPath("/upload"); // 得到upload目录在tomcat的绝对磁盘路径
try {
for (int i = 0; i < upload.length; i++) {
// 创建一个服务器端文件
File dest = new File(path, uploadFileName[i]);
// 完成文件上传操作
FileUtils.copyFile(upload[i], dest);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2 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>
<package name="default" namespace="/" extends="struts-default">
<!-- 单一文件上传 -->
<action name="up" class="com.struts2.fileupload.action.UploadAction" method="uploadFile">
<result name="input">/error.jsp</result>
<!-- 设置Action的filter -->
<interceptor-ref name="fileUpload">
<!-- 设置每一个action允许的上传文件大小 -->
<!-- <param name="maximumSize"></param> -->
<!-- 设置上传允许的文件的allowedTypes类型,多个使用逗号分开 -->
<!-- <param name="allowedTypes"></param> -->
<!-- 设置上传文件允许的后缀名,多个使用逗号分开 -->
<param name="allowedExtensions">jpg,bmp,txt</param>
</interceptor-ref>
<!-- 这个默认的拦截器栈必须引入,否则所有的默认拦截器将失效 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!-- 多个文件上传 -->
<action name="upMany" class="com.struts2.fileupload.action.UploadManyAction" method="uploadFile">
<result name="input">/error.jsp</result>
<!-- 设置Action的filter -->
<interceptor-ref name="fileUpload">
<!-- 设置每一个action允许的上传文件大小 -->
<!-- <param name="maximumSize"></param> -->
<!-- 设置上传允许的文件的allowedTypes类型,多个使用逗号分开 -->
<!-- <param name="allowedTypes"></param> -->
<!-- 设置上传文件允许的后缀名,多个使用逗号分开 -->
<param name="allowedExtensions">jpg,bmp,txt</param>
</interceptor-ref>
<!-- 这个默认的拦截器栈必须引入,否则所有的默认拦截器将失效 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
3.3 jsp文件
(1) 单文件上传jsp
<%@ 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}/up" method="post" enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" value="上传">
</form>
</body>
</html>
(2) 多文件上传jsp
<%@ 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}/upMany" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br>
<input type="file" name="upload"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
(3) error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:actionerror/>
<s:fielderror/>
</body>
</html>
3.4 web.xml文件配置
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2-day03</display-name>
<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>struts2Filter</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
四、文件类型