一 新建jsp文件: index.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="action_execute" method="post" enctype="multipart/form-data">
select file:<input type="file" name="upload">
<input type="submit" value="Upload">
</form>
</body>
</html>
注意: 在使用包含文件上传控件的表单时, 必须添加属性: enctype="multipart/form-data"
二 创建Action类:UploadAction.java
添加私有属性:
private String uploadFileName; //上传的文件名
private File upload; //上传的文件
private String uploadContentType; //上传的文件类型
注意: 文件, 文件名,文件类型这三个属性的命名有一定的规则,否则会出现空指针错误, 例如: 若文件为xxx, 则文件名为xxxFileName, 文件类型为xxxContentType
public class UploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//上传文件的文件名
private String uploadFileName;
//上传的文件
private File upload;
//上传文件类型
private String uploadContentType;
...自动生成get, set方法
}
注意: 属性upload必须与index.jsp页面中的name="upload"一致
添加public String execute( )方法
public String execute() throws IOException {
//设置上传文件的保存路径
String target=ServletActionContext.getServletContext().getRealPath("/upload/" + getUploadFileName());
//判断上传的文件是否为空
if(upload != null) {
//根据文件保存路径新建一个File文件实例
File savefile = new File(target);
//判断此路径是否已存在
if(!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
//把上传文件拷贝到新路径下,完成上传
FileUtils.copyFile(upload, savefile);
return "success";
}
//其他情况, 上传失败
return "error";
}
三 在src下创建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.enable.DynamicMethodInvocation" value="true" />
<package name="Upload" namespace="/" extends="struts-default">
<action name="action_execute" class="Upload.UploadAction" method="execute">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
action:
- class: 指定表单处理类的全限定名
- method: 处理类的某个方法
- name: 与index.jsp表单的action属性值相同, 表示该form表单将由class类的method方法来处理, 处理完毕后, 将视返回值来决定跳转到哪个页面:
- 返回值为"success", 则跳转到success.jsp
- 返回值为"error", 则跳转到error.jsp
四 success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>File Uploaded</title>
</head>
<body>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
upload secceed!<br>
file name:<s:property value="+ uploadFileName"/><br>
file type:<s:property value="+ uploadContentType"/><br>
file address:<%=basePath %><s:property value="'uploadFiles/' + uploadFileName"/><br>
</body>
</html>
- 要使用s标签, 则必须在文件头部引入
- request.getContextPath() 获得此Web工程相对与Tomcat的路径: /Struts2
- request.getScheme() 获得使用的协议: http
- request.getServerName() 获得主机名: localhost
- request.getServerPort() 获得端口号: 8080
五 error.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>upload error</title>
</head>
<body>
No File selected!
</body>
</html>
六
启动tomcat服务器, 运行index.jsp, 选择上传的文件, 若成功, 将跳转到success.jsp输出该文件的信息, 失败则跳转error.jsp显示 "No File selected"
七 思考
UploadAction处理类的三个属性: upload, uploadFileName, uploadContentType在没有显式调用set方法的 情况下是如何设置属性值的?