我正在尝试使用https://code.google.com/p/wami-recorder/在浏览器端录制声音片段,然后通过wami示例和服务器端控制器方法上传该文件。出于某种原因,request.body
InputStream
会抛出此错误:
java.io.IOException: Bad file descriptor
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:198)
at controllers.APIVoicemailController.addNewRecording(APIVoicemailController.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:548)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:502)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:478)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:256)
at play.Invoker$Invocation.run(Invoker.java:276)
at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:234)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
路线POST /upload
的控制器方法
public static void addNewRecording() {
if (request.method.equals(Constants.METH_POST)) {
try {
InputStream inputStream = request.body;
Map<String, Header> headerMap = request.headers;
for (Entry<String, Header> entry : headerMap.entrySet()) {
Logger.info("Head: " + entry.getKey() + " name; "
+ entry.getValue().name + " val: " + entry.getValue().value());
}
OutputStream out = new FileOutputStream(new File(
"/Users/franklovecchio/Desktop/file.mp3"));
int read = 0;
byte[] bytes = new byte[1024];
/* ERROR ON LINE BELOW */
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
for (Entry<String, String> params : request.params.allSimple().entrySet()) {
Logger.info("k: " + params.getKey() + "v: " + params.getValue());
}
renderCustomMessage("Created new recording.", "Success.");
}
}
我该如何解决这个问题?
答案 0 :(得分:1)
我正在使用Play 2,但是如果你只是在没有检查标题的情况下获得InputStream
的内容,那么你就可以将它存储在WAV文件或数据库中而没有任何问题。
答案 1 :(得分:0)
我最近的表现完全相同。所遵循的步骤如下:
1.获取发布到record.php的Wami的示例代码
2.将记录网址从record.php更改为Play中与我的控制器对应的网址
3.在控制器内部,只保存收到的数据而不检查标题。(可能这是我在您的代码中看到的差异)。 使用firebug可以帮助您查看请求类型(无论是音频/ ..还是不是)
数据包含在请求正文中,只是将其作为输入流读取并将其写入一个足以满足我的mp3。