用servlet接收音频文件 [英] Receive audio file with servlet

查看:165
本文介绍了用servlet接收音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的故事:
我有一个Servlet,它接收到我无法读取的请求(getContentType()= audio / x-wav)。我需要读这个wave并保存在服务器端。

详细的故事:
我对Flex,JavaScript,PHP和Python一无所知,我想(从客户端浏览器)记录一个波形文件,并发送到服务器保存(进一步的ASR处理)。

经过一番搜索,我找到了一个名为Wami-Recorder的库(使用flex和java scrip),我已经使用过了,但是没有给出任何java服务器端的例子,它也缺乏文档,所以我决定让我的手脏,让它工作。
它包含一个服务器端的python和PHP示例(我将列出PHP的一个):

$ p $ <?php
#将音频保存到URL可访问的目录进行播放。
parse_str($ _ SERVER ['QUERY_STRING'],$ params);
$ name = isset($ params ['name'])? $ params ['name']:'output.wav';
$ content = file_get_contents('php:// input');
$ fh = fopen($ name,'w')或死(无法打开文件);
fwrite($ fh,$ content);
fclose($ fh);
?>

最后需要说明的是,我确定如果我创建了套接字服务器并将请求发送给它,我将能够很容易地获得媒体,但我希望一切都由Servlets来处理。

基本上,Java

  $ content = file_get_contents('php: //输入'); 



  InputStream input = request.getInputStream(); 

这基本上返回唯一的HTTP请求主体。你可以把它写成任意的 OutputStream 通常的Java方式。例如,一个新的FileOutputStream(/ some.wav)

你只应该认识到HTTP请求体只能被读取一次,而且当你调用任何请求时,它将被隐式地解析.getParameterXxx()方法。所以如果你对请求URI查询字符串中的参数感兴趣,那么你应该使用

  String queryString = request.getQueryString(); 

并自己进一步解析(即分割& ,然后拆分 = ,然后 URLDecode 名称和值。 b

Brief Story: I have a Servlet which receives a request (getContentType() = audio/x-wav) that I can't read. I need to read this wave and save it on the server side.

Detailed Story: I know nothing about Flex, javascript, PHP and Python, I want to record (from the client side "Browser") a wave file and send it to the server to save it (for further ASR processing).

After some searching I found a library called Wami-Recorder (uses flex and java scrip) which I already used, but it didn't give me any java server side example, it also lacks the documentation so I decided to get my hands dirty to get it working. it contains a server side python and PHP example (I will list the 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);    
?>    

A final note is that I am sure if I created a socket server and directed the request to it, I will be able to get the media easily, but I want everything to be handled by the Servlets.

解决方案

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

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

is

InputStream input = request.getInputStream();

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

You should only realize that the HTTP request body can be read only once and also that it would implicitly be parsed when you invoke any of the request.getParameterXxx() methods. So if you're interested in the parameters in the request URI query string as well, then you should instead use

String queryString = request.getQueryString();

and parse it further yourself (i.e. split on &, then split on =, then URLDecode the name and value).

这篇关于用servlet接收音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆