文章目录
大文件数据分片上传简单示例
1. HTML
2. JS
3. Java
3.1 文件属性实体类
3.2 统一返回结果定义
3.3 Controller
3.4 Service实现类
大文件数据分片上传简单示例
1. HTML
1
2
2. JS
3. Java
3.1 文件属性实体类
/**
* @program: service-center
* @description: 文件Model,前端将文件切片后将数据分片传给后台
* @author: Mr.superbeyone
* @create: 2018-11-14 09:20
**/
public class FileModel {
private int fileSize; //文件大小
private int sliceSize; //切片数量
private String md5; //文件md5值
private int index; //当前为第几片
private String fileName; //文件名
private MultipartFile file; //文件
//GET SET
}
3.2 统一返回结果定义
/**
* @program:
* @description:
* @author: Mr.superbeyone
* @create: 2018-10-18 16:37
**/
public class JsonResult
private Integer code;
private String msg;
private T data;
//Get
//Set
}
3.3 Controller
@RestController
public class FileUploadController {
@Autowired
FileUploadService fileUploadService;
/**
* 数据分片上传
*
* @param fileModel
* @return
*/
@RequestMapping("/uploadSlice")
public JsonResult
Object uploadSchedule = RequestHolder.getCurrentRequest().getSession().getAttribute("uploadSchedule");
if (uploadSchedule == null) {
RequestHolder.getCurrentRequest().getSession().setAttribute("uploadSchedule", new FileOperatorSchedule());
}
JsonResult
/**
* 获取上传任务进度
*
* @return
*/
@RequestMapping("/uploadSchedule")
public JsonResult
JsonResult
Object uploadSchedule = request.getSession().getAttribute("uploadSchedule");
FileOperatorSchedule fileOperatorSchedule = null;
if (uploadSchedule != null) {
fileOperatorSchedule = (FileOperatorSchedule) uploadSchedule;
if (fileOperatorSchedule.getFileSize() <= fileOperatorSchedule.getCompletedSize()) {
result.setCode(200);
result.setMsg("上传任务完成");
RequestHolder.getCurrentRequest().getSession().removeAttribute("uploadSchedule");
} else {
result.setCode(600);
result.setMsg("上传任务进行中");
}
} else {
result.setCode(900);
result.setMsg("数据准备中");
}
result.setData(fileOperatorSchedule);
return result;
}
}
3.4 Service实现类
/**
* @program: service-center
* @description:
* @author: Mr.superbeyone
* @create: 2018-10-19 12:54
**/
@Service
public class FileUploadServiceImpl implements FileUploadService {
/**
* 切片上传文件
*
* @param fileModel
* @return
*/
@Override
public JsonResult
//文件分片位置
File file = null;
if (fileModel.getSliceSize() == 1) {
file = new File(parentFolder, fileModel.getFileName());
} else {
file = new File(parentFolder, fileModel.getFileName() + "_" + fileModel.getIndex());
}
if (file.exists()) {
file.delete();
}
FileOperatorSchedule fileOperatorSchedule = (FileOperatorSchedule) RequestHolder.getCurrentRequest().getSession().getAttribute("uploadSchedule");
if (fileOperatorSchedule == null) {
fileOperatorSchedule = new FileOperatorSchedule();
}
fileOperatorSchedule.setFileSize(fileModel.getFileSize());
BufferedOutputStream bufferedOutputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
//上传文件 1
// fileModel.getFile().transferTo(file);
//上传文件 2
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
bufferedInputStream = new BufferedInputStream(fileModel.getFile().getInputStream());
int len = 0;
byte[] bytes = new byte[2048];
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
//判断所有分片是否上传完成,完成则合并
File[] files = parentFolder.listFiles();
if (files.length == fileModel.getSliceSize()) { //上传完成
if (fileModel.getSliceSize() > 1) {
File newFile = new File(parentFolder, fileModel.getFileName());
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(newFile, true));
for (int i = 1; i <= files.length; i++) {
bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(parentFolder, fileModel.getFileName() + "_" + i)));
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
for (int i = 1; i <= parentFolder.listFiles().length; i++) {
File splitFile = new File(parentFolder, fileModel.getFileName() + "_" + i);
if (splitFile.exists()) {
if (splitFile.delete()) {
logger.debug(splitFile.getName() + "删除成功");
} else {
logger.error( splitFile.getName() + "删除失败");
}
}
}
result.setCode(200);
result.setMsg("上传成功");
result.setData(fileModel.getFileName());
} else {
result.setCode(800);
result.setMsg("上传失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}