struts2 MVC作为一个比较新的框架,功能非常强大,比之以前的版本1.x 强过太多。
无论是学习还是开发推荐 struts 2, 而且与spring ,Hibernate更容易集成,
还可以方便的与ext 结合做ajax 开发。
不多说,看多文件上传:
环境:JDK 1.6, TOMCAT 5.5.20,
IDE:Myeclipse 6
lib包:struts2-core-2.0.11.1.jar,xwork-2.0.4.jar,freemarker.jar,ognl-2.6.11.jar,
pull-parser-2.1.10.jar,commons-fileupload.jar 必须的包。
代码:
package org;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionSupport;
import java.io.*;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File[] uploadFile;
private String[] contentType;
private String[] uploadFilename;
private String[] caption;
private String[] hide;
public void setHide(String[] hide) {
this.hide = hide;
}
public String[] getCaption() {
return
caption;
}
public void setCaption(String[] caption) {
this.caption = caption;
}
public String[] getContentType() {
return
contentType;
}
public void setUploadFileContentType(String[]
contentType) {
this.contentType = contentType; // notice:here is the
key!! 不要改变命名规范
}
public File[] getUploadFile() {
return
uploadFile;
}
public void setUploadFile(File[] uploadFile)
{
this.uploadFile = uploadFile;
}
public String[] getUploadFilename() {
return
uploadFilename;
}
public void setUploadFileFileName(String[]
uploadFilename) {
this.uploadFilename = uploadFilename; //
notice:here is the key!! 命名约定
}
private static void copy(File src, File dst)
{
try {
InputStream in = null;
OutputStream out =
null;
try {
in = new BufferedInputStream(new
FileInputStream(src));
out = new BufferedOutputStream(new
FileOutputStream(dst));
byte[] buffer = new byte[16 * 1024];
while (in.read(buffer) > 0) {
out.write(buffer);
}
}
finally {
if (null != in) {
in.close();
}
if
(null != out) {
out.close();
}
}
} catch
(Exception e) {
e.printStackTrace();
}
}
public String execute() {
for (String str :
hide) {
System.out.print("--- " + str + " ---\n");
}
for
(int i = 0; i < this.getUploadFile().length; i++) {
File imageFile =
new File(ServletActionContext.getServletContext()
.getRealPath("/")
+ "file/" + uploadFilename[i]);
copy(this.getUploadFile()[i], imageFile);
}
return
SUCCESS;
}
}
classes/目录下,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>
<!-- developer mode convenient for Debug
-->
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK" />
<package name="uploadFile" extends="struts-default">
<action name="myUpload"
class="org.UploadAction">
<interceptor-ref
name="fileUpload">
<param
name="allowedTypes">
image/bmp,image/png,image/gif,image/pjpeg,image/jpg,image/JPG,image/jpeg
</param>
<param
name="maximumSize">
1024000
</param>
</interceptor-ref>
<interceptor-ref
name="defaultStack" />
<result name="input">
/upload.jsp
</result>
<result name="success">
/showUpload.jsp
</result>
</action>
</package>
</struts>
web.xml配置:
<?xml version="1.0"
encoding="gb2312"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<display-name>struts2上传文件示例</display-name>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- 注意顺序 -->
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>
上传页面(ognl 表达式):upload.jsp
<%@ page language="java"
contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="s"
uri="/struts-tags"%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>upload
file</title>
<%
long i = 88;
String[] str = { "aa",
"bb", "cc" };
%>
<script type="text/javascript">
function addMore()
{
var td =
document.getElementById("more");
var ss=
document.createElement("span");
var
br=document.createElement("br");
var input =
document.createElement("input");
var span=
document.createElement("span"); // text zone here
var text =
document.createElement("input");
var button =
document.createElement("input");
ss.innerHTML="select
file:";
input.type = "file";
input.name = "uploadFile";
span.innerHTML="say something:";
text.type="text";
text.name="caption";
button.type = "button";
button.value = "
Remove ";
button.onclick = function()
{
td.removeChild(br); td.removeChild(ss); td.removeChild(input);
td.removeChild(span); td.removeChild(text);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(ss); td.appendChild(input);
td.appendChild(span); td.appendChild(text);
td.appendChild(button);
}
function check(){
if(document.form1.uploadFile == null ||form1.uploadFile == ""|| form1.caption ==
null || form1.caption == "")
{window.alert("请正确输入");
return
false;}
else{
return true;}
}
</script>
</head>
<body>
<s:fielderror
/>
<s:form action="myUpload" method="post"
name="form1"
enctype="multipart/form-data" onsubmit="return check()"
theme="simple">
<table border="1">
<tr>
<td
id="more">select file:<s:file name="uploadFile" /> say
something: <s:textfield name="caption" label="" /> <input
type="button" value="add more.." onclick="addMore()" /></td>
</tr>
<s:hidden name="hide" value="str"/>
<tr>
<td align="center"><s:submit
value="submit file" /> <s:reset
value="reset"
/></td>
</tr>
</table>
</s:form>
</body>
</html>
显示页面:showUpload.jsp
<%@ page language="java"
contentType="text/html;charset=GBK"%>
<%@ taglib prefix="s"
uri="/struts-tags"%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>查看上传文件</title>
</head>
<body>
<center>
<table
border="1">
<s:iterator value="uploadFilename"
status="stat">
<tr>
<td>
<s:property value="%{uploadFilename[#stat.index]}" />
<br
/>
</td>
</tr>
<tr>
<td>
<s:property value="%{caption[#stat.index]}"
/>
</td>
</tr>
</s:iterator>
</table>
</center>
</body>
</html>
浙公网安备 33010602011771号