批量上传
JSP 页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String webcontent = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JET欢迎你</title>
</head>
<script type="text/javascript">function addMore()
{
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
var span=document.createElement("span");
input.type = "file";
input.name = "upload";
button.type = "button";
button.value = "删除";
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
td.removeChild(span);
}
input.οnchange=function(){
clearTooltip(this);
checkExt(this);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(span);
td.appendChild(button);
}</script>
<body>
<%--
<form action="<%=webcontent %>/company/add.action" method="post">
<input type="text" name="companyname" />
<input type="submit" value="提交" />
</form>
--%>
<s:form validate="true" method="post" theme="simple" enctype="multipart/form-data"
action="/company/add.action">
<table align="center" width="50%" border="1">
<tr>
<td align="left">公司名称</td>
<td align="left">
<s:textfield name="company.company_name"
maxlength="50"></s:textfield>
</td>
</tr>
<tr>
<td align="left" valign="top">上传文件</td>
<td id="more">
<s:file name="upload" οnchange="clearTooltip(this);checkExt(this);"></s:file>
<span></span>
<input type="button" value="添加多个" οnclick=addMore();>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<s:submit value="确定"></s:submit>
<s:reset value="重置"></s:reset>
</td>
<s:token></s:token>
<s:fielderror></s:fielderror>
</tr>
</table>
</s:form>
</body>
</html>
action
上传
@Action(value = "add", results = {
@Result(name = "success", type = "redirect", location = "/company/findAll.action"),
@Result(name = "input", location = "/company/companyAdd.jsp") }, interceptorRefs = {
@InterceptorRef(value = "fileUploadStack"),
@InterceptorRef(value = "defaultStack") })
public String add() {
String msg = "success";
String upload_files = "";
savePath = "/userfiles/images";
if (upload.size() > 0) {
try {
upload_files = Imagesupload.imageup(getUploadContentType(),
getSavePath(), getUpload());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message = "文件上传失败,类型不允许或文件大小受限!";
}
}
try {
company.setUpload_files(upload_files);
companyService.add(company);
message = "添加成功";
} catch (Exception e) {
message = "添加失败";
e.printStackTrace();
}
return msg;
}
下载用到的 注解
@Results( { @Result(name = "download", type = "stream", params = { "contentType", "application/vnd.ms-excel",
"inputName", "inputStream", "contentDisposition", "attachment;filename=\"${downloadFileName}\"", "bufferSize",
"4096" }) })
struts2.3.4.1+hibernate4.1.6+spring3.1.2 完整实例 : http://download.csdn.net/detail/sunsize1130/4698305