音频文件转为二进制流

1、主方法

@GetMapping(value = "/download/complaintTapeFile")
@Operation(summary = "下载工单音频文件", description = "根据工单音频文件id下载工单音频文件")
@TimedOperation(summary = "根据工单音频文件id下载工单音频文件", description = "根据工单音频文件id下载工单音频文件")
public ResponseEntity<byte[]> downloadComplaintTapeFile(
    @Parameter(required = true, description = "工单音频文件id", allowEmptyValue = false, allowReserved = true) @RequestParam(value = "complaintTapeId") String complaintTapeId)
throws ControllerException {

    if (StringUtils.isEmpty(complaintTapeId)) {
        throw new ControllerException("id不可为空!");
    }
    try {
        // 根据工单音频文件id下载工单音频文件
        ComplaintTapeEntity complaintTapeEntity = complaintTapeService
        .findComplaintTapById(complaintTapeId);
        byte[] bytes = FileHandleHelper.downloadFile(complaintTapeEntity.getTapeFile());

        // 设置响应头
        HttpHeaders headers = FileHandleHelper.handleTapeFileHeader(complaintTapeEntity.getTapeFile());

        return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_OCTET_STREAM)
        .body(bytes);
    } catch (Exception e) {
        String errorMessage = "下载失败:" + e.getMessage();
        log.error(errorMessage, e);
        throw new ControllerException(errorMessage);
    }
}

2、查询文件存放位置

 public ComplaintTapeEntity findComplaintTapById(String complaintTapeId) throws ServiceException {
        Optional<ComplaintTapeEntity> complaintTapeEntityOptional = iComplaintTapeRepository.findById(complaintTapeId);
        if (!complaintTapeEntityOptional.isPresent()) {
            throw new ServiceException("工单录音文件不存在");
        }

        return complaintTapeEntityOptional.get();
    }

3、将文件转为二进制流

public static byte[] downloadFile(String filePath) throws ServiceException {
                Path fullPath = Paths.get(userPath, filePath);

                if (!Files.exists(fullPath)) {
                        throw new ServiceException("文件不存在!");
                }

                File file = fullPath.toFile();
                // 预分配字节数组,避免文件过大导致内存溢出
                byte[] bytes = new byte[(int) file.length()];
                try (InputStream inputStream = new FileInputStream(file)) {
                        inputStream.read(bytes);
                        return bytes;
                } catch (IOException e) {
                        throw new ServiceException(e.getMessage(), e);
                }
        }

4、设置响应头

public static HttpHeaders handleTapeFileHeader(String filePath) throws ServiceException {

                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.parseMediaType("audio/wav"));
                try {
                        Path fullPath = Paths.get(userPath, filePath);

                        String fileName = URLEncoder.encode(fullPath.getFileName().toString(),
                                        StandardCharsets.UTF_8.toString());
                        headers.setContentDispositionFormData("attachment", fileName);
                        return headers;
                } catch (UnsupportedEncodingException e) {
                        throw new ServiceException(e.getMessage(), e);

                }
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿伟来了an

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

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

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

打赏作者

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

抵扣说明:

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

余额充值