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

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

问题描述

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

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.

详细故事:我对 Flex、javascript、PHP 和 Python 一无所知,我想记录(从客户端浏览器")一个波形文件并将其发送到服务器以保存它(用于进一步的 ASR 处理).

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).

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

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);    
?>    

最后一点是,我确信如果我创建了一个套接字服务器并将请求定向到它,我将能够轻松获取媒体,但我希望所有内容都由 Servlet 处理.

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.

推荐答案

基本上,Java servlet 等价于以下 PHP 行,这是代码中的关键行,

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');    

InputStream input = request.getInputStream();

这基本上返回唯一的 HTTP 请求正文.您可以按照通常的 Java 方式将其写入任意 OutputStream.例如,一个 new FileOutputStream("/some.wav").

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").

您应该只意识到 HTTP 请求正文只能读取一次,并且当您调用任何 request.getParameterXxx() 方法时,它会被隐式解析.因此,如果您对请求 URI 查询字符串中的参数也感兴趣,那么您应该改为使用

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();

并自己进一步解析(即在 & 上拆分,然后在 = 上拆分,然后在 URLDecode 上拆分名称和值).

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

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

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