⼤⽂件上传第⼆弹(分⽚、秒传、断点续传)
关键部分
前端⽤file.slice()分块
前端⽤FileReader获取每⼀分块的md5值
后端⽤MultipartFile接受分块⽂件
后端⽤FileOutputStream拼装分块⽂件
话不多说,直接上代码,我想这是你们最喜欢的
⼯程截图
<%@page language="java"import="java.util.*"pageEncoding="UTF-8"%><%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
String clientCookie = Header("Cookie");
%><!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=utf-8"/>
<title>up6.2-MySQL演⽰页⾯</title>
<link href="js/up6.css"type="text/css"rel="Stylesheet"/>
<script type="text/javascript"src="js/jquery-1.4.min.js"></script>
<script type="text/javascript"src="js/json2.min.js"charset="utf-8"></script>
<script type="text/javascript"src="fig.js"charset="utf-8"></script>
<script type="text/javascript"src="js/up6.app.js"charset="utf-8"></script>
<script type="text/javascript"src="js/up6.edge.js"charset="utf-8"></script>
<script type="text/javascript"src="js/up6.file.js"charset="utf-8"></script>
<script type="text/javascript"src="js/up6.folder.js"charset="utf-8"></script>
<script type="text/javascript"src="js/up6.js"charset="utf-8"></script>
<script language="javascript"type="text/javascript">
var cbMgr = new HttpUploaderMgr();
cbMgr.event.md5Complete = function (obj, md5) { /*alert(md5);*/ };
cbMgr.event.fileComplete = function (obj) { /*alert(obj.pathSvr);*/ };
cbMgr.Config["Cookie"] = 'JSESSIONID=<%=Session().getId()%>';
cbMgr.Config.Fields["test"] = "test";
$(function()
{
cbMgr.load_to("FilePanel");//加载控件
});
</script>
</head>
<body>
<p>up6.2多⽂件上传演⽰页⾯</p>
<div id="FilePanel"></div>
<div id="msg"></div>
</body>
</html>
⽂件MD5计算
/**
* 把⽂件转成md5字符串
* @param file
* @return
*/
publicstatic String fileToMD5(File file) {
if(file == null) {
returnnull;
}
ists() == false) {
returnnull;
}
if(file.isFile() == false) {
returnnull;
}
FileInputStream fis = null;
try {
//创建⼀个提供信息摘要算法的对象,初始化为md5算法对象
MessageDigest md = Instance("MD5");
fis = new FileInputStream(file);
byte[] buff = newbyte[1024];
int len = 0;
while(true) {
len = ad(buff, 0, buff.length);
if(len == -1){
break;
}
//每次循环读取⼀定的字节都更新
md.update(buff,0,len);
}
//关闭流
fis.close();
/
/返回md5字符串
return bytesToHex(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
returnnull;
}
⽂件夹⽂件夹名称⽣成逻辑
package up6.biz;
import java.io.IOException;
SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
del.FileInf;
publicclass PathBuilderUuid extends PathBuilder{
/* ⽣成⽂件夹存储路径,完全与客户端⽂件夹结构保持⼀致
* 格式:
* upload/2016/05/17/uuid/folder_name
* 更新记录:
* 2016-03-01 upload/uid/folders/uuid/folder_name
* 2016-05-17 将格式改为⽇期格式
*
*/
public String genFolder(int uid,String nameLoc) throws IOException {
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
Date date = new Date();
String strDD = fmtDD.format(date);
String strMM = fmtMM.format(date);
String strYY = fmtYY.format(date);
String uuid = UUID.randomUUID().toString();
uuid = place("-","");
String path = Root() + "/";
path = at(strYY);
path = at("/");
path = at(strMM);
path = at("/");
path = at(strDD);
path = at("/");
path = at(uuid);
path = at("/");
path = at(nameLoc);
return path;
}
/* 保留原始⽂件名称,不检查⽂件是否重复
* 格式:
* upload/uid/年/⽉/⽇/uuid/file_name
* @see Xproer.PathBuilder#genFile(int, Xproer.xdb_files)
*/
public String genFile(int uid,FileInf f) throws IOException{
String uuid = UUID.randomUUID().toString();
uuid = place("-", "");
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
Date date = new Date();
String strDD = fmtDD.format(date);
String strMM = fmtMM.format(date);
String strYY = fmtYY.format(date);
String path = Root() + "/";
jsessionidpath = at(strYY);
path = at("/");
path = at(strMM);
path = at("/");
path = at(strDD);
path = at("/");
path = at(uuid);
path = at("/");
path = at(f.nameLoc);
return path;
}
}
⽂件上传的效果
⽂件保存位置及逻辑
在up6中有两种保存模式,⼀种是md5⼀种是uuid。
md5由PathBuilderMd5⽣成存储路径。md5主要提供给⽂件使⽤,可在服务器端保存唯⼀的⽂件,有效避免重复⽂件。
uuid由PathBuilderUuid⽣成存储路径。uuid主要提供给⽂件夹使⽤,可以与本地⽂件夹结构完全保持⼀致。使⽤uuid模式上传⽂件夹时秒传功能会失效。⽂件默认保存位置在项⽬路径下:
demo会⾃动⽣成upload⽂件夹
规则:upload/年/⽉/⽇/md5
代码截图:
⽂件逻辑:
⽣成⽂件服务器存储路径
⽂件夹逻辑:
⽣成⽂件夹存储路径
⽣成⼦⽂件路径
⽣成⼦⽬录路径
后端代码逻辑⼤部分是相同的,⽬前能够⽀持MySQL,Oracle,SQL。在使⽤前需要配置⼀下数据库,可以参考我写的这篇⽂章:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论