我們在工作中可能會遇到大文件的上傳,如果一次傳輸,很有可能會遇到 網絡、超時各種各樣的問題。這裏將分片上傳和斷點續傳兩種方式介紹一下方案。
一、分片上傳
分片上傳的原理:前端首先請求接口獲取這次上傳的uuid,上傳的時間等信息返回給前端頁面。前端將所要上傳文件進行分片,在上傳的時候包含 服務器返回給前端的 該次上傳的uuid,該篇文件的md5,上傳的第幾片的的標識,和文件名稱等標識。等到上傳的片數等於總片數的時候對所有分片進行合併,刪除臨時分片。
這裏由springboot+freemarker 來演示這個原理
(服務端用到工具類和前端用到的 js由於斷點續傳 同樣也用到,在介紹完 斷點續傳 之後統一貼出來)
1、首先添加配置文件 application.properties(由於斷點續傳 需要依賴mysql故也將mysql引入)
server.port=8080
spring.freemarker.expose-spring-macro-helpers=true
# 是否優先從文件系統加載template,以支持熱加載,默認爲true
spring.freemarker.prefer-file-system-access=true
# 設定模板的後綴.
spring.freemarker.suffix=.ftl
# 設定模板的加載路徑,多個以逗號分隔,默認:
spring.freemarker.template-loader-path=classpath:/templates/
spring.mvc.static-path-pattern=/static/**
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pingyougou
spring.datasource.username=root
spring.datasource.password=root
#配置.xml文件路徑
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:conf/mapper/*Mapper.xml
#配置模型路徑
mybatis.type-aliases-package=com.yin.freemakeradd.pojo
2、引入controller類
localhost:8080/upload2/index 第斷點上傳入口
@Controller
@RequestMapping(value = "/test")
public class IndexController {
@GetMapping(value = "/upload/index")
public String index(Model model){
return "breakpointBurst";
}
@GetMapping(value = "/upload2/index")
public String index2(Model model){
return "burst";
}
}
分片上傳的具體上傳邏輯
@RestController
@RequestMapping(value = "/burst/test")
public class BurstController {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
private static final String temp_dir = "C:\\Users\\Administrator\\Desktop\\upload";
private static final String final_dir = "C:\\Users\\Administrator\\Desktop\\test";
/**
* 上傳文件
*
* @param request
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping(value = "/upload")
public Map<String, Object> upload(
HttpServletRequest request, @RequestParam(value = "data",required = false) MultipartFile multipartFile) throws IllegalStateException, IOException, Exception {
String uuid = request.getParameter("uuid");
String fileName = request.getParameter("name");
//總大小
String size = request.getParameter("size");
//總片數
int total = Integer.valueOf(request.getParameter("total"));
//當前是第幾片
int index = Integer.valueOf(request.getParameter("index"));
//整個文件的md5
String fileMd5 = request.getParameter("filemd5");
//文件第一個分片上傳的日期(如:20200118)
String date = request.getParameter("date");
//分片的md5
String md5 = request.getParameter("md5");
//生成上傳文件的路徑信息,按天生成
String savePath = "\\upload" + File.separator + date;
String saveDirectory = temp_dir + savePath +File.separator+uuid;
//驗證路徑是否存在,不存在則創建目錄
File path = new File(saveDirectory);
if (!path.exists()) {
path.mkdirs();
}
//文件分片位置
//File file = new File(saveDirectory, uuid + "_" + index);
multipartFile.transferTo(new File(saveDirectory, uuid + "_" + index));
if (path.isDirectory()) {
File[] fileArray = path.listFiles();
if (fileArray != null) {
if (fileArray.length == total) {
//分塊全部上傳完畢,合併
String suffix = NameUtil.getExtensionName(fileName);
String dir = final_dir + savePath;
File newFile = new File(dir, uuid + "." + suffix);
File copyDir = new File(dir);
if(!copyDir.mkdir()){
copyDir.mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(newFile, true);//文件追加寫入
byte[] byt = new byte[10 * 1024 * 1024];
int len;
// FileInputStream temp = null;//分片文件
for (int i = 0; i < total; i++) {
int j = i + 1;
FileInputStream temp = new FileInputStream(new File(saveDirectory, uuid + "_" + j));
while ((len = temp.read(byt)) != -1) {
System.out.println("-----" + len);
outputStream.write(byt, 0, len);
}
//關閉流
temp.close();
}
outputStream.close();
//刪除臨時文件
FileUtil.deleteFolder( temp_dir+ savePath +File.separator+uuid);
}
}
}
HashMap map = new HashMap<>();
map.put("fileId", uuid);
return map;
}
/**
* 上傳文件前獲取id和日期(如果是分片上傳這一步可以交給前端處理)
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = "/isUpload")
public Map<String, Object> getUpload(HttpServletRequest request) throws Exception {
HashMap<String,Object> map = new HashMap<>();
String uuid = UUID.randomUUID().toString();
map.put("fileId", uuid);
map.put("date", formatter.format(LocalDateTime.now()));
return map;
}
}
分片前端展示:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>HTML5大文件分片上傳示例</title>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="/static/md5.js"></script>
<script>
var i = -1;
var succeed = 0;
var databgein; //開始時間
var dataend; //結束時間
var action = false; //false檢驗分片是否上傳過(默認); true上傳文件
var page = {
init: function () {
$("#upload").click(function () {
$("#upload").attr('disabled', true)
databgein = new Date();
var file = $("#file")[0].files[0]; //文件對象
isUpload(file);
});
}
};
$(function () {
page.init();
});
function isUpload(file) {
//構造一個表單,FormData是HTML5新增的
var form = new FormData();
var r = new FileReader();
r.readAsBinaryString(file);
$(r).load(function (e) {
var bolb = e.target.result;
var md5 = hex_md5(bolb);
form.append("md5", md5);
//Ajax提交
$.ajax({
url: "http://localhost:8080//burst/test/isUpload",
type: "POST",
data: form,
//異步
async: true,
//很重要,告訴jquery不要對form進行處理
processData: false,
//很重要,指定爲false才能形成正確的Content-Type
contentType: false,
success: function (dataObj) {
// var dataObj = eval("("+data+")");
var uuid = dataObj.fileId;
var date = dataObj.date;
//沒有上傳過文件
upload(file, uuid, md5, date);
}, error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("服務器出錯!");
}
});
})
}
/*
* file 文件對象
* uuid 後端生成的uuid
* filemd5 整個文件的md5
* date 文件第一個分片上傳的日期(如:20170122)
*/
function upload(file, uuid, filemd5, date) {
name = file.name; //文件名
size = file.size; //總大小
var shardSize = 512 * 1024, //以0.5m爲一個分片
shardCount = Math.ceil(size / shardSize); //總片數
if (i > shardCount) {
i = -1;
i = shardCount;
} else {
i += 1; //只有在檢測分片時,i纔去加1; 上傳文件時無需加1
}
//計算每一片的起始與結束位置
var start = i * shardSize,
end = Math.min(size, start + shardSize);
//構造一個表單,FormData是HTML5新增的
var form = new FormData();
form.append("action", "upload"); //直接上傳分片
form.append("data", file.slice(start, end)); //slice方法用於切出文件的一部分
$("#param").append("action==upload ");
form.append("uuid", uuid);
form.append("filemd5", filemd5);
form.append("date", date);
form.append("name", name);
form.append("size", size);
form.append("total", shardCount); //總片數
form.append("index", i + 1); //當前是第幾片
var ssindex = i + 1;
$("#param").append("index==" + ssindex + "<br/>");
//按大小切割文件段
var data = file.slice(start, end);
var r = new FileReader();
r.readAsBinaryString(data);
$(r).load(function (e) {
var bolb = e.target.result;
var md5 = hex_md5(bolb);
form.append("md5", md5);
//Ajax提交
$.ajax({
url: "http://localhost:8080//burst/test/upload",
type: "POST",
data: form,
async: true, //異步
processData: false, //很重要,告訴jquery不要對form進行處理
contentType: false, //很重要,指定爲false才能形成正確的Content-Type
success: function (dataObj) {
//var dataObj = eval("("+data+")");
var fileuuid = dataObj.fileId;
//服務器返回分片是否上傳成功
//改變界面
++succeed;
$("#output").text(succeed + " / " + shardCount);
if (i + 1 == shardCount) {
dataend = new Date();
$("#uuid").append(fileuuid);
$("#usetime").append(dataend.getTime() - databgein.getTime());
$("#upload").attr('disabled', false);
} else if (i + 1 < shardCount) {
//遞歸調用
upload(file, uuid, filemd5, date);
} else {
i = -1;
succeed = 0;
$("#upload").attr('disabled', true)
databgein = new Date();
file = $("#file")[0].files[0]; //文件對象
isUpload(file);
}
}, error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#upload").attr('disabled', false);
alert("服務器出錯!");
}
});
})
}
</script>
</head>
<body>
<input type="file" id="file"/>
<button id="upload">上傳</button>
<span id="output" style="font-size:12px">等待</span>
<span id="usetime" style="font-size:12px;margin-left:20px;">用時</span>
<span id="uuid" style="font-size:12px;margin-left:20px;">uuid==</span>
<br/>
<br/>
<br/>
<br/>
<span id="param" style="font-size:12px;margin-left:20px;">param==</span>
</body>
</html>
分片上傳每當一次上傳失敗,都需要重新生成一個上傳唯一uuid,全部從頭開始上傳。但是相對後面斷點續傳 同樣有上傳與服務器的交互少了一半,不用依賴數據庫的優點。
二、斷點續傳
斷點續傳的原理:前端首先請求接口通過上傳文件的MD5來獲取此次是否上傳,如果上傳通過md5將上一次的uuid返回給前端。前端將所要上傳文件進行分片,在上傳的時候包含 服務器返回給前端的 該次上傳的uuid,該篇文件的md5,上傳的第幾片的的標識,首先去檢查是否此片是否上傳,如果上傳,直接跳到下一片的檢查。如果沒有上傳,再上傳文件。上傳第一片的時候將信息記錄到mysql。等上傳完畢,將文件信息改爲上傳成功。刪除臨時文件夾。
1、由於首先添加配置文件 application.properties和 入口controller已經添加,這裏不再繼續貼出。
2、上傳具體controller
@RestController
@RequestMapping(value = "/breakpointBusrt/test")
public class BreakpointBurstController {
@Autowired
private FileUploadService fileUploadService;
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
private static final String temp_dir = "C:\\Users\\Administrator\\Desktop\\upload";
private static final String final_dir = "C:\\Users\\Administrator\\Desktop\\test";
/**
* 上傳文件
*
* @param request
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping(value = "/upload")
public Map<String, Object> upload(
HttpServletRequest request, @RequestParam(value = "data",required = false) MultipartFile multipartFile) throws IllegalStateException, IOException, Exception {
String action = request.getParameter("action");
String uuid = request.getParameter("uuid");
String fileName = request.getParameter("name");
String size = request.getParameter("size");//總大小
int total = Integer.valueOf(request.getParameter("total"));//總片數
int index = Integer.valueOf(request.getParameter("index"));//當前是第幾片
String fileMd5 = request.getParameter("filemd5"); //整個文件的md5
String date = request.getParameter("date"); //文件第一個分片上傳的日期(如:20170122)
String md5 = request.getParameter("md5"); //分片的md5
//生成上傳文件的路徑信息,按天生成
String savePath = "\\upload" + File.separator + date;
String saveDirectory = temp_dir + savePath +File.separator+uuid;
//驗證路徑是否存在,不存在則創建目錄
File path = new File(saveDirectory);
if (!path.exists()) {
path.mkdirs();
}
//文件分片位置
File file = new File(saveDirectory, uuid + "_" + index);
//根據action不同執行不同操作. check:校驗分片是否上傳過; upload:直接上傳分片
Map<String, Object> map = null;
if("check".equals(action)){
String md5Str = FileMd5Util.getFileMD5(file);
if (md5Str != null && md5Str.length() == 31) {
System.out.println("check length =" + md5.length() + " md5Str length" + md5Str.length() + " " + md5 + " " + md5Str);
md5Str = "0" + md5Str;
}
if (md5Str != null && md5Str.equals(md5)) {
//分片已上傳過
map = new HashMap<>();
map.put("flag", "2");
map.put("fileId", uuid);
map.put("status", true);
return map;
} else {
//分片未上傳
map = new HashMap<>();
map.put("flag", "1");
map.put("fileId", uuid);
map.put("status", true);
return map;
}
}else if("upload".equals(action)){
//分片上傳過程中出錯,有殘餘時需刪除分塊後,重新上傳
if (file.exists()) {
file.delete();
}
multipartFile.transferTo(new File(saveDirectory, uuid + "_" + index));
}
if (path.isDirectory()) {
File[] fileArray = path.listFiles();
if (fileArray != null) {
if (fileArray.length == total) {
//分塊全部上傳完畢,合併
String suffix = NameUtil.getExtensionName(fileName);
String dir = final_dir + savePath;
File newFile = new File(dir, uuid + "." + suffix);
File copyDir = new File(dir);
if(!copyDir.mkdir()){
copyDir.mkdirs();
}
//文件追加寫入
FileOutputStream outputStream = new FileOutputStream(newFile, true);
byte[] byt = new byte[10 * 1024 * 1024];
int len;
//FileInputStream temp = null;//分片文件
for (int i = 0; i < total; i++) {
int j = i + 1;
FileInputStream temp = new FileInputStream(new File(saveDirectory, uuid + "_" + j));
while ((len = temp.read(byt)) != -1) {
System.out.println("-----" + len);
outputStream.write(byt, 0, len);
}
//關閉流
temp.close();
}
outputStream.close();
//修改FileRes記錄爲上傳成功
fileUploadService.updateUploadRecord(fileMd5, FileConstant.FileStatusEnum.UPLOAD_FINSH.getStatus());
}else if(index == 1){
//文件第一個分片上傳時記錄到數據庫
FileUploadRecord fileUploadRecord = new FileUploadRecord();
String name = NameUtil.getFileNameNoEx(fileName);
if (name.length() > 50) {
name = name.substring(0, 50);
}
String suffix = NameUtil.getExtensionName(fileName);
fileUploadRecord.setName(name);
fileUploadRecord.setTimeContent(date);
fileUploadRecord.setUuid(uuid);
fileUploadRecord.setPath(savePath + File.separator + uuid + "." + suffix);
fileUploadRecord.setSize(size);
fileUploadRecord.setMd5(fileMd5);
fileUploadRecord.setStatus(FileConstant.FileStatusEnum.UPLOAD_BEGIN.getStatus());
fileUploadService.insertUploadRecord(fileUploadRecord);
}
}
}
map = new HashMap<>();
map.put("flag", "3");
map.put("fileId", uuid);
map.put("status", true);
return map;
}
/**
* 上傳文件前校驗
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = "/isUpload")
public Map<String, Object> isUpload(HttpServletRequest request) throws Exception {
String fileMd5 = request.getParameter("md5");
FileUploadRecord fileUploadRecord=fileUploadService.findByFileMd5(fileMd5);
HashMap<String,Object> map = new HashMap<>();
if(Objects.isNull(fileUploadRecord)){
//沒有上傳過文件
String uuid = UUID.randomUUID().toString();
map.put("flag", "1");
map.put("fileId", uuid);
map.put("date", formatter.format(LocalDateTime.now()));
map.put("status", true);
return map;
}
if(FileConstant.FileStatusEnum.UPLOAD_FINSH.getStatus().equals(fileUploadRecord.getStatus())){
//文件上傳成功
map.put("flag", "3");
map.put("fileId", fileUploadRecord.getUuid());
map.put("date",fileUploadRecord.getTimeContent());
map.put("status", true);
return map;
}
if(FileConstant.FileStatusEnum.UPLOAD_FINSH.getStatus().equals(fileUploadRecord.getStatus())){
map.put("flag", "1");
map.put("fileId", fileUploadRecord.getUuid());
map.put("date", fileUploadRecord.getTimeContent());
map.put("status", true);
return map;
}
//狀態不對
String uuid = UUID.randomUUID().toString();
map.put("flag", "1");
map.put("fileId", uuid);
map.put("date", formatter.format(LocalDateTime.now()));
map.put("status", true);
return map;
}
}
3、上傳的前端頁面
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>HTML5大文件分片上傳示例</title>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="/static/md5.js"></script>
<script>
var i = -1;
var succeed = 0;
var databgein; //開始時間
var dataend; //結束時間
var action=false; //false檢驗分片是否上傳過(默認); true上傳文件
var page = {
init: function(){
$("#upload").click(function(){
$("#upload").attr('disabled',true)
databgein=new Date();
var file = $("#file")[0].files[0]; //文件對象
isUpload(file);
});
}
};
$(function(){
page.init();
});
function isUpload (file) {
//構造一個表單,FormData是HTML5新增的
var form = new FormData();
var r = new FileReader();
r.readAsBinaryString(file);
$(r).load(function(e){
var bolb = e.target.result;
var md5 = hex_md5(bolb);
form.append("md5", md5);
//Ajax提交
$.ajax({
url: "http://localhost:8080//breakpointBusrt/test/isUpload",
type: "POST",
data: form,
async: true, //異步
processData: false, //很重要,告訴jquery不要對form進行處理
contentType: false, //很重要,指定爲false才能形成正確的Content-Type
success: function(dataObj){
// var dataObj = eval("("+data+")");
var uuid = dataObj.fileId;
var date = dataObj.date;
if (dataObj.flag == "1") {
//沒有上傳過文件
upload(file,uuid,md5,date);
} else if(dataObj.flag == "2") {
//已經上傳部分
upload(file,uuid,md5,date);
} else if(dataObj.flag == "3") {
//文件已經上傳過
alert("文件已經上傳過,秒傳了!!");
$("#uuid").append(uuid);
}
},error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("服務器出錯!");
}
});
})
}
/*
* file 文件對象
* uuid 後端生成的uuid
* filemd5 整個文件的md5
* date 文件第一個分片上傳的日期
*/
function upload (file,uuid,filemd5,date) {
name = file.name; //文件名
size = file.size; //總大小
var shardSize = 512 * 1024 , //以0.5m爲一個分片
shardCount = Math.ceil(size / shardSize); //總片數
if (i > shardCount) {
i = -1;
i = shardCount;
} else {
if(!action){
i += 1; //只有在檢測分片時,i纔去加1; 上傳文件時無需加1
}
}
//計算每一片的起始與結束位置
var start = i * shardSize,
end = Math.min(size, start + shardSize);
//構造一個表單,FormData是HTML5新增的
var form = new FormData();
if(!action){
form.append("action", "check"); //檢測分片是否上傳
$("#param").append("action==check ");
}else{
form.append("action", "upload"); //直接上傳分片
form.append("data", file.slice(start,end)); //slice方法用於切出文件的一部分
$("#param").append("action==upload ");
}
form.append("uuid", uuid);
form.append("filemd5", filemd5);
form.append("date", date);
form.append("name", name);
form.append("size", size);
form.append("total", shardCount); //總片數
form.append("index", i+1); //當前是第幾片
var ssindex = i+1;
$("#param").append("index=="+ssindex+"<br/>");
//按大小切割文件段
var data = file.slice(start, end);
var r = new FileReader();
r.readAsBinaryString(data);
$(r).load(function(e){
var bolb = e.target.result;
var md5 = hex_md5(bolb);
form.append("md5", md5);
//Ajax提交
$.ajax({
url: "http://localhost:8080//breakpointBusrt/test/upload",
type: "POST",
data: form,
async: true, //異步
processData: false, //很重要,告訴jquery不要對form進行處理
contentType: false, //很重要,指定爲false才能形成正確的Content-Type
success: function(dataObj){
var fileuuid = dataObj.fileId;
var flag = dataObj.flag;
//服務器返回該分片是否上傳過
if(!action){
if (flag == "1") {
//未上傳
action = true;
} else if(dataObj.flag == "2") {
//已上傳
action = false;
++succeed;
}
//遞歸調用
upload(file,uuid,filemd5,date);
}else{
//服務器返回分片是否上傳成功
//改變界面
++succeed;
$("#output").text(succeed + " / " + shardCount);
if (i+1 == shardCount) {
dataend=new Date();
$("#uuid").append(fileuuid);
$("#usetime").append(dataend.getTime()-databgein.getTime());
$("#upload").attr('disabled',false);
} else {
//已上傳成功,然後檢測下一個分片
action = false;
//遞歸調用
upload(file,uuid,filemd5,date);
}
}
},error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#upload").attr('disabled',false);
alert("服務器出錯!");
}
});
})
}
</script>
</head>
<body>
<input type="file" id="file" />
<button id="upload">上傳</button>
<span id="output" style="font-size:12px">等待</span>
<span id="usetime" style="font-size:12px;margin-left:20px;">用時</span>
<span id="uuid" style="font-size:12px;margin-left:20px;">uuid==</span>
<br/>
<br/>
<br/>
<br/>
<span id="param" style="font-size:12px;margin-left:20px;">param==</span>
</body>
</html>
這裏用到mybatis,mybatis大家都有用到。這裏就僅將使用到建表語句和實體類貼出
DROP TABLE IF EXISTS `upload_file_record`;
CREATE TABLE `upload_file_record` (
`uuid` varchar(50) not null,
`name` varchar(50) not null COMMENT '文件名',
`timeContent` varchar(10) not NULL COMMENT 'yyyyMMdd',
`c_path` varchar(225) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL COMMENT '文件地址',
`c_size` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL COMMENT '大小',
`file_md5` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL COMMENT 'fileMd5',
`status` TINYINT(2) not NULL COMMENT '文件上傳狀態',
`createTime` TIMESTAMP COMMENT '創建時間',
PRIMARY KEY (`uuid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
實體類:
package com.yin.freemakeradd.pojo;
import com.yin.freemakeradd.utils.NameUtil;
import java.io.File;
/**
* @author yin
* @Date 2020/1/17 22:31
* @Method
*/
@Data
public class FileUploadRecord {
private String uuid;
private String name;
private String timeContent;
private String path;
private String size;
private String md5;
private Integer status;
private String createTime;
附(這裏將使用到工具類和md5.js貼出):
1、服務端使用md5生成工具
public class FileMd5Util {
public static final String KEY_MD5 = "MD5";
public static final String CHARSET_ISO88591 = "ISO-8859-1";
/**
* Get MD5 of one file:hex string,test OK!
*
* @param file
* @return
*/
public static String getFileMD5(File file) {
if (!file.exists() || !file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
/***
* Get MD5 of one file!test ok!
*
* @param filepath
* @return
*/
public static String getFileMD5(String filepath) {
File file = new File(filepath);
return getFileMD5(file);
}
/**
* MD5 encrypt,test ok
*
* @param data
* @return byte[]
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
public static byte[] encryptMD5(String data) throws Exception {
return encryptMD5(data.getBytes(CHARSET_ISO88591));
}
/***
* compare two file by Md5
*
* @param file1
* @param file2
* @return
*/
public static boolean isSameMd5(File file1,File file2){
String md5_1= FileMd5Util.getFileMD5(file1);
String md5_2= FileMd5Util.getFileMD5(file2);
return md5_1.equals(md5_2);
}
/***
* compare two file by Md5
*
* @param filepath1
* @param filepath2
* @return
*/
public static boolean isSameMd5(String filepath1,String filepath2){
File file1=new File(filepath1);
File file2=new File(filepath2);
return isSameMd5(file1, file2);
}
}
2、文件名生成工具。
public class NameUtil {
/**
* Java文件操作 獲取文件擴展名
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename.toLowerCase();
}
/**
* Java文件操作 獲取不帶擴展名的文件名
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename.toLowerCase();
}
public static void main(String[] args) {
String str = "AAbbbCC.jpg";
System.out.println(getExtensionName(str).toLowerCase());
System.out.println(getFileNameNoEx(str).toUpperCase());
}
}
刪除文件工具類
package com.yin.freemakeradd.utils;
import java.io.File;
public class FileUtil {
/**
* 刪除單個文件
* @param filePath 被刪除文件的文件名
* @return 文件刪除成功返回true,否則返回false
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}
/**
* 刪除文件夾以及目錄下的文件
* @param filePath 被刪除目錄的文件路徑
* @return 目錄刪除成功返回true,否則返回false
*/
public static boolean deleteDirectory(String filePath) {
boolean flag = false;
//如果filePath不以文件分隔符結尾,自動添加文件分隔符
if (!filePath.endsWith(File.separator)) {
filePath = filePath + File.separator;
}
File dirFile = new File(filePath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
flag = true;
File[] files = dirFile.listFiles();
//遍歷刪除文件夾下的所有文件(包括子目錄)
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
//刪除子文件
flag = deleteFile(files[i].getAbsolutePath());
if (!flag){
break;
}
} else {
//刪除子目錄
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag){
break;
}
}
}
if (!flag){
return false;
}
//刪除當前空目錄
return dirFile.delete();
}
/**
* 根據路徑刪除指定的目錄或文件,無論存在與否
* @param filePath 要刪除的目錄或文件
* @return 刪除成功返回 true,否則返回 false。
*/
public static boolean deleteFolder(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return false;
} else {
if (file.isFile()) {
// 爲文件時調用刪除文件方法
return deleteFile(filePath);
} else {
// 爲目錄時調用刪除目錄方法
return deleteDirectory(filePath);
}
}
}
public static void main(String[] args) {
deleteFolder("C:\\Users\\Administrator\\Desktop\\upload"+"\\upload" + File.separator + "20200118"+File.separator+"eed4e9e3-52f0-4755-a391-aaba54d606cc");
}
}
前端引入md5.js文件
/* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }
/*
* Perform a simple self-test to see if the VM is working
*/
function md5_vm_test()
{
return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}
/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
for(var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);
}
/*
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
/*
* Calculate the HMAC-MD5, of a key and some data
*/
function core_hmac_md5(key, data)
{
var bkey = str2binl(key);
if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++)
{
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
return core_md5(opad.concat(hash), 512 + 128);
}
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/*
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}
/*
* Convert an array of little-endian words to a string
*/
function binl2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
return str;
}
/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
}
return str;
}
/*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray)
{
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for(var i = 0; i < binarray.length * 4; i += 3)
{
var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
| (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
| ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
for(var j = 0; j < 4; j++)
{
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
return str;
}