本例展示struts文件上传单个文件(以上传图片为例,可以上传任何类型的文件),并且对将要上传文件的类型和大小进行了限制,同时增加了国际化错误提示!
先看下代码组织结构
下面是详细介绍
步骤:
1.导入jar包(不同版本的struts jar包的版本是不同的 本例中使用xxx代替)
commons-fileupload-xxx.jar
commons-io-xxx.jar
2.搭建struts2环境(点击查看搭建过程)
3.编写上传界面upload.jsp,核心操作enctype=”multipart/form-data”
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginForm.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>
<s:fielderror name="upload"/>
<form action="test/uploadPro" method="post" enctype="multipart/form-data">
上传文件<input type="file" name="upload"/><br/>
文件标题<input type="text" name="title"/><br/>
<input name="tijiao" type="submit" value="提交">
</form>
</body>
</html>
4.编写uploadAction.java 三个核心属性
private File file;//文件
private String file ContentType;//文件类型
private String file FileName;//文件名
注意:倾斜的字符串命名必须一样!
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
//封装文件标题请求参数的属性---表单域中的title
private String title;
//封装上传文件域的属性----表单域中的upload
private File upload;
//封装上传文件类型的属性
private String uploadContentType;
//封装上传文件名的属性
private String uploadFileName;
//直接在struts.xml文件中配置的属性
private String savaPath;
//接收struts.xml文件配置的方法---典型的依赖注入
public void setSavePath(String value){
this.savaPath=value;
}
//返回上传文件的保存位置
private String getSavaPath() throws Exception{
return ServletActionContext.getRequest().getRealPath(savaPath);
}
//title
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
//upload
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
//uploadContentType
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
//uploadFileName
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
String newName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."));
//以服务器的文件保存地址和随机文件名建立上传文件输出流
FileOutputStream fos=new FileOutputStream(getSavaPath()+"\\"+newName);
FileInputStream fis=new FileInputStream(getUpload());
byte [] buffer=new byte[1024];
int len=0;
while((len = fis.read(buffer))>0){
fos.write(buffer,0,len);
}
setUploadFileName(newName);
return SUCCESS;
}
}
5.编写struts.xml
一共有两部分
constant设置一些struts2的常量
package管理action和拦截器
如果不清楚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>
<!-- 配置国际化信息 -->
<constant name="struts.custom.i18n.resources" value="message"></constant>
<!-- 包为lee -->
<package name="lee" extends="struts-default" namespace="/test">
<!--配置action 这个action可以处理各种请求 目的是为了保护jsp页面 将jsp页面放在WEB-INF下 这个action一般放在所有action之后 -->
<action name="*">
<!--直接跳到*所代表的页面 -->
<result>/WEB-INF/content/{1}.jsp</result>
</action>
<action name="uploadPro" class="action.UploadAction" method="execute">
<!-- fileUpload拦截器实现文件过滤,使用拦截器可以轻松的实现文件过滤 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,只允许上传png,bmp格式的图片 -->
<param name="allowedTypes">image/bmp,image/png</param>
<!-- 配置允许上传的文件大小 不超过10W字节-->
<param name="maximumSize">100000</param>
</interceptor-ref>
<!-- 配置系统的默认拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 动态设置action的属性savaPath的值-->
<param name="savePath">/upload</param>
<result>/WEB-INF/content/success.jsp</result>
<!-- 上传失败以后跳转的页面 -->
<result name="input">/WEB-INF/content/upload.jsp</result>
</action>
</package>
</struts>
6.编写上传成功页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.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>
上传成功
<s:property value="uploadFileName"/>
文件是<img src='upload/<s:property value="uploadFileName"/>'>
</body>
</html>
7.配置国际化信息(可以选择不配置,配置的主要原因是为了更好的用户体验)
配置步骤
7.1在struts.xml中添加如下代码,value的值是properties的文件名
<constant name="struts.custom.i18n.resources" value="message"></constant>
7.2编写message.properties
8.在项目中新建upload文件夹,存放上传文件!
测试:
访问路径:http://127.0.0.1:8080/struts2Study24/test/upload
1.当文件类型出错以后
2.当文件大小不匹配
3.当文件类型和大小全部匹配以后上传成功
文件位置:tomcat安装目录/webapps/项目名/upload