文件上传:
Struts2 文件上传Demo
对象封装:
属性(可以省略),get和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.定义一个Action
xxFileName 不能改
private File[] imgs;
private String[] imgsFileName;
动态获取服务器路径
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完整代码
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/");
System.out.println(path);
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注册到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>
过滤器:设置文件上传格式:扩展名判断
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">jpg,png</param>
</interceptor-ref>
默认上传文件的大小 20M
<constant name="struts.multipart.maxSize" value="20971520"/>
struts2.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>
<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.跳转jsp页面
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>
上传路径
tomcat的安装路径下/webapps/当前项目名称/图片保存路径(之前自己配置的路径)
C:\Program Files\ApacheSoftwareFoundation\Tomcat 8.0\webapps\18-upLoad\image\