java服务器如何对zip文件分包上传?
发布网友
发布时间:2022-04-22 10:33
我来回答
共1个回答
热心网友
时间:2022-07-13 10:24
这个你是用什么客户端上传呢?
java写的客户端和H5页面都可以做这个操作,思路都是一样的。
把文件切割再上传,后台接受结束后再把文件合并。
在DB里做个记录就是断点续传了嘛。
给点代码提示:
js:
每次上传2M,必须是支持H5的浏览器才行,兼容的问题需要注意!
function calculate(file,callBack){
var fileReader = new FileReader(),
blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice,
chunkSize = 2097152,
// read in chunks of 2MB
chunks = Math.ceil(file.size / chunkSize),
currentChunk = 0,
spark = new SparkMD5();
fileReader.onload = function(e) {
spark.appendBinary(e.target.result); // append binary string
currentChunk++;
if (currentChunk < chunks) {
loadNext();
}
else {
callBack(spark.end());
}
};
function loadNext() {
var start = currentChunk * chunkSize,
end = start + chunkSize >= file.size ? file.size : start + chunkSize;
fileReader.readAsBinaryString(blobSlice.call(file, start, end));
};
loadNext();
}
java代码没什么注释,也不是全部的代码看个大概意思,理解一下吧
根据文件的MD5码来判断每次上传的文件是不是上传过的。
如果是就找到上次的点告诉前台从哪开始上传。
Message message = new Message();
PrintWriter out = response.getWriter();
ServiceBreakpointUpload service = new ServiceBreakpointUpload();
BreakpointShard shard = new BreakpointShard();
String currentShardIndex = "";
String totalShard = "";
String fileMD5 = "";
String fileName = "";
String fileType = "other";
try {
fileMD5 = request.getParameter("fileMD5");
Part part = request.getPart("fileData");
currentShardIndex = request.getParameter("currentShardIndex");
totalShard = request.getParameter("totalShard");
fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");
fileType = request.getParameter("fileType");
String typeFolderName = service.getTypeFolder(fileType);
String folderPath = getServletContext().getRealPath("/upload/") + typeFolderName + File.separator;
String path = folderPath + fileName + "-" + fileMD5 + "-" + currentShardIndex;
System.out.println("fileName:"+fileName);
// 是否初次上传
if (!service.isUpload(fileMD5,fileType)) {
BreakpointFile breakpointFile = new BreakpointFile();
breakpointFile.setMd5(fileMD5);
breakpointFile.setFile_name(fileName);
breakpointFile.setTotal_shard(totalShard);
breakpointFile.setCurrent_shard_index(currentShardIndex);
breakpointFile.setFile_type(fileType);
breakpointFile.setPath(folderPath);
service.saveFile(breakpointFile);
} else {// 返回上次完成位置
service.updateFile(fileMD5, currentShardIndex,fileType);
System.out.println("upload shard "+currentShardIndex+" OK");
}
shard.setMd5(fileMD5);
shard.setShard_index(currentShardIndex);
shard.setPath(path);
service.saveShardFile(shard);
part.write(path);
if (currentShardIndex.equals(totalShard)) {// 上传完成
System.out.println("upload File finsh start merge shard");
service.mergeFiles(fileMD5,fileType);
System.out.println("merge shard OK");
message.setData("completed");
}
message.setData(currentShardIndex);
out.println(JSONObject.fromObject(message).toString());
} catch (Exception e) {
e.printStackTrace();
message.setHasError(true);
message.setErrorMessage("错误!");
out.println(JSONObject.fromObject(message).toString());
}
}