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