struts2 可以很简单的实现文件上传,具体内容如下:
strut.xml---strut2 配置文件
<?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.custom.i18n.resources" value="message"></constant>
<constant name="struts.multipart.saveDir" value="D:/"></constant>
<constant name="struts.multipart.maxSize" value="60240000"></constant>
<package name="login23" extends="struts-default" namespace="/test">
<action name="upload2" class="com.test.action.UploadAction" >
<result name="success" type="dispatcher">/uploadresult.jsp</result>
<result name="input" type="dispatcher">/uploadlist2.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/html</param>
<param name="maximumSize">409600</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
message_zh_CN.properties---struts2资源文件
struts.messages.error.content.type.not.allowed=/u4E0A/u4F20/u6587/u4EF6/u7C7B/u578B/u4E0D/u5141/u8BB8/uFF0C/u8BF7/u91CD/u8BD5 /!
//配置上传文件类型不对的错误信息
struts.messages.error.file.too.large=/u4E0A/u4F20/u6587/u4EF6/u8FC7/u5927/uFF0C/u8BF7/u91CD/u8BD5 /!
//配置上传文件过大的错误信息
UploadAction 类package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 6150106717429376504L;
private String username ;
private String password;
private List<File> file;
private List<String> fileFileName;
private List<String>fileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
@SuppressWarnings("deprecation")
public String execute() throws Exception {
String path=ServletActionContext.getRequest().getRealPath("/upload");
int length=0;
if(file!=null){
length=file.size();
}
for(int i=0;i<length;i++){
FileInputStream is=new FileInputStream(file.get(i));
FileOutputStream os=new FileOutputStream(new File(path,fileFileName.get(i)));
byte []buffer=new byte[300];
int len=0;
while((len=is.read(buffer))>0){
os.write(buffer, 0, len);
}
os.close();
is.close();
return SUCCESS;
}
return "input";
}
}
Uploadlist.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 'upload.jsp' starting page</title>
<SCRIPT type="text/javascript">
function addMore(){
//用JavaScript实现可以上传任意数量的文件
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";
//确保name的值与UploadAction的属性,页面file的值的一致
button.type="button"
button.value="删除";
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
//给remove 这个按钮注册事件
button.οnclick=function(){
//点击删除button可以去除一个输入框
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
}
</SCRIPT>
</head>
<body>
<!--提交方法post,form enctype 改为'multipart/form-data'-->
<s:form action="upload2" theme="simple" method="post"
enctype="multipart/form-data" namespace="/test">
<table border='1' cellpadding="3" bordercolor="blue" cellspacing="3"
width="420px">
<caption>上传文件</caption>
<tr>
<td>
username:
</td>
<td>
<s:textfield name="username" cssStyle="width:180px" />
</td>
</tr>
<tr>
<td>
password:
</td>
<td>
<s:password name="password" cssStyle="width:180px" />
</td>
</tr>
<tr>
<td>
file :
</td>
<td id="more">
<s:file name="file" />
<input type="button" value="继续添加" οnclick="addMore()"/>
</td>
</tr>
<tr>
<td colspan="2" ><s:fielderror cssStyle="color:red" /></td>
<!--显示上传出错的信息,配置在message_zh_CN.properties 文件中-->
</tr>
<tr>
<td colspan="2" align="right" >
<s:submit value="submit" />
</td>
</tr>
</table>
</s:form>
</body>
</html>
uploadresult.jsp
//简单的显示提交信息
<body>
username :<s:property value="username"></s:property><br/>
password :<s:property value="password"></s:property><br/>
fileUpload:
<ul>
<s:iterator id="item" value="fileFileName"><li><s:property value="item"/></li></s:iterator>
</ul>
<p>上传成功!<a href="uploadlist2.jsp">点击返回继续上传!</a></p>