1复制HeadFirstStruts2chapter07 改名:HeadFirstStruts2chapter08,删除所有的包和类。新建fileUpload.jsp,form表单 enctype="multipart/form-data"实现二进制传输,
<%@ 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>
<form action="upLoad" method="post" enctype="multipart/form-data">
文件:<input type="file" name="cruise"/>
<input type="submit" value="提交文件"/>
</form>
</body>
</html>
2success.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>
文件上传成功!!!
</table>
</body>
</html>
3新建FileUploadAction,继承ActionSupport,文件:cruise与jsp中的name属性一致,文件名cruiseFileName,文件类型cruiseContentType;这几个属性的写法是固定的
package com.cruise.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File cruise;
private String cruiseFileName;
private String cruiseContentType;
public File getCruise() {
return cruise;
}
public void setCruise(File cruise) {
this.cruise = cruise;
}
public String getCruiseFileName() {
return cruiseFileName;
}
public void setCruiseFileName(String cruiseFileName) {
this.cruiseFileName = cruiseFileName;
}
public String getCruiseContentType() {
return cruiseContentType;
}
public void setCruiseContentType(String cruiseContentType) {
this.cruiseContentType = cruiseContentType;
}
@Override
public String execute() throws Exception {
String filePath="C:/Users/pengc/Desktop/"+cruiseFileName;
File saveFile = new File(filePath);
FileUtils.copyFile(cruise, saveFile);
return SUCCESS;
}
}
4配置struts2.xml文件,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="manager" extends="struts-default">
<action name="upLoad" class="com.cruise.action.FileUploadAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
5测试
http://localhost:8080/HeadFirstStruts2chapter08/fileUpload.jsp
6配置struts2.xml文件,引用拦截器fileUpload,设置上传文件的类型和大小,如果上传的文件被拒绝,返回“input”字符串,fileUpload是固定写法,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="manager" extends="struts-default">
<action name="upLoad" class="com.cruise.action.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/fileUpload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
<param name="maximumSize">81101</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
7修改fileUpload.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:fielderror></s:fielderror>
<form action="upLoad" method="post" enctype="multipart/form-data">
文件:<input type="file" name="cruise"/>
<input type="submit" value="提交文件"/>
</form>
</body>
</html>
8测试
http://localhost:8080/HeadFirstStruts2chapter08/fileUpload.jsp
9修改struts.xml文件,修改文件上传的大小限制,struts默认是 不超过2M,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="10000000"></constant>
<package name="manager" extends="struts-default">
<action name="upLoad" class="com.cruise.action.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/fileUpload.jsp</result>
</action>
</package>
</struts>
10测试
http://localhost:8080/HeadFirstStruts2chapter08/fileUpload.jsp