语音文件上传正式

This commit is contained in:
joylink_zhangsai 2020-11-25 18:26:15 +08:00
parent de5fa9030f
commit d2ec25cfb0
3 changed files with 64 additions and 10 deletions

View File

@ -98,6 +98,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>

View File

@ -1,11 +1,20 @@
package club.joylink.rtss.services;
import club.joylink.rtss.exception.BusinessExceptionAssertEnum;
import club.joylink.rtss.util.JsonUtils;
import club.joylink.rtss.vo.CommonJsonResponse;
import club.joylink.rtss.vo.client.VoiceRecognitionResult;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.bytebuddy.utility.RandomString;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@ -14,13 +23,14 @@ import java.util.Objects;
public interface IVoiceService {
String AudioFileBasePath = "/usr/local/joylink/jlcloud/audio/";
static String getFilePath() {
LocalDate now = LocalDate.now();
String datePath = now.getYear() + File.separator + now.getMonthValue() + File.separator + now.getDayOfMonth();
String fileName = System.currentTimeMillis() + "-" + RandomString.make() + ".wav";
String directoryPath = AudioFileBasePath + datePath;
File directory = new File(directoryPath);
if(!directory.exists()) {
if (!directory.exists()) {
directory.mkdirs();
}
String filePath = directoryPath + File.separator + fileName;
@ -29,6 +39,7 @@ public interface IVoiceService {
/**
* 保存语音文件
*
* @param inputStream 此方法结束时会进行关闭
* @return
* @throws IOException
@ -46,33 +57,63 @@ public interface IVoiceService {
}
saveFile = new File(filePath);
} finally {
if(Objects.nonNull(inputStream)) {
if (Objects.nonNull(inputStream)) {
inputStream.close();
}
if(os != null) {
if (os != null) {
os.close();
}
}
return new VoiceFile(filePath.replace(AudioFileBasePath, ""), saveFile);
}
static VoiceFile saveFile(MultipartFile multipartFile) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://joylink.club/jlfile/api/upload/AUDIO?appId=00001&appSecret=joylink00001";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
String fileName = multipartFile.getName();
String prefix = fileName.substring(fileName.lastIndexOf("."));
File tempFile = null;
try {
tempFile = File.createTempFile("fileName", prefix);
multipartFile.transferTo(tempFile);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new FileSystemResource(tempFile));
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);
String body = responseEntity.getBody();
// body = body.replaceAll("\\\\", "/");
CommonJsonResponse response = JsonUtils.read(body, CommonJsonResponse.class);
BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.assertEquals(200, response.getCode());
String path = (String) response.getData();
File file = new File("https://joylink.club/oss/joylink" + path);
return new VoiceFile(path, file);
} catch (IOException e) {
throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("创建文件报错");
} finally {
if (tempFile != null)
tempFile.delete();
}
}
static VoiceFile handleAndSaveFile(MultipartFile file) throws IOException {
String contentType = file.getContentType();
BusinessExceptionAssertEnum.UNSUPPORTED_FILE_FORMAT.assertTrue(
"audio/wave".equals(contentType) || "audio/wav".equals(contentType),
String.format("不支持的文件格式[%s]", contentType));
InputStream inputStream = file.getInputStream();
return saveFile(inputStream);
// InputStream inputStream = file.getInputStream();
return saveFile(file);
}
/**
* 识别服务器收到的语音文件
* @param file
* @param lang
* @return
*/
VoiceRecognitionResult voiceRecognition(MultipartFile file, String lang);
/**
* 语音合成返回文件路径
*
* @return 文件路径
*/
String synthesis(String message, String per);

View File

@ -5,12 +5,15 @@ import club.joylink.rtss.services.IVoiceService;
import club.joylink.rtss.vo.client.VoiceRecognitionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@Slf4j
@Service("baiDuVoiceService")
@ -22,6 +25,9 @@ public class VoiceServiceImpl implements IVoiceService {
@Autowired
private TtsService ttsService;
@Autowired
private RestTemplate restTemplate;
@Override
public VoiceRecognitionResult voiceRecognition(MultipartFile multipartFile, String lang) {
try {
@ -40,7 +46,9 @@ public class VoiceServiceImpl implements IVoiceService {
public String synthesis(String message, String per) {
try {
byte[] data = this.ttsService.run(message, per, TokenHolder.getInstance().getToken());
VoiceFile voiceFile = IVoiceService.saveFile(new ByteArrayInputStream(data));//生成的音频数据
InputStream inputStream = new ByteArrayInputStream(data);
MultipartFile file = new MockMultipartFile("语音文件.wav", inputStream);
VoiceFile voiceFile = IVoiceService.saveFile(file);//生成的音频数据
return voiceFile.getPath();
} catch (IOException e) {
throw BusinessExceptionAssertEnum.THIRD_SERVICE_CALL_EXCEPTION.exception();