struts2文件上传的条件:
所需jar包:
一、单文件上传:
文件上传页面demo.jsp:
上传成功返回页面message.jsp:
后台处理:
struts.xml文件:
web.xml文件:
示例效果:
二、多文件上传:
文件上传页面demo.jsp:
后台处理:
struts.xml文件、web.xml文件与单文件上传的内容保持一致。
示例效果:
① 表单提交的方式是post;
② 表单中上传项必须有一个name属性;
③ 表单的enctype属性值是multipart/form-data。
所需jar包:
commons-fileupload-1.2.1.jar
commons-io-2.0.1.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.jar
xwork-core-2.1.6.jar
一、单文件上传:
文件上传页面demo.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>Struts2单文件上传</title>
</head>
<body>
<form action="upload.action" enctype="multipart/form-data" method="post">
<input type="file" name="upload">
<input type="submit" value="上传">
</form>
</body>
</html>
上传成功返回页面message.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>消息页面</title>
</head>
<body>
<h1><font color="green">上传成功!!!</font></h1><br/>
文件名: <s:property value="uploadFileName"/><br/>
文件类型: <s:property value="uploadContentType"/><br/>
图片显示: <br/><img src="<s:property value="#session.img"/>">
</body>
</html>
public class FileUploadAction extends ActionSupport{
private File upload; //上传的文件 , 与页面的name属性名一致
private String uploadFileName; // 上传文件名 ,固定写法:xxFileName
private String uploadContentType; //上传文件类型,固定写法:xxContentType
public String execute() throws Exception{
if (upload != null) {
//获取文件上传绝对路径
String path = ServletActionContext.getServletContext().getRealPath("/files");
//新文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String newFileName = sdf.format(new Date()) + UUID.randomUUID().toString().replace("-", "") +
uploadFileName.substring(uploadFileName.lastIndexOf("."));
//创建文件
File diskFile = new File(path + "//" + newFileName);
//文件上传
FileUtils.copyFile(upload, diskFile);
//传值到页面
String img = "http://localhost:80/struts2upload/files/" + newFileName;
ServletActionContext.getRequest().getSession().setAttribute("img", img);
}
return "success";
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
struts.xml文件:
<struts>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
<constant name="struts.multipart.maxSize" value="10701096"/>
<!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->
<constant name="struts.multipart.saveDir" value="d:/tmp"/>
<package name="upload" extends="struts-default" namespace="/">
<action name="upload" class="test.FileUploadAction">
<result name="success">/WEB-INF/message.jsp</result>
</action>
</package>
</struts>
<!-- 配置struts2核心过滤器 -->
<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>*.action</url-pattern>
</filter-mapping>
二、多文件上传:
文件上传页面demo.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>Struts2多文件上传</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="upload.action" enctype="multipart/form-data" method="post">
<div>
文件1:<input type="file" name="upload"><br/>
文件2:<input type="file" name="upload"><br/>
文件3:<input type="file" name="upload"><br/>
<input type="submit" value="上传"/>
</div>
</form>
</body>
</html>
上传成功返回页面message.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>上传成功</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>
<h1><font color="green">上传成功!!!</font></h1><br/>
<!-- 遍历文件名 -->
<s:iterator value="uploadFileName" var="f" status="s">
文件名<s:property value="#s.index + 1"/>:<s:property value="#f"/>
</s:iterator>
<br><br>
<!-- 遍历文件类型 -->
<s:iterator value="uploadContentType" var="c" status="s">
文件类型<s:property value="#s.index + 1"/>:<s:property value="#c"/>
</s:iterator>
<br><br>
<!-- 遍历图片 -->
<s:iterator value="list" var="l" status="s">
<img src="<s:property value='#l'/>">
</s:iterator>
</body>
</html>
public class FileUpload extends ActionSupport{
private List<File> upload ; //上传的文件 , 与页面的name属性名一致
private List<String> uploadFileName ; // 上传文件名 ,固定写法:xxFileName
private List<String> uploadContentType ; //上传文件类型,固定写法:xxContentType
public String execute() throws Exception {
if (upload != null) {
//获取文件上传绝对路径
String path = ServletActionContext.getServletContext().getRealPath("/files");
List<String> list = new ArrayList<String>();
for (int i = 0; i < upload.size(); i++) {
//新文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String newFileName = sdf.format(new Date()) + UUID.randomUUID().toString().replace("-", "") +
uploadFileName.get(i).substring(uploadFileName.get(i).lastIndexOf("."));
//创建文件
File diskFile = new File(path + "//" + newFileName);
//文件上传
FileUtils.copyFile(upload.get(i), diskFile);
//文件路径
String img = "http://localhost:80/struts2uploads/files/" + newFileName;
list.add(img);
}
//传值到页面
ActionContext.getContext().getValueStack().set("list", list);
}
return "success";
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
示例效果: