-
Notifications
You must be signed in to change notification settings - Fork 6
/
MyJsp.jsp
252 lines (218 loc) · 9.31 KB
/
MyJsp.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/webuploader/jquery-1.10.2.min.js"></script>
<script src="${pageContext.request.contextPath}/static/webuploader/md5.js" type="text/javascript" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/static/webuploader/webuploader.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/webuploader/webuploader.css" />
<style type="text/css">
.itemDel, .itemStop, .itemUpload{
margin-left: 15px;
color: blue;
cursor: pointer;
}
#theList{
width: 80%;
min-height: 100px;
border: 1px solid red;
}
#theList .itemStop{
display: none;
}
</style>
</head>
<body>
<div id="uploader">
<ul id="theList"></ul>
<div id="picker">选择文件</div>
</div>
<script type="text/javascript">
var userid = "chenzhao";
var userInfo = {userId:userid, md5:""}; //用户会话信息
var chunkSize = 10*1024 * 1024; //分块大小
var uniqueFileName = null; //文件唯一标识符
var md5Mark = null;
WebUploader.Uploader.register({
"before-send-file": "beforeSendFile"
, "before-send": "beforeSend"
, "after-send-file": "afterSendFile"
}, {
beforeSendFile: function(file){
//秒传验证
//
var task = new $.Deferred();
var start = new Date().getTime();
(new WebUploader.Uploader()).md5File(file, 0, 10*1024*1024).progress(function(percentage){
console.log(percentage);
}).then(function(val){
console.log("总耗时: "+((new Date().getTime()) - start)/1000);
md5Mark = val;
userInfo.md5 = val;
$.ajax({
type: "POST"
, url: "${ctx}/webUp/check.do"
, data: {
status: "md5Check"
, md5: val
}
, cache: false
, timeout: 1000 //todo 超时的话,只能认为该文件不曾上传过
, dataType: "json"
}).then(function(data, textStatus, jqXHR){
//console.log(data);
if(data.ifExist){ //若存在,这返回失败给WebUploader,表明该文件不需要上传
task.reject();
uploader.skipFile(file);
file.path = data.path;
UploadComlate(file);
}else{
task.resolve();
//拿到上传文件的唯一名称,用于断点续传
uniqueFileName = md5(''+userInfo.userId+file.name+file.type+file.lastModifiedDate+file.size);
}
}, function(jqXHR, textStatus, errorThrown){ //任何形式的验证失败,都触发重新上传
task.resolve();
//拿到上传文件的唯一名称,用于断点续传
uniqueFileName = md5(''+userInfo.userId+file.name+file.type+file.lastModifiedDate+file.size);
});
});
return $.when(task);
}
, beforeSend: function(block){
//分片验证是否已传过,用于断点续传
var task = new $.Deferred();
$.ajax({
type: "POST"
, url:"${ctx}/webUp/check.do"
, data: {
status: "chunkCheck"
, name: uniqueFileName
, chunkIndex: block.chunk
, size: block.end - block.start
}
, cache: false
, timeout: 1000 //todo 超时的话,只能认为该分片未上传过
, dataType: "json"
}).then(function(data, textStatus, jqXHR){
if(data.ifExist){ //若存在,返回失败给WebUploader,表明该分块不需要上传
task.reject();
}else{
task.resolve();
}
}, function(jqXHR, textStatus, errorThrown){ //任何形式的验证失败,都触发重新上传
task.resolve();
});
return $.when(task);
}
, afterSendFile: function(file){
var chunksTotal = 0;
if((chunksTotal = Math.ceil(file.size/chunkSize)) > 1){
// 合并请求
var task = new $.Deferred();
$.ajax({
type: "POST"
, url: "${ctx}/webUp/check.do"
, data: {
status: "chunksMerge"
, name: uniqueFileName //临时文件名,唯一
, chunks: chunksTotal
, ext: file.name //真实文件名
, md5: md5Mark
}
, cache: false
, dataType: "json"
}).then(function(data, textStatus, jqXHR){
// todo 检查响应是否正常
task.resolve();
file.path = data.path;
UploadComlate(file);
}, function(jqXHR, textStatus, errorThrown){
task.reject();
});
return $.when(task);
}else{
UploadComlate(file);
}
}
});
var uploader = WebUploader.create({
swf: "${pageContext.request.contextPath}/static/webuploader-0.1.5/Uploader.swf",
server: "${ctx}/webUp/fileUpload.do",
pick: "#picker",
multiple:true,
//resize: false,
dnd: "#theList",
//paste: document.body,
//disableGlobalDnd: true,
thumb: {
width: 100
, height: 100
, quality: 70
, allowMagnify: true
, crop: true
//, type: "image/jpeg"
},
// , compress: {
// quality: 90
// , allowMagnify: false
// , crop: false
// , preserveHeaders: true
// , noCompressIfLarger: true
// ,compressSize: 100000
// }
compress: false,
prepareNextFile: true,
chunked: true,
chunkSize: chunkSize,
threads: true,
formData: function(){return $.extend(true, {}, userInfo);},
fileNumLimit: 1,
fileSingleSizeLimit: 10 * 1024 * 1024 * 1024,
duplicate: true
});
uploader.on("fileQueued", function(file){
$("#theList").append('<li id="'+file.id+'">' +
'<img /><span>'+file.name+'</span><span class="itemUpload">上传</span><span class="itemStop">暂停</span><span class="itemDel">删除</span>' +
'<div class="percentage"></div>' +
'</li>');
var $img = $("#" + file.id).find("img");
uploader.makeThumb(file, function(error, src){
if(error){
$img.replaceWith("<span>不能预览</span>");
}
$img.attr("src", src);
});
});
$("#theList").on("click", ".itemUpload", function(){
uploader.upload();
//"上传"-->"暂停"
$(this).hide();
$(".itemStop").show();
});
$("#theList").on("click", ".itemStop", function(){
uploader.stop(true);
//"暂停"-->"上传"
$(this).hide();
$(".itemUpload").show();
});
//todo 如果要删除的文件正在上传(包括暂停),则需要发送给后端一个请求用来清除服务器端的缓存文件
$("#theList").on("click", ".itemDel", function(){
uploader.removeFile($(this).parent().attr("id")); //从上传文件列表中删除
$(this).parent().remove(); //从上传列表dom中删除
});
uploader.on("uploadProgress", function(file, percentage){
$("#" + file.id + " .percentage").text(percentage * 100 + "%");
});
function UploadComlate(file){
console.log(file);
$("#" + file.id + " .percentage").text("上传完毕");
$(".itemStop").hide();
$(".itemUpload").hide();
$(".itemDel").hide();
}
</script>
</body>
</html>