1.index.jsp首页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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="file2" method="Post" enctype="multipart/form-data">
姓名:<input name="name" /></br>
年龄:<input name="age" /></br>
性别:<input name="sex" /></br>
照片1:<input name="photo" type="file" /></br>
照片2:<input name="photo" type="file" /></br>
照片3:<input name="photo" type="file" /></br>
<input name="submit" type="submit" /></br>
</form>
</body>
</html>
2.action对应的类
package cn.acion;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class FileAction {
private String name;
private Integer age;
private String sex;
// 设置成数组,存放多个图片
private File[] photo;
private String[] photoFileName;
private String savePath;
private String[] imgSrc;
public String execute(){
ServletContext context = ServletActionContext.getServletContext();
this.imgSrc = new String[this.photo.length];
//获取路径
String realSavePath = context.getRealPath(savePath);
File saveDirectory = new File(realSavePath);
if( !saveDirectory.exists() )
saveDirectory.mkdir();
//用循环对图片处理
for( int i = 0;i<this.photo.length;i++ ){
String photoFileName = this.photoFileName[i];
String saveFileName = UUID.randomUUID().toString();
String ext = photoFileName.substring(photoFileName.lastIndexOf("."));
String outFilePath = realSavePath + "/" + saveFileName + ext;
this.imgSrc[i] = this.savePath + "/" + saveFileName + ext;
this.imgSrc[i] = imgSrc[i].substring(1);
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(photo[i]));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
int tmp = -1;
while( (tmp = bis.read()) != -1 ){
bos.write(tmp);
}
bos.flush();
bos.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return Action.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public File[] getPhoto() {
return photo;
}
public void setPhoto(File[] photo) {
this.photo = photo;
}
public String[] getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String[] getImgSrc() {
return imgSrc;
}
public void setImgSrc(String[] imgSrc) {
this.imgSrc = imgSrc;
}
}
3.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="file2" class="cn.acion.FileAction">
<param name="savePath">/upload</param>
<result>/success.jsp</result>
</action>
</package>
</struts>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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 'succ.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>
姓名:${name } <br>
年龄:${age }<br/>
性别:${sex }<br/>
照片列表:
<c:forEach items="${imgSrc}" var="src">
<img src="${src }" />
</c:forEach>
</body>
</html>