工程截图:
jar包:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
fileupload 我用的是1.0版本
Uploadify.java 源码
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
public class Uploadify extends HttpServlet {
/**
* 文件上传处理,页面上传操作请参照apache 的 fileupload,需导入apache的fileupload、IO两个jar包
*/
private static final long serialVersionUID = 1L;
/**
* 实现多文件的同时上传
*/
@SuppressWarnings("unchecked")
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得容器中上传文件夹所在的物理路径
String savePath = this.getServletConfig().getServletContext().getRealPath("/")+"uploads\\";
System.out.println("路径"+savePath);
DiskFileUpload upload = new DiskFileUpload();
//DiskFileItemFactory fac = new DiskFileItemFactory();
//ServletFileUpload upload = new ServletFileUpload(fac);
//对于向上传文件大小控制等fac.setSizeThreshold(4096)最多允许在内存中存放4096个字节 这类请查apache 的 fileupload例子
//获取多个上传文件
List fileList = null;
try {
//fileList = upload.parseRequest(request);
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
System.out.println("没有上传文件");
return;
}
//遍历上传文件写入磁盘
Iterator<FileItem> it = fileList.iterator();
while(it.hasNext()){
FileItem item = it.next();
if(!item.isFormField()){
String name = item.getName();
if(name == null || name.trim().equals("") || item.getSize()==0.0)
continue;
File saveFile = new File(savePath+name);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
index.jsp 源码
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>upload index</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/swfobject.js"></script>
<script type="text/javascript" src="scripts/jquery.uploadify.v2.0.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'scripts/uploadify.swf',
'script' : 'servlet/Upload',//servlet的路径或者.jsp 这是访问servlet 'scripts/uploadif' 如果是.jsp,我还加上了'scriptData'和'method'
'scriptData': {'x':$("#nodeid").attr("value")},
'method':'GET',
'cancelImg' : 'cancel.png',
'folder' : 'uploads',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 10, //一次同步上传的文件数目
'sizeLimit': 19871202, //设置单个文件大小限制,单位为byte
'queueSizeLimit' : 10,
'fileDesc': '支持格式:jpg/gif/jpeg/png/bmp.', //如果配置了以下的'fileExt'属性,那么这个属性是必须的
'fileExt': '*.jpg;*.gif;*.jpeg;*.png',//允许的格式
onComplete: function (event, queueID, fileObj, response, data) {
var value = response ;
alert("success back value"+value);
alert("文件:" + fileObj.name + "上传成功");
},
onError: function(event, queueID, fileObj) {
alert("文件:" + fileObj.name + "上传失败");
},
onCancel: function(event, queueID, fileObj){
alert("取消了" + fileObj.name);
}
});
});
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>
<a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a></p>
<input type="text" name="nodeid" id="nodeid" value="inputtxtvalue" />
</body>
</html>
web.xml 源码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Uploadify</servlet-name>
<servlet-class>com.ek.fileupload.Uploadify</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Uploadify</servlet-name>
<url-pattern>/scripts/uploadify</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>