准备工作
a.下载uploadily插件的js,下载地址http://www.uploadify.com/documentation/
参考地址:
http://my.oschina.net/jasonultimate/blog/164999?fromerr=QJ5CCdGd
http://www.uploadify.com/documentation/
我的例子:
1.jsp页面
1.1 index
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'index.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">
<title>Insert title here</title>
<!--装载文件-->
<link rel="stylesheet" href="/js/uploadify/uploadify.css" type="text/css"></link>
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="/js/uploadify/jquery.uploadify.js"></script>
</head>
<body>
This is my JSP page.
<br>
<a href="upload.jsp">upload</a>
<a href="<%=path%>/test/hell.action">_hello_</a>
</body>
</html>
1.2 upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
<!--装载文件-->
<link rel="stylesheet" href="js/uploadify/uploadify.css" type="text/css"></link>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="js/uploadify/jquery.uploadify.js"></script>
<%-- --%><script type="text/javascript" src="/js/uploadify/swfobject.js"></script>
<script type="text/javascript">
$(function(){
initUploadify();
});
function initUploadify(){
$('#uploadify').uploadify({
'swf' : 'js/uploadify/uploadify.swf',//上传按钮的图片,默认是这个flash文件
"uploader":"test/test_upload.action",
'cancelImg' : 'js/uploadify/uploadify-cancel.png',//取消图片
'fileObjName':'uploadFiles',
'method':'post',
'queueID' : 'uploadifyQueue',//上传显示进度条的那个div
'buttonText' : '请选择文件',
'auto' : false,
'multi' : true,
'fileSizeLimit':'20MB'
});
}
</script>
</head>
<body>
<input id="uploadify" name="uploadFiles" value="上传"/>
<div id="uploadifyQueue"></div>
<p>
<!-- 加上“*”表示当第一个文件上传成功后,立即上传以后队列中的文件,否则需要自己手动 -->
<a href="javascript:$('#uploadify').uploadify('upload','*')">上传</a>
</p>
</body>
</html>
2. Action代码
package action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ProduceAction extends ActionSupport {
static Logger log = Logger.getLogger(HttpClient.class.getName());
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("登录");
System.out.println(super.execute());
return super.execute();
}
public String login() {
System.out.println("登录:login");
return "success";
}
/*
* 下面的uploadFiles有两点需要注意 1、必须是List数组
* 2、命名必须跟jsp页面中的uploadify的一个参数"fileObjName"一致,否则无法获取到上传的文件
*/
public List<File> uploadFiles;
/*
* 下面的两个属性为使用Action上传文件所必须的,在前台jsp页面无需做任何操作
*/
public List<String> uploadFilesFileName;
public List<String> uploadFilesContentType;
public String upload() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse respon = ServletActionContext.getResponse();
for (int i = 0; i < uploadFilesFileName.size(); i++) {
System.out.println(uploadFilesFileName.get(i));
}
for (int i = 0; i < uploadFiles.size(); i++) {
System.out.println(uploadFiles.get(i).getName());
}
try {
respon.getOutputStream().print("成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return NONE;
}
public List<File> getUploadFiles() {
return uploadFiles;
}
public void setUploadFiles(List<File> uploadFiles) {
this.uploadFiles = uploadFiles;
}
public List<String> getUploadFilesFileName() {
return uploadFilesFileName;
}
public void setUploadFilesFileName(List<String> uploadFilesFileName) {
this.uploadFilesFileName = uploadFilesFileName;
}
public List<String> getUploadFilesContentType() {
return uploadFilesContentType;
}
public void setUploadFilesContentType(List<String> uploadFilesContentType) {
this.uploadFilesContentType = uploadFilesContentType;
}
}
3. struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="53687091200" /><!-- 设置伤上传最大为5G 5368709120-->
<package name="default" namespace="/test" extends="struts-default">
<action name="come">
<result>
page/Come.jsp
</result>
</action>
<action name="hell">
<result>
page/Hello.jsp
</result>
</action>
<action name="test" class="action.ProduceAction">
<result>page/login.jsp</result>
</action>
<action name="test_*" class="action.ProduceAction" method="{1}">
<result name="success">page/login.jsp</result>
</action>
</package>
</struts>