后端springmvc,前端html5的FormData实现文件断点上传
来源:互联网 发布:筒灯选择 知乎 编辑:程序博客网 时间:2024/05/18 12:01
后端代码:
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
package
com.test.controller;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.util.UUID;
import
org.apache.commons.io.FileUtils;
import
org.apache.commons.io.FilenameUtils;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.bind.annotation.ResponseBody;
import
org.springframework.web.multipart.MultipartFile;
@Controller
public
class
UploadController {
@ResponseBody
// 返回rest json
@RequestMapping
(value = {
"/rest/saveUploadFileAsChunk"
}, method = RequestMethod.POST, produces =
"application/json"
)
public
File saveUploadFileAsChunk(
@RequestParam
(required =
false
) MultipartFile file,
@RequestParam
(required =
true
) Integer chunk,
// 分割块数
@RequestParam
(required =
true
) Integer chunks,
// 总分割数
@RequestParam
(required =
true
) String tempFileName
//临时文件名 必须带后缀名
)
throws
Exception{
if
(
null
!= file){
// 保存临时文件
String chunkName = tempFileName;
if
(chunk !=
null
) {
chunkName = chunk +
"_"
+ tempFileName;
}
File savedFile =
new
File(
"普通存放文件的路径"
, chunkName);
if
(!savedFile.getParentFile().exists())
savedFile.getParentFile().mkdirs();
file.transferTo(savedFile);
//将MultipartFile转存到file对象
}
//如果到最后一个分割块,则做合并处理
if
(chunk !=
null
&& chunk +
1
== chunks) {
String newFileName = UUID.randomUUID().toString().replace(
"-"
,
""
).concat(
"."
).concat(FilenameUtils.getExtension(tempFileName));
BufferedOutputStream outputStream =
new
BufferedOutputStream(
new
FileOutputStream(
new
File(
"普通存放文件的路径"
, newFileName)));
// 遍历文件合并 chunks,循环将文件写入新的文件中,并且删除之前的临时文件
for
(
int
i =
0
; i < chunks; i++) {
File tempFile =
new
File(
"普通存放文件的路径"
, i +
"_"
+ tempFileName);
byte
[] bytes = FileUtils.readFileToByteArray(tempFile);
//将file对象解析城byte数组
outputStream.write(bytes);
outputStream.flush();
tempFile.delete();
}
outputStream.flush();
outputStream.close();
File reallyFile =
new
File(
"普通存放文件的路径"
, newFileName);
//reallyFile即最后合并的文件
return
reallyFile;
}
return
null
;
}
}
前端代码:
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
var
setsize = 10 * 1024;
//1024字节 = 1KB * 10 即每次10k
/**
*参数filepath就是本地文件路径,主要就是用于放在localStorage中确定唯一性
file : js的Blob对象
*/
function
uplaod(filepath,file){
var
filesize = file.size;
var
filecount = filesize/setsize;
//计算出可以分成几块
var
i = localStorage.getItem(filepath);
i = (i!=
null
&& i!=
""
)?parseInt(i):0;
if
(i < filecount){
//新建一个FormData对象
var
formData =
new
FormData();
//++++++++++
var
blobfile;
if
((filesize - i * setsize) > setsize){
blobfile = file.slice(i*setsize,(i+1)*setsize);
}
else
{
//代表是最后一此了
blobfile = file.slice(i*setsize,filesize);
}
formData.append(
'chunk'
, i);
//++++++++++当前文件块
formData.append(
'chunks'
, Math.ceil(filecount));
//++++++++++ 总块数
formData.append(
"tempFileName"
,fileName);
//临时文件名 带uuid的
formData.append(
'file'
, blobfile);
if
(i < filecount){
$.ajax({
url: PATROL_CONSTANTS.SRV_URL+
"/rest/saveUploadFileAsChunk"
,
type:
'POST'
,
data: formData,
processData:
false
,
contentType:
false
,
success:
function
(responseStr) {
localStorage.setItem(filepath,i+1);
var
rs = filecount <= 0 ?
"0%"
: (Math.round((i+1) / Math.ceil(filecount) * 10000) / 100.00 +
"%"
);
console.log(
"进度百分比:"
+rs);
uploadFile(filepath,file);
//递归调用
},
error:
function
(responseStr) {
console.log(
"上传失败,重复尝试!"
);
console.log(responseStr);
uploadFile(filepath,file);
}
});
}
else
{
//上传完成后,则将对应的localStorage移除掉
localStorage.removeItem(filepath);
}
}
阅读全文
0 0
- 后端springmvc,前端html5的FormData实现文件断点上传
- html5+FormData 实现ajax文件上传
- 利用html5-formdata实现文件异步上传
- 前端使用FormData实现上传文件
- HTML5 FormData多文件上传
- HTML5 FormData 方法介绍以及实现文件上传
- HTML5 FormData 方法介绍以及实现文件上传
- FormData实现文件的异步上传
- 利用html5的FormData对象和ajax实现异步文件上传
- 利用html5的FormData对象和ajax实现异步文件上传
- HTML5利用FormData对象实现显示进度条的文件上传【译】
- HTML5利用FormData对象实现显示进度条的文件上传【转】
- HTML5 FormData对象 的上传文件的使用
- 基于Ajax的formData图片和数据上传(前端发送及后端验证)
- Android前端RxJava2+Retrofit2;后端SpringMvc实现图片上传
- FormData 实现文件上传实例
- ajax--html5上传文件file api +FormData
- HTML5 FormData 进行文件jquery ajax 上传
- Odd Even Linked List
- 不用加减乘除做加法
- expdp及impdp中的exclude及include参数
- PAT (Basic Level) Practise (中文) 1070. 结绳(25)
- php 实现进制相互转换
- 后端springmvc,前端html5的FormData实现文件断点上传
- SpringQuartz 定时器 java
- Python 用pyinstaller生成可执行文件No module named 'pefile'
- WKWebView之OC代码注入,删除百度HTML页面的按钮
- Android 最常用的设计模式九 安卓源码分析—— 适配器模式(Adapter)
- Java虚拟机(JVM)中的内存设置详解
- Linux vi 撤销与取消撤销
- mybatis执行批量更新batch update 的方法
- 剑指offer第六题