Receiving an audio file with servlets

A Brief History: I have a Servlet that receives a request (getContentType () = audio / x-wav) that I cannot read. I need to read this wave and save it on the server side.

Detailed history: I do not know anything about Flex, javascript, PHP and Python, I want to write a wave file (on the client side of the Browser) and send it to the server to save it (for further processing of ASR).

After some searching, I found a library called Wami-Recorder (uses flex and a java script) that I already used, but it did not give me any example on the side of the java server, there was also a lack of documentation, so I decided to get my dirty hands to make it work. it contains server side python and a PHP example (I have listed PHP one):

<?php # Save the audio to a URL-accessible directory for playback. parse_str($_SERVER['QUERY_STRING'], $params); $name = isset($params['name']) ? $params['name'] : 'output.wav'; $content = file_get_contents('php://input'); $fh = fopen($name, 'w') or die("can't open file"); fwrite($fh, $content); fclose($fh); ?> 

Finally, I’m sure that if I created a socket server and sent a request to it, I can easily get the media, but I want everything to be handled by servlets.

+2
source share
1 answer

Basically, a Java servlet is the equivalent of the next PHP line, which is the key line in the code,

 $content = file_get_contents('php://input'); 

is an

 InputStream input = request.getInputStream(); 

This returns basically a single HTTP request object. You can write it to an arbitrary OutputStream usual Java way. For example, a new FileOutputStream("/some.wav") .

You should understand that the body of an HTTP request can only be read once, and that it will be implicitly parsed when you call any of the request.getParameterXxx() methods. Therefore, if you are also interested in the parameters in the query string of the request URI, you should use instead

 String queryString = request.getQueryString(); 

and analyze it further (i.e. divide by & , then divide by = , then URLDecode name and value).

+2
source

Source: https://habr.com/ru/post/979455/


All Articles