语音文件流压缩

This commit is contained in:
wangxiangshun
2025-10-09 13:16:03 +08:00
parent c63c206877
commit c4efb6d9ce

View File

@@ -6,6 +6,7 @@ import com.vetti.common.ai.whisper.WhisperClient;
import com.vetti.common.config.RuoYiConfig;
import com.vetti.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import javax.websocket.*;
@@ -106,9 +107,10 @@ public class ChatWebSocketHandler {
try {
//文件转换成文件流
ByteBuffer outByteBuffer = convertFileToByteBuffer(resultPathUrl);
//发送文件流数据
session.getBasicRemote().sendBinary(outByteBuffer);
// 发送响应确认
session.getBasicRemote().sendText("已收到二进制数据,长度: " + bytes.length);
// session.getBasicRemote().sendText("已收到二进制数据,长度: " + bytes.length);
log.info("7、运行时间:{}",System.currentTimeMillis()/1000);
} catch (IOException e) {
e.printStackTrace();
@@ -185,16 +187,8 @@ public class ChatWebSocketHandler {
*/
private ByteBuffer convertFileToByteBuffer(String fileUrl) {
File file = new File(fileUrl);
// 使用RandomAccessFile获取FileChannel
try (RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel()) {
// 分配与文件大小相同的ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
// 从通道读取数据到缓冲区
channel.read(buffer);
// 切换为读模式
buffer.flip();
return buffer;
try {
return ByteBuffer.wrap(FileUtils.readFileToByteArray(file));
} catch (Exception e) {
e.printStackTrace();
}