Action类
package com.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Test extends ActionSupport{
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
@Override
public String execute() throws Exception {
for(int i=0;i<file.size();i++){
if(!file.get(i).exists()){
return INPUT;
}
InputStream is=new FileInputStream(file.get(i));
String root=ServletActionContext.getRequest().getRealPath("/save");
File f=new File(root,this.getFileFileName().get(i));
OutputStream out=new FileOutputStream(f);
byte []b=new byte[1024];
int len=0;
while((len=is.read(b))>0){
out.write(b, 0, len);
}
out.close();
is.close();
}
return SUCCESS;
}
}
struts.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>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<constant name="struts.multipart.maxSize" value="10000000" />
<constant name="struts.custom.i18n.resources"
value="globalMessages"></constant>
<package name="struts2" extends="struts-default">
<action name="upload" class="com.upload.Test">
<interceptor-ref name="fileUpload">
<param
name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/G
PG,image/x-png,image/pjpeg</param>
<param name="maximumSize">90000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/Success.jsp</result>
<result name="input">/Show.jsp</result>
</action>
</package>
</struts>
globalMessages_zh_CN.properties配置文件
struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u5FC5\u987B\u4E3A\u56FE\u7247
struts.messages.error.file.too.large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927
struts.mwssages.error.uploading=\u4E0A\u4F20\u8FC7\u7A0B\u51FA\u73B0\u5F02\u5E38,\u8BF7\u91CD\u8BD5
show.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>
<script type="text/javascript">
function more(){
var td=document.getElementById("more");
var br=document.createElement("br");
var input=document.createElement("input");
var button=document.createElement("input");
input.type="file";
input.name="file";
button.value="删除";
button.type="button";
button.οnclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
<style type="text/css">
ul li{
margin: 0px;
padding: 0px;
list-style-type: none;
}
</style>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.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="upload.action" method="post" enctype="multipart/form-data">
<table width="60%">
<tr><td id="more">
<s:fielderror cssStyle="color:red"></s:fielderror>
<input type="file" name="file">
<input type="button" value="更多" οnclick="more()"/>
</td></tr>
</table>
<input type="submit" value="提交"/>
</form>
</body>
</html>
Success.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 'Success.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>
<p>上传成功</p>
<s:property value="fileFileName"/>
</body>
</html>