File Upload:
Struts2 file upload Demo
Object encapsulation:
properties (can be omitted), get and set, tostring
1.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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>My JSP 'index.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>
<form action="Login" method="post" enctype="multipart/form-data">
<input type="file" name ="imgs"><br>
<input type="file" name ="imgs"><br>
<input type="file" name ="imgs"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
2. Define an Action
xxFileName 不能改
private File[] imgs;
private String[] imgsFileName;
Get server path dynamically
for (int i = 0; i < imgs.length; i++) {
String path = ServletActionContext.getServletContext().getRealPath("/image/");
File destFil = new File(path, imgsFileName[i]);
FileUtils.copyFile(imgs[i], destFil);
}
LoginAction complete code
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class LoginAction {
private File[] imgs;
private String[] imgsFileName;// xxFileName 不能改
public String execute() {
try {
// String path="c:/image";
if (imgs != null) {
// 如何获取服务器上的路径,上传一个文件夹
// 动态获取服务器根路径
for (int i = 0; i < imgs.length; i++) {
String path = ServletActionContext.getServletContext()
.getRealPath("/image/");
File destFil = new File(path, imgsFileName[i]);
FileUtils.copyFile(imgs[i], destFil);
}
return "success";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "input";
}
public File[] getImgs() {
return imgs;
}
public void setImgs(File[] imgs) {
this.imgs = imgs;
}
public String[] getImgsFileName() {
return imgsFileName;
}
public void setImgsFileName(String[] imgsFileName) {
this.imgsFileName = imgsFileName;
}
}
3 Register to struts.xml
<package name="xxx" extends="struts-default">
<action name="Login" class="com.lanou3g.action.LoginAction" >
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
</package>
Filter: set file upload format: extension judgment
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">jpg,png</param>
</interceptor-ref>
The default upload file size is 20M
<constant name="struts.multipart.maxSize" value="20971520"/>
struts2.xml complete code
<?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>
<package name="xxx" extends="struts-default">
<action name="Login" class="com.action.LoginAction" >
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
<!--扩展名-->
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">jpg,png</param>
</interceptor-ref>
</action>
</package>
<!--
修改默认上传文件大小
默认是2M
-->
<constant name="struts.multipart.maxSize" value="20971520"/>
</struts>
4. Jump to jsp page
sucess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
</head>
<body>
<h2>上传成功!</h2>
</body>
</html>