@AutoLog(value = "上传文件夹") @ApiOperation(value = "上传文件夹", notes = "上传文件夹") @PostMapping(value = "/upFolder") public Result<?> uploadFolder(MultipartFile file, String id, Integer level, String path, String title) { synchronized ("") { #同步锁 String originalFilename = file.getOriginalFilename(); Result<ArrayList<Document>> result = new Result<>(); ArrayList<Document> documents = new ArrayList<>(); Document pDocument = new Document(); String[] strings = originalFilename.split("/"); String p = ""; String pid = id; int level_ = 0; for (int i = 0; i < strings.length - 1; i++) { p = p + "/" + strings[i]; File file1 = new File(ROOT_PATH + path + "/" + title + p); String title1 = p.substring(p.lastIndexOf("/") + 1); String path1 = null; if (id.equals("root")) { path1 = path + p.substring(0, p.lastIndexOf("/")); } else { path1 = path + "/" + title + p.substring(0, p.lastIndexOf("/")); } if (!file1.exists()) { file1.mkdir(); Document newFolder = new Document() .setKey(UUID.randomUUID().toString().replaceAll("-", "").substring(18)) .setTitle(title1) .setCreateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now())) .setLevel(level + i + 1) .setParentId(pid) .setIsFile(0) .setPath(path1) .setDocumentNum(0) .setFolderNum(0); documentService.save(newFolder); documentService.increaseFolderNum(pid); pid = newFolder.getId(); } else { try { pid = documentService.queryByTitleAndPath(title1, path1).getId(); } catch (NullPointerException e) { result.error500(originalFilename + " 上传失败"); } } level_ = level + i + 2; if (id.equals("root") && i == 0) { pDocument.setId(pid); } } Document document = new Document(); String title_ = strings[strings.length - 1]; String fileType = title_.substring(title_.lastIndexOf(".") + 1); String documentType = FileTypeConstant.FILE_TYPE_MAP.get(fileType); String mode = ""; if (fileType.equals("pdf")) { mode = "view"; } float tempSize = (float) file.getSize() / 1024 / 1024; String size = (tempSize + "").substring(0, 5) + " Mb"; String localTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()); String path_ = null; if (id.equals("root")) { path_ = path + p; } else { path_ = path + "/" + title + p; } /** * 保存数据库 */ document.setKey(UUID.randomUUID().toString().replaceAll("-", "").substring(18)) .setTitle(title_) .setFileType(fileType) .setDocumentType(documentType) .setMode(mode) .setSize(size) .setCreateTime(localTime) .setUpdateTime(localTime) .setLevel(level_) .setParentId(pid) .setIsFile(1) .setUrl(HTTP_PATH + path_ + "/" + title_) .setPath(path_); documentService.save(document); documentService.updateDocumentNum(1, pid); documents.add(document); documents.add(pDocument); File file1 = new File(ROOT_PATH + path_ + "/", title_); try { if (!file1.exists()) { file1.createNewFile(); } file.transferTo(file1); } catch (IOException e) { e.printStackTrace(); } /** * 调用es方法 */ EsFileResource net = new EsFileResource(); net.setUuid(id); net.setFileName(title_); net.setLocalFileUrl(ROOT_PATH + path_ + "/" + title_); net.setNetFileUrl(HTTP_PATH + path_ + "/" + title_); net.setFileCreationDate(localTime); esIndexService.process(net); result.setResult(documents); return result; } }
java 实现文件夹上传
最新推荐文章于 2024-04-30 17:28:01 发布