注:一些实体类数据自行修改
1,api文件
import request from '@/router/axiosInfo';
export const uploadChunk = (data) => {
url: '/api/blade-sample/sample/covid19/uploadChunk',
export const uploadMerge = (data) => {
url: '/api/blade-sample/sample/covid19/uploadMerge',
2, axios文件
import axios from 'axios';
import store from '@/store/';
import router from '@/router/router';
import { serialize } from '@/util/util';
import { getToken } from '@/util/auth';
import { Message } from 'element-ui';
import { isURL } from "@/util/validate";
import website from '@/config/website';
import { Base64 } from 'js-base64';
import { baseUrl } from '@/config/env';
// import NProgress from 'nprogress';
// import 'nprogress/nprogress.css';
import crypto from '@/util/crypto';
axios.defaults.timeout = 100000;
axios.defaults.validateStatus = function (status) {
return status >= 200 && status <= 500;
axios.defaults.withCredentials = true;
axios.interceptors.request.use(config => {
if (!isURL(config.url) && !config.url.startsWith(baseUrl)) {
config.url = baseUrl + config.url
const authorization = config.authorization === false;
config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`;
const meta = (config.meta || {});
const isToken = meta.isToken === false;
const cryptoToken = config.cryptoToken === true;
const cryptoData = config.cryptoData === true;
const token = getToken();
config.headers[website.tokenHeader] = cryptoToken
? 'crypto ' + crypto.encryptAES(token, crypto.cryptoKey)
const data = crypto.encryptAES(JSON.stringify(config.params), crypto.aesKey);
config.params = { data };
config.data = crypto.encryptAES(JSON.stringify(config.data), crypto.aesKey);
if (config.text === true) {
config.headers["Content-Type"] = "text/plain";
//headers中配置serialize为true开启序列化
if (config.method === 'post' && meta.isSerialize === true) {
config.data = serialize(config.data);
return Promise.reject(error)
axios.interceptors.response.use(res => {
const status = res.data.code || res.status;
const statusWhiteList = website.statusWhiteList || [];
const message = res.data.msg || res.data.error_description || '未知错误';
const config = res.config;
const cryptoData = config.cryptoData === true;
if (statusWhiteList.includes(status)) return Promise.reject(res);
if (status === 401) store.dispatch('FedLogOut').then(() => router.push({ path: '/login' }));
return Promise.reject(new Error(message))
res.data = JSON.parse(crypto.decryptAES(res.data, crypto.aesKey));
return Promise.reject(new Error(error));

3,vue源码
<el-upload class="upload-demo" :on-change="handleChange" :show-file-list="false" :limit="1">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button style="margin-left: 10px;" size="small" type="success" @click="uploadMerge" v-loading.fullscreen.lock='loading'>上传到服务器</el-button>
<el-progress :percentage="percentage" :color="customColor"></el-progress>
import { uploadMerge, uploadChunk } from "@/api/sample/sampleCovid19Info";
currentChunkIndex: 0, // 当前切片索引
currentFile: null, // 当前处理文件
const formData = new FormData();
formData.append('fileName', this.currentFile.name);
formData.append('totalSize', this.totalSize);
let res = await uploadMerge({ fileName: this.currentFile.name, totalSize: this.totalSize })
if (res.data.code === 200) {
this.currentChunkIndex = 0
this.$message.error(error)
handleChange(file, fileList) {
this.totalSize = file.size
if (file.status === 'ready') {
this.currentFile = file.raw; // 获取原始文件对象
this.sliceFile(this.currentFile);
const chunkSize = 1024 * 1024 * 10; // 切片大小设置为1MB
const totalSize = file.size;
this.totalChunks = Math.ceil(totalSize / chunkSize);
for (let i = 0; i < this.totalChunks; i++) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, totalSize);
const chunk = file.slice(start, end);
const formData = new FormData();
formData.append('chunk', chunk);
formData.append('chunkIndex', i);
formData.append('totalChunks', this.totalChunks);
formData.append('fileName', this.currentFile.name);
formData.append('totalSize', this.totalSize);
let res = await uploadChunk(formData)
if (res.data.code === 200) {
this.currentChunkIndex = i + 1;
if (this.percentage != 99) {
if (this.currentChunkIndex === this.totalChunks) {

4,切片请求接口
/**
* 大上传文件
*/
@PostMapping("/uploadChunk")
@ApiOperationSupport(order = 15)
@ApiOperation(value = "大上传文件", notes = "大上传文件")
public R uploadChunk(@RequestParam("chunk") MultipartFile chunk,
@RequestParam("chunkIndex") Integer chunkIndex,
@RequestParam("totalChunks") Integer totalChunks,
@RequestParam("fileName") String fileName,
@RequestParam("totalSize") Long totalSize) throws IOException {
Boolean isOk = sampleCovid19Service.LargeFiles(chunk, chunkIndex, totalChunks, fileName, totalSize);
return R.status(isOk);
}
/**
* 大上传文件
* @param chunk 文件流
* @param chunkIndex 切片索引
* @param totalChunks 切片总数
* @param fileName 文件名称
* @param totalSize 文件大小
* @return
*/
Boolean LargeFiles(MultipartFile chunk, Integer chunkIndex, Integer totalChunks, String fileName, Long totalSize);
/**
* 大上传文件
* @param chunk 文件流
* @param chunkIndex 切片索引
* @param totalChunks 切片总数
* @param fileName 文件名称
* @param totalSize 文件大小
* @return
*/
@Override
public Boolean LargeFiles(MultipartFile chunk, Integer chunkIndex, Integer totalChunks, String fileName, Long totalSize) {
if (chunk == null) {
throw new ServiceException("添加失败,文件名称获取失败");
}
if (chunkIndex == null) {
throw new ServiceException("添加失败,分片序号不能为空");
}
if (fileName == null) {
throw new ServiceException("添加失败,文件名称获取失败");
}
if (totalSize == null || totalSize == 0) {
throw new ServiceException("添加失败,文件名大小不能为空或0");
}
//获取文件名称
fileName = fileName.substring(0, fileName.lastIndexOf("."));
File chunkFolder = new File(chunkPath + totalSize + "/" + fileName);
//判断文件路径是否创建
if (!chunkFolder.exists()) {
chunkFolder.mkdirs();
}
//直接将文件写入
File file = new File(chunkPath + totalSize + "/" + fileName + "/" + chunkIndex);
try {
chunk.transferTo(file);
} catch (IOException e) {
log.info("时间:{}" + new Date() + "文件名称:{}" + fileName + "上传失败");
throw new ServiceException("时间:{}" + new Date() + "文件名称:{}" + fileName + "上传失败");
}
return true;
}
5,合并接口
package org.springblade.sample.vo;
import lombok.Data;
@Data
public class UploadMergeVO {
//文件上传名称
private String fileName;
//文件大小
private Long totalSize;
}
/**
* 大文件合并
*/
@PostMapping("/uploadMerge")
@ApiOperationSupport(order = 16)
@ApiOperation(value = "文件合并", notes = "文件合并")
public R uploadMerge(@RequestBody UploadMergeVO vo) throws IOException {
Boolean isOk = sampleCovid19Service.LargeMerge(vo);
return R.status(isOk);
}
/**
* 大文件合并
* @param vo
* @return
* @throws IOException
*/
Boolean LargeMerge(UploadMergeVO vo) throws IOException;
/**
* 大文件合并
* @param vo
* @return
* @throws IOException
*/
@Override
public Boolean LargeMerge(UploadMergeVO vo) throws IOException {
if (StringUtil.isBlank(vo.getFileName())) {
throw new ServiceException("上传失败,文件名称获取失败");
}
if (vo.getTotalSize() == 0) {
throw new ServiceException("上传失败,文件大小不能为空");
}
//块文件目录
String fileNameInfo = vo.getFileName().substring(0, vo.getFileName().lastIndexOf("."));
//合并文件夹
File chunkFolder = new File(chunkPath + vo.getTotalSize() + "/" + fileNameInfo + "/");
File depositFile = new File(sourceFile + vo.getTotalSize() + "/" + fileNameInfo + "/");
//创建文件夹
if (!depositFile.exists()) {
depositFile.mkdirs();
}
//创建新的合并文件
File largeFile = new File(depositFile + "/" + vo.getFileName());
largeFile.createNewFile();
//用于写文件
RandomAccessFile raf_write = new RandomAccessFile(largeFile, "rw");
//指针指向文件顶端
raf_write.seek(0);
//缓冲区
byte[] b = new byte[1024];
//分块列表
File[] fileArray = chunkFolder.listFiles();
// 转成集合,便于排序
List fileList = Arrays.asList(fileArray);
// 从小到大排序
Collections.sort(fileList, new Comparator() {
@Override
public int compare(File o1, File o2) {
return Integer.parseInt(o1.getName()) - Integer.parseInt(o2.getName());
}
});
//合并文件
for (File chunkFile : fileList) {
RandomAccessFile raf_read = new RandomAccessFile(chunkFile, "rw");
int len = -1;
while ((len = raf_read.read(b)) != -1) {
raf_write.write(b, 0, len);
}
raf_read.close();
}
raf_write.close();
//取出合并文件大小和hash
long fileSize = getFileSize(largeFile);
int hashCode = largeFile.hashCode();
//判断是否合并成功
if (fileSize == vo.getTotalSize()) {
//将内容写入数据库
FileBack fileBack = new FileBack();
fileBack.setFileName(vo.getFileName());
fileBack.setFileSize(Integer.parseInt(String.valueOf(vo.getTotalSize())));
fileBack.setUpdateTime(new Date());
fileBack.setFileUrl(chunkFolder.toString());
fileBack.setFileHash(String.valueOf(hashCode));
fileBack.setUploadData(new Date());
fileBackService.save(fileBack);
//删除临时文件目录
File chunkFolderInfo = new File(chunkPath + vo.getTotalSize() + "/");
FileUtils.deleteDirectory(chunkFolderInfo);
log.info("时间:{}" + new Date() + "文件名称:{}" + vo.getFileName() + "上传成功");
return true;
} else {
//删除临时文件目录
File chunkFolderInfo = new File(chunkPath + vo.getTotalSize() + "/");
FileUtils.deleteDirectory(chunkFolderInfo);
log.info("时间:{}" + new Date() + "文件名称:{}" + vo.getFileName() + "上传失败");
return false;
}
}