/**
* 骆武辉
*/
var videoUrl;
$(function() {
var fileSeed = new Array();// 保存分片表单
var loser= new Array();// 保存上传失败文件
var Interval;// 重复事件监听参数
var uploadState=true;// 分片表单使用状态
var fileName;
var sizeNum;
$("#upinput").change(function() {
$(".progress").show();
console.info(this);
// 获取文件流
var file1 = this.files[0];
if(file1.type=="video/mp4"){
console.info(file1);
// 获取文件名
fileName = file1.name;
// 获取文件大小
sizeNum = file1.size;
// console.info(sizeNum);
// console.info(postfix);
// 后缀判断
var shardSize = 4*1024 * 1024; // 以1MB为一个分片
var shardCount = Math.ceil(sizeNum / shardSize); // 总片数
// console.info(shardCount);
// 文件切片
var index = 0;
Interval = setInterval(function() {
// console.info("正在分片..." + index);
// 计算每一片的起始与结束位置
var start = index * shardSize,
end = Math.min(sizeNum, start + shardSize);
// 构造一个表单,FormData是HTML5新增的
var form = new FormData();
form.append("data", file1.slice(start, end)); // slice方法用于切出文件的一部分
form.append("name", index+"_"+fileName );
// 保存到数组
fileSeed[index] = form;
progress(shardCount, index);
// console.log("表单" + fileSeed[index]);
index++;
console.log(index == shardCount);
if(index == shardCount) {
// 停止重复事件
clearInterval(Interval);
progress(shardCount, shardCount);
}
}, 5);
}else $(this).attr("value",null);
});
function progress(shardCount, i) {
var num = i / shardCount * 100;
// 修改长度
$(".progress-seed").css("width", num + "%");
}
function progress(shardCount, i) {
var num = i / shardCount * 100;
console.info(num);
// 修改长度
$(".progress-seed").css("width", num + "%");
}
function combineVoid(index) {// 合并请求
// Ajax提交
$.ajax({
url: "/video/combineVoid",
type: "POST",
scriptCharset: 'utf-8',
data:{
"fileName":fileName,
"fileSzie":sizeNum,
"index":index
},
success: function(data) {
// alert(data);
// 成功后的事件
if(data!="false")
videoUrl=data;
else
alert("验证失败");
}
});
}
function uploadAjax(filePiece){
uploadState=false;
// Ajax提交
$.ajax({
url: "/video/upload",
type: "POST",
resetFomr: true,
data:filePiece ,
async: false, // 同步减请浏览器负担
processData: false, // jquery不要对form进行处理
contentType: false, // 指定为false才能形成正确的Content-Type
success: function(data) {
// 成功后的事件
if (data=="false") {// 上传失败标记
console.info("上传期间出现错误");
loser[loser.length]=filePiece;
}
uploadState=true;
}
});
}
// 监听ctlBtn
$("#ctlBtn").click(function() {
if(fileName!=null && fileName!=""){
var index=0;
$(".right840").empty();
$(".right840").load("/html/user_upload2.html");
// 定时执行
Interval= setInterval(function() {
if(uploadState){
// 加入上传列队
uploadAjax(fileSeed[index]);
index++;
}
if(index==fileSeed.length){
clearInterval(Interval);
// 校验提醒
combineVoid(index);
alert("上传完成");
}
},50);
}
});
});
代码:java
public static boolean uploadFile(MultipartFile data, String name) {
// TODO Auto-generated method stub
boolean falg = false;
// 设置保存位置
String realPath = "F:\cache";
// 创建文件
File targetFile = new File(realPath, name);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
// 保存文件流
data.transferTo(targetFile);
falg = true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return falg;
}
/**
* 文件合并工具
* @param fileName
* @param index
* @return
*/
public static String fileCombinePlant(String fileName, Integer index, Integer fileSzie) {
String result;
// TODO Auto-generated method stub
int byteSzie = 100 * 1024;
int nextInt = new Random().nextInt(10000);
OutputStream out = null;
BufferedOutputStream outputStream = null;
List<File> files = new ArrayList<>();
try {
result = nextInt + fileName;
// 创建流对象
out = new FileOutputStream("F:\vide\vod1\" + result);
outputStream = new BufferedOutputStream(out);
// 匹配文件
for (int i = 0; i < index; i++) {
// 创建文件对象
File file = new File("F:\cache\" + i + "_" + fileName);
files.add(file);
InputStream intpu = new FileInputStream(file);
// 读取文件到流
byte[] b = new byte[byteSzie];
int read = 0;
try {
// 读取数据
while ((read = intpu.read(b, 0, b.length)) > 0) {
// 输出数据
outputStream.write(b, 0, read);
}
// 关闭读入流
intpu.close();
} catch (IOException e) {
result = "false";
System.out.println("FileUtil-->fileCombinePlant读取流错误");
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
result = "false";
// TODO Auto-generated catch block
System.out.println("FileUtil-->fileCombinePlant输出流错误");
e.printStackTrace();
} finally {
try {
outputStream.flush();
outputStream.close();
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 删除缓存文件
removeFile(files);
return result;
}
/** 删除文件
* @param files
*/
private static void removeFile(List<File> files) {
// 遍历集合
for (File file : files) {
// 判断文件是否存在
if (file.exists()) {
// 删除文件
file.delete();
}
}
}
前端效果:
上传文件存储服务器目录:
D:wamp64wwwup6dbupload2019 419920144c756af424ca59136be71cf9209
文件上传记录可在数据库中查看:
大文件上传完成,文件的传输很完美,没有丢包。
DEMO下载地址:https://dwz.cn/fgXtRtnu