上传音频文件

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;

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿伟来了an

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值