<span style="font-family: Arial, Helvetica, sans-serif;">Action部分代码 </span>
</pre><pre name="code" class="java">package com.huchuhan.img_upload.action;
import java.io.File;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadsAction extends ActionSupport {
private static final long serialVersionUID = 1157141200452916950L;
private File[] file;
private String[] fileFileName;
private String[] fileContentType;
private String[] fileName;
public String uploads() throws Exception {
if (file != null && file.length > 0) {
fileName=new String[file.length];
for (int i = 0; i < file.length; i++) {
// 利用时间戳生成不会重复的文件名
fileName[i] = new Date().getTime()
+this.getExtention(fileFileName[i]);
System.out.println(fileName[i]);
// 设置文件在服务器上的存储路径, 以便读取
File newFile = new File(ServletActionContext
.getServletContext().getRealPath(
File.separator + "UploadImages"
+ File.separator + fileName[i]));
System.out.println(newFile.getAbsolutePath());
// 直接使用struts2提供的文件操作工具类, 将要上传的文件存储到服务器
FileUtils.copyFile(file[i], newFile);
}
}else
{
System.out.println("没有文件");
return INPUT;
}
return SUCCESS;
}
// 获得文件扩展名
private String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public void setFileName(String[] fileName) {
this.fileName = fileName;
}
public String[] getFileName() {
return fileName;
}
}
struts.xml部分代码
<span style="color:#330033;"><?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.saveDir" value="C:\"></constant>
<package name="struts2" extends="struts-default">
<action name="uploads" class="com.huchuhan.img_upload.action.UploadsAction" method="uploads">
<result name="success">/jsp/shows.jsp</result>
<result name="input" type="redirect">/jsp/uploads.jsp</result>
</action>
</package>
</struts></span><span style="color:#ff0000;">
</span>
uploads.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/jsp/share/taglib.jsp" %>
<!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>Uploads</title>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
var i=1;
function addMore()
{
$("#submit").css("display","inline");
var addButton=document.getElementById("add");
addButton.value="add more...";
var table=document.getElementById("table");
if(table.childNodes.length==1)
{
$("#del").css("display", "inline");
}
var tr=document.createElement("tr");
var td1=document.createElement("td");
var td2=document.createElement("td");
var td3=document.createElement("td");
var file=document.createElement("input");
var button=document.createElement("input");
file.type="file";
file.name="file";
button.type="button";
button.value="remove";
button.οnclick=function()
{
if(table.childNodes.length==1)
{
addButton.value="add";
$("#del").css("display", "none");
$("#submit").css("display","none");
}
table.removeChild(tr);
};
td1.innerHTML="file"+i;
td1.align="center";
td2.appendChild(file);
td3.appendChild(button);
td3.align="center";
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
table.appendChild(tr);
i++;
}
function delAll(o)
{
var table=document.getElementById("table");
//在ie下,对于COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR,innerHTML属性是只读的。
//也就是说你不能通过innerhtml对这些标签进行操作。
//table.innerHTML=""; //ie 不支持, 用以下代码代替.
var childs=table.childNodes;
var length=childs.length;
for(var i=0;i<length;i++)
{
table.removeChild(childs[0]);
}
var addButton=document.getElementById("add");
addButton.value="add";
$("#submit").css("display","none");
$(o).css("display", "none");
}
</script>
</head>
<body>
<s:form action="uploads" method="post" enctype="multipart/form-data" theme="simple">
<table border="0">
<tr>
<td align="center">
<input id="add" type="button" value="add" οnclick="addMore()"/>
</td>
<td align="center">
<input id="del" type="button" value="delAll" οnclick="delAll(this)" style="display:none; "/>
</td>
</tr>
</table>
<table id="table" border="0"></table>
<input type="submit" id="submit" value="submit" style="display: none;"/>
</s:form>
</body>
</html>
shows.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/jsp/share/taglib.jsp" %>
<!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>show img</title>
</head>
<body>
<c:forEach items="${fileName}" var="v">
<div style="padding: 3px; border: solid 1px #cccccc; text-align:center">
<img src ="../UploadImages/${v}"/>
<br/>
</div>
</c:forEach>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Img-Upload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 引入struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>