配置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.multipart.saveDir" value="f:/testwork"></constant>
<package name="upload" namespace="/upload" extends="struts-default">
<action name="upload" class="star.july.upload.UploadAction" method="upload">
<!-- 修改文件上传参数,配置拦截器的参数 -->
<!-- struts2内置的FileUploadInterceptor拦截器 -->
<!-- fileUpload:默认包下声明的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 文件大小限制:10M -->
<param name="maximumSize">10485760</param>
<!-- 文件类型限制:mime类型 -->
<!-- 允许类型 -->
<!-- <param name="allowedTypes">image/jpg,image/jpeg,image/gif</param> -->
<!-- 允许的文件后缀 -->
<param name="allowedExtensions">jpg,jpeg,gif</param>
</interceptor-ref>
<!-- 继承系统默认拦截器,不继承则获取不了文件信息 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 定义服务器目录,接收文件 -->
<param name="savePath">f:/testwork/tempFiles/</param>
<!-- 定义错误视图INPUT -->
<result name="input">/upload.jsp</result>
<result name="success">/index.jsp</result>
</action>
<!-- 多个文件上传 -->
<action name="uploadMulti" class="star.july.upload.UploadActionMulti" method="upload">
<!-- 修改文件上传参数,配置拦截器的参数 -->
<!-- struts2内置的FileUploadInterceptor拦截器 -->
<!-- fileUpload:默认包下声明的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 文件大小限制:10M -->
<param name="maximumSize">10485760</param>
<!-- 文件类型限制:mime类型 -->
<!-- 允许类型 -->
<!-- <param name="allowedTypes">image/jpg,image/jpeg,image/gif</param> -->
<!-- 允许的文件后缀 -->
<param name="allowedExtensions">jpg,jpeg,gif</param>
</interceptor-ref>
<!-- 继承系统默认拦截器,不继承则获取不了文件信息 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 定义服务器目录,接收文件 -->
<param name="savePath">f:/testwork/tempFiles/</param>
<!-- 定义错误视图INPUT -->
<result name="input">/upload.jsp</result>
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
UploadAction:
package star.july.upload;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
//文件内容
private File attach;
//文件名称
private String attachFileName;
//文件类型
private String attachContentType;
public void setAttach(File attach) {
this.attach = attach;
}
public void setAttachFileName(String attachFileName) {
this.attachFileName = attachFileName;
}
public void setAttachContentType(String attachContentType) {
this.attachContentType = attachContentType;
}
//普通邮件信息
private String email;
public void setEmail(String email) {
this.email = email;
}
//定义服务器目录
private String savePath;
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload() throws Exception{
//输出文件信息
System.out.println(attach);
System.out.println(attachFileName);
System.out.println(attachContentType);
System.out.println(email);
//拷贝文件到服务器,存入硬盘
FileUtils.copyFile(attach, new File(savePath+attachFileName));
return SUCCESS;
}
}
多文件上传,用数组或集合
UploadActionMulti.java
package star.july.upload;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
/**
* 多个文件上传
* 可以使用数组或集合解决
* @author Starjuly
*
*/
public class UploadActionMulti extends ActionSupport{
//文件内容
private File[] attach;
//文件名称
private String[] attachFileName;
//文件类型
private String[] attachContentType;
public void setAttach(File[] attach) {
this.attach = attach;
}
public void setAttachFileName(String[] attachFileName) {
this.attachFileName = attachFileName;
}
public void setAttachContentType(String[] attachContentType) {
this.attachContentType = attachContentType;
}
//普通邮件信息
private String email;
public void setEmail(String email) {
this.email = email;
}
//定义服务器目录
private String savePath;
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload() throws Exception{
//遍历输出文件信息
for(int i = 0 ; i<attach.length;i++){
//输出文件信息
System.out.println(attach[i]);
System.out.println(attachFileName[i]);
System.out.println(attachContentType[i]);
//拷贝文件到服务器,存入硬盘
FileUtils.copyFile(attach[i], new File(savePath+attachFileName));
}
System.out.println(email);
return SUCCESS;
}
}
upload.jsp网页(单文件)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>title</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 错误视图返回信息 -->
<!-- <s:fielderror></s:fielderror> -->
<form action="<c:url value='/upload/upload'/>" method="post" enctype="multipart/form-data">
文件上传:<input type="file" name="attach"/><br>
邮箱:<input type="text" name="email"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- <%@ taglib uri="/struts-tags"prefix="s" %> --%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>多个文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 错误视图返回信息 -->
<!-- <s:fielderror></s:fielderror> -->
<form action="<c:url value='/upload/uploadMulti'/>" method="post" enctype="multipart/form-data">
文件上传:<input type="file" name="attach"/><br>
文件上传:<input type="file" name="attach"/><br>
文件上传:<input type="file" name="attach"/><br>
文件上传:<input type="file" name="attach"/><br>
邮箱:<input type="text" name="email"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>