| | |
| | | import java.io.*; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | | import java.nio.file.StandardCopyOption; |
| | | import java.util.*; |
| | | import java.util.concurrent.CountDownLatch; |
| | | |
| | |
| | | private ThreadPoolTaskExecutor poolTaskExecutor; |
| | | |
| | | @Override |
| | | public String copyFileWithCompanyId(String sourceFilePath, Long companyId) { |
| | | // 1. 解析原始文件路径 |
| | | File sourceFile = new File(sourceFilePath); |
| | | if (!sourceFile.exists() || !sourceFile.isFile()) { |
| | | throw new ApiException("源文件不存在或不是一个有效文件"); |
| | | } |
| | | |
| | | // 2. 获取原始文件名和扩展名 |
| | | String originalFileName = sourceFile.getName(); |
| | | String fileExtension = ""; |
| | | int dotIndex = originalFileName.lastIndexOf("."); |
| | | if (dotIndex > 0) { |
| | | fileExtension = originalFileName.substring(dotIndex); // 包含点号 |
| | | } |
| | | |
| | | // 3. 生成新文件名(使用公司ID + 扩展名) |
| | | String newFileName = UUID.randomUUID().toString().replace("-","")+"_"+companyId + fileExtension; |
| | | String dateStr= DateUtil.format(new Date(),"yyyyMMdd"); |
| | | // 4. 构造目标文件路径 |
| | | String targetDirPath = System.getProperty("user.dir") + File.separator + uploadPath+File.separator+dateStr; |
| | | File targetDir = new File(targetDirPath); |
| | | if (!targetDir.exists()) { |
| | | targetDir.mkdirs(); // 创建目录 |
| | | } |
| | | String targetFilePath = targetDirPath + File.separator + newFileName; |
| | | |
| | | // 5. 复制文件 |
| | | try { |
| | | FileUtils.copyFile(sourceFile, new File(targetFilePath)); |
| | | //Files.copy(sourceFile, new File(targetFilePath), StandardCopyOption.REPLACE_EXISTING); |
| | | } catch (IOException e) { |
| | | log.error("文件复制失败: {}", e.getMessage()); |
| | | throw new ApiException("文件复制失败,请联系管理员"); |
| | | } |
| | | // 6. 返回新文件路径 |
| | | return targetFilePath.replace("\\", "/"); |
| | | |
| | | } |
| | | |
| | | public static String copyFileWithCompanyIdA(String sourceFilePath, Long companyId) { |
| | | // 1. 解析原始文件路径 |
| | | File sourceFile = new File(sourceFilePath); |
| | | if (!sourceFile.exists() || !sourceFile.isFile()) { |
| | | throw new ApiException("源文件不存在或不是一个有效文件"); |
| | | } |
| | | |
| | | // 2. 获取原始文件名和扩展名 |
| | | String originalFileName = sourceFile.getName(); |
| | | String fileExtension = ""; |
| | | int dotIndex = originalFileName.lastIndexOf("."); |
| | | if (dotIndex > 0) { |
| | | fileExtension = originalFileName.substring(dotIndex); // 包含点号 |
| | | } |
| | | |
| | | // 3. 生成新文件名(使用公司ID + 扩展名) |
| | | String newFileName = UUID.randomUUID().toString().replace("-","")+"_"+companyId + fileExtension; |
| | | String dateStr= DateUtil.format(new Date(),"yyyyMMdd"); |
| | | // 4. 构造目标文件路径 |
| | | String targetDirPath = System.getProperty("user.dir") + File.separator + "images"+File.separator+dateStr; |
| | | File targetDir = new File(targetDirPath); |
| | | if (!targetDir.exists()) { |
| | | targetDir.mkdirs(); // 创建目录 |
| | | } |
| | | String targetFilePath = targetDirPath + File.separator + newFileName; |
| | | |
| | | // 5. 复制文件 |
| | | try { |
| | | FileUtils.copyFile(sourceFile, new File(targetFilePath)); |
| | | //Files.copy(sourceFile, new File(targetFilePath), StandardCopyOption.REPLACE_EXISTING); |
| | | } catch (IOException e) { |
| | | log.error("文件复制失败: {}", e.getMessage()); |
| | | throw new ApiException("文件复制失败,请联系管理员"); |
| | | } |
| | | // 6. 返回新文件路径 |
| | | return targetFilePath.replace("\\", "/"); |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public UploadObjectVO uploadFile(MultipartFile file) { |
| | | if(file==null){ |
| | | throw new ApiException("上传文件不能为空"); |
| | |
| | | dirFile.delete(); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public String uploadVideo2M3u8(MultipartFile file) throws Exception { |
| | |
| | | return uploadObjectVO; |
| | | } |
| | | |
| | | |
| | | } |
| | | |