springmvc + VUE大文件分片上传前后端MD5加密
时间: 2023-08-02 10:08:51 浏览: 117
springmvc 多文件上传
Spring MVC + Vue.js 的大文件分片上传前后端MD5加密流程如下:
1. 前端通过 Vue.js 实现文件的分片上传,同时计算每个分片的MD5值。
```javascript
<template>
<div>
<input type="file" @change="uploadFile"/>
</div>
</template>
<script>
import SparkMD5 from 'spark-md5'
export default {
name: 'FileUpload',
methods: {
async uploadFile(event) {
const file = event.target.files[0]
const chunkSize = 2097152 // 每个分片的大小为2MB
const chunks = Math.ceil(file.size / chunkSize) // 文件分成的总块数
const fileReader = new FileReader()
const spark = new SparkMD5.ArrayBuffer()
const md5List = []
for (let i = 0; i < chunks; i++) {
const start = i * chunkSize
const end = start + chunkSize > file.size ? file.size : start + chunkSize
const chunk = file.slice(start, end)
const chunkReader = new FileReader()
chunkReader.onload = e => {
spark.append(e.target.result)
md5List.push(spark.end())
}
chunkReader.readAsArrayBuffer(chunk)
}
fileReader.onload = e => {
const formData = new FormData()
formData.append('file', file)
formData.append('md5List', JSON.stringify(md5List))
// 发送文件分片和MD5值到后端
this.$axios.post('/uploadFile', formData)
}
fileReader.readAsArrayBuffer(file)
}
}
}
</script>
```
2. 后端 Spring MVC 接收到文件分片和 MD5 值,对每个分片进行 MD5 计算,最后将所有分片的 MD5 值进行合并,得到整个文件的 MD5 值,并与前端传过来的 MD5 值进行比较,如果相同,则说明文件传输完整无误。
```java
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
@RestController
public class FileUploadController {
@PostMapping("/uploadFile")
public void uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("md5List") String md5List,
HttpServletRequest request) throws IOException, NoSuchAlgorithmException {
// 将 MD5 值的 JSON 字符串转换成 List
List<String> md5Array = JSON.parseArray(md5List, String.class);
// 计算文件的 MD5 值
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024 * 1024];
int len;
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file.getOriginalFilename(), "rw")) {
for (int i = 0; i < md5Array.size(); i++) {
randomAccessFile.seek(i * buffer.length);
len = randomAccessFile.read(buffer, 0, buffer.length);
md5Digest.update(buffer, 0, len);
if (!md5Array.get(i).equals(getMD5(buffer, len))) {
throw new IOException("文件传输错误");
}
}
randomAccessFile.seek(0);
while ((len = file.getInputStream().read(buffer)) != -1) {
randomAccessFile.write(buffer, 0, len);
}
}
// 保存文件
File newFile = new File("upload/" + file.getOriginalFilename());
file.transferTo(newFile);
}
private String getMD5(byte[] buffer, int len) throws NoSuchAlgorithmException {
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
md5Digest.update(buffer, 0, len);
byte[] md5Bytes = md5Digest.digest();
StringBuilder stringBuilder = new StringBuilder();
for (byte md5Byte : md5Bytes) {
int value = md5Byte & 0xFF;
if (value < 16) {
stringBuilder.append("0");
}
stringBuilder.append(Integer.toHexString(value));
}
return stringBuilder.toString();
}
}
```
以上代码仅供参考,实际应用中需要根据具体需求进行修改。
阅读全文