0

In my Spring Boot application, I accept an audiofile as MultipartFile with @RequestParam. I know, I can convert the file into some InputStream. I am also able to convert it into some byte array:

@PostMapping("/microsoft")
void transcribe(@RequestParam("file") MultipartFile file) {
  InputStream inputStream =  new BufferedInputStream(file.getInputStream());
  byte[] byteArr = file.getBytes();

  AudioConfig audioConfig = AudioConfig.???;    //here the correct method with my file needs to be called
}

For transcription using Microsoft API, I need to create some Audioconfig object. This is the link to the class. I used in the past fromWavFileInput(...) when I loaded a local audio file. But now, I need to be able to use the MultipartFile. I have no idea which method of the AudioConfig class I can use and how to convert the file correctly.

Henning
  • 39
  • 8
  • Why don't you create a [temporary file](https://stackoverflow.com/questions/26860167/what-is-a-safe-way-to-create-a-temp-file-in-java) then combine all parts from into it with [MultipartFile.transferTo](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html#transferTo(java.io.File)) , and then use [AudioConfig.fromAudioFileOutput](https://learn.microsoft.com/en-us/javascript/api/microsoft-cognitiveservices-speech-sdk/audioconfig?view=azure-node-latest#microsoft-cognitiveservices-speech-sdk-audioconfig-fromaudiofileoutput) ? – Victor Gubin Jan 18 '23 at 15:34

1 Answers1

0

The idea is, to create a temp file and transfer the MultipartFile into it:

File tempFile = File.createTempFile("prefix-", "-suffix");
file.transferTo(tempFile);

The use fromWavFileOutput with the path of the temporary file:

AudioConfig audioConfig = AudioConfig.fromWavFileOutput(tempFile.getPath());

For me, the temp file exceeds its maximum permitted size. To get this solved, make sure to add spring.servlet.multipart.max-file-size=-1 in your application.properties file to set limit to infinity.

Henning
  • 39
  • 8