1、主方法
public ResponseEntity<String> importComplaintTapeFile( @Parameter(required = true, description = "投诉记录ID", allowEmptyValue = false, allowReserved = true) @RequestParam(value = "complaintId") String complaintId, @Parameter(required = true, description = "导入的音频文件", allowEmptyValue = false, allowReserved = true) @RequestPart(value = "file", required = true) MultipartFile uploadFile) throws ControllerException { log.debug("获得上传的音频文件名字是:{}", uploadFile.getOriginalFilename()); if (uploadFile.isEmpty()) { throw new ControllerException("音频文件不可为空!"); } try { // 1.处理上传的文件 String filePath = FileHandleHelper.saveFile(uploadFile, complaintFileSavedFolder, complaintId); // 2.保存文件的路径 ComplaintTapeEntity returnComplaintTapeEntity = complaintTapeService .importComplaintTapeFile(complaintId, filePath); // 3.调用讯飞文字解析服务,实现语音转文字 String userPath = System.getProperty("user.dir"); lfasrService.parseAudio(returnComplaintTapeEntity.getId(), new File(userPath, filePath)); return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON) .body(HttpResponseHelper.successInfoInbox(returnComplaintTapeEntity)); } catch (Exception e) { String errorMessage = "导入" + uploadFile.getOriginalFilename() + "音频文件失败: " + e.getMessage(); log.error(errorMessage, e); throw new ControllerException(errorMessage); } }
2、保存音频文件
public static String saveFile(MultipartFile file, String savedFolder, String complaintId) throws ServiceException { //1.把文件保存到硬盘 // 使用DateTimeFormatter来生成符合要求的时间格式字符串 String timestamp = LocalDateTime.now() .format(formatter); // 构建子目录的路径 String directoryPath = savedFolder + File.separator + complaintId + File.separator + timestamp; // 创建子目录 File directory = new File(directoryPath); if (!directory.exists()) { directory.mkdirs(); } // 构建目标文件的完整路径 String filePath = directoryPath + File.separator + file.getOriginalFilename(); try { // 保存文件 Path path = Paths.get(filePath); Files.copy(file.getInputStream(), path); log.info("音频文件保存成功: " + filePath); } catch (IOException e) { throw new ServiceException(e.getMessage(), e); } return filePath; }
3、保存记录
ublic ComplaintTapeEntity importComplaintTapeFile(String complaintId, String filePath) throws ServiceException { Optional<ComplaintEntity> complaintTapeEntityOptional = complaintRepository.findById(complaintId); if (!complaintTapeEntityOptional.isPresent()) { throw new ServiceException("工单记录不存在"); } ComplaintTapeEntity complaintTapeEntity = new ComplaintTapeEntity(); complaintTapeEntity.setComplaintId(complaintId); complaintTapeEntity.setTapeFile(filePath); ComplaintTapeEntity returnComplaintTapeEntity = iComplaintTapeRepository.save(complaintTapeEntity); return returnComplaintTapeEntity; }
上传音频文件
最新推荐文章于 2024-09-17 11:50:15 发布