给下面代码添加注释:public Document breakpointUpload(@RequestPart("file") MultipartFile file, @RequestParam("chunkNumber") Integer chunkNumber, @RequestParam("totalChunks") Integer totalChunks, @RequestParam("identifier") String identifier, @RequestParam(name = "documentId",required = false) String documentId, @RequestParam(name = "folderId") String folderId, @RequestParam(name = "versionId",required = false) String versionId) { if (StringUtils.isBlank(folderId)) { throw new BimdcException(HttpStatus.BAD_REQUEST.value(), "folderId不能为空"); } else { String fileName = file.getOriginalFilename(); fileName = fileName.substring(0, fileName.lastIndexOf(".")); if (fileName.length() > this.maxFileNameLength) { throw new BimdcException(400, "【" + fileName + "】文件长度为:" + fileName.length() + "字符,已超过" + this.maxFileNameLength + "字符长度!"); } else { try { log.info("DocumentServiceImpl breakpointUpload, upfile=[{}], chunkNumber=[{}], totalChunks=[{}], identifier=[{}], documentId=[{}], folderId=[{}], versionId=[{}]", new Object[]{file.getOriginalFilename(), chunkNumber, totalChunks, identifier, documentId, folderId, versionId}); double checkFileSize = (double)file.getSize() / 1048576.0; if (checkFileSize > this.maxbreakpointUploadFileSize) { throw new BimdcException(file.getOriginalFilename() + "文件大小不能超过" + this.maxbreakpointUploadFileSize + "M!"); } else { Map<String, Object> params = new HashMap(9); params.put("chunkNumber", chunkNumber); params.put("totalChunks", totalChunks); params.put("identifier", identifier); if (null != documentId) { params.put("documentId", documentId); } if (null != folderId) { params.put("folderId", folderId); } if (null != versionId) { params.put("versionId", versionId); } String url = this.serviceUrl + "document/v1/document/document/uploadResume"; String result = this.httpMultipartFormdata.postMultipartFile(url, params, file, "upfile"); log.info("upload result = [{}]", result); Document document = new Document(); if (result.contains("code")) { JSONObject jsonObject = JSONObject.fromObject(result); if ("0".equals(jsonObject.get("code"))) { jsonObject = JSONObject.fromObject(JSONObject.fromObject(result).get("data")); document.setDocumentId(jsonObject.getString("pathMd5")); document.setFileId(jsonObject.getString("id")); document.setVersionNo(Integer.parseInt(jsonObject.getString("updateNo"))); } } return document;
时间: 2024-04-28 17:22:50 浏览: 161
/**
* 从前端上传的文件中断点续传
* @param file 上传的文件
* @param chunkNumber 当前分片编号
* @param totalChunks 总分片数
* @param identifier 文件唯一标识符
* @param documentId 文档ID(可选)
* @param folderId 文件夹ID
* @param versionId 版本ID(可选)
* @return 返回上传成功的文档对象
*/
public Document breakpointUpload(@RequestPart("file") MultipartFile file,
@RequestParam("chunkNumber") Integer chunkNumber,
@RequestParam("totalChunks") Integer totalChunks,
@RequestParam("identifier") String identifier,
@RequestParam(name = "documentId",required = false) String documentId,
@RequestParam(name = "folderId") String folderId,
@RequestParam(name = "versionId",required = false) String versionId) {
// 检查文件夹ID是否为空
if (StringUtils.isBlank(folderId)) {
throw new BimdcException(HttpStatus.BAD_REQUEST.value(), "folderId不能为空");
} else {
String fileName = file.getOriginalFilename();
// 获取文件名(不含后缀名)
fileName = fileName.substring(0, fileName.lastIndexOf("."));
// 检查文件名长度是否超过最大长度限制
if (fileName.length() > this.maxFileNameLength) {
throw new BimdcException(400, "【" + fileName + "】文件长度为:" + fileName.length() + "字符,已超过" + this.maxFileNameLength + "字符长度!");
} else {
try {
// 记录上传日志
log.info("DocumentServiceImpl breakpointUpload, upfile=[{}], chunkNumber=[{}], totalChunks=[{}], identifier=[{}], documentId=[{}], folderId=[{}], versionId=[{}]", new Object[]{file.getOriginalFilename(), chunkNumber, totalChunks, identifier, documentId, folderId, versionId});
// 检查文件大小是否超过最大限制
double checkFileSize = (double)file.getSize() / 1048576.0;
if (checkFileSize > this.maxbreakpointUploadFileSize) {
throw new BimdcException(file.getOriginalFilename() + "文件大小不能超过" + this.maxbreakpointUploadFileSize + "M!");
} else {
// 构造上传参数
Map<String, Object> params = new HashMap(9);
params.put("chunkNumber", chunkNumber);
params.put("totalChunks", totalChunks);
params.put("identifier", identifier);
if (null != documentId) {
params.put("documentId", documentId);
}
if (null != folderId) {
params.put("folderId", folderId);
}
if (null != versionId) {
params.put("versionId", versionId);
}
// 构造上传URL
String url = this.serviceUrl + "document/v1/document/document/uploadResume";
// 调用HTTP上传工具类,上传文件
String result = this.httpMultipartFormdata.postMultipartFile(url, params, file, "upfile");
// 记录上传结果
log.info("upload result = [{}]", result);
// 解析上传结果,构造Document对象
Document document = new Document();
if (result.contains("code")) {
JSONObject jsonObject = JSONObject.fromObject(result);
if ("0".equals(jsonObject.get("code"))) {
jsonObject = JSONObject.fromObject(JSONObject.fromObject(result).get("data"));
document.setDocumentId(jsonObject.getString("pathMd5"));
document.setFileId(jsonObject.getString("id"));
document.setVersionNo(Integer.parseInt(jsonObject.getString("updateNo")));
}
}
return document;
}
}
catch (Exception e) {
// 记录错误日志
log.error("breakpointUpload error : {}", e.getMessage());
throw new BimdcException(400, "文件上传失败:" + e.getMessage());
}
}
}
}
阅读全文