我有一个嵌入标签,例如
<embed id="player" style="height:100%;width:100%"src="../PlayAudio2" controller="true" autoplay="true" autostart="True" type="audio/wav" />
我可以从 servlet doGet
播放文件
File file = new File("Z:/53611.wav");
FileInputStream fis;
byte[] buffer=null;
try {
fis = new FileInputStream(file);
buffer= new byte[fis.available()];
fis.read(buffer);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
response.setContentType("audio/vnd.wave");
try {
response.getOutputStream().write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我试过在 Spring 有办法吗
<embed id="player" style="height:100%;width:100%"src="PlayAudio.html" controller="true" autoplay="true" autostart="True" type="audio/wav" />
请求处理程序代码与 servlet 中相同,但这里甚至没有收到 PlayAudio.html
的请求。我在 Spring 中该怎么做。
编辑: Controller
@Controller
@RequestMapping("main")
public class ApplicationController {
@RequestMapping(value="Login.html",method=RequestMethod.POST)
public String showhome(){
return "home";
}
@RequestMapping(value="PlayFile.html",method=RequestMethod.GET)
public void playAudio(HttpServletRequest request,HttpServletResponse response){
System.out.println("--playFile");
File file = new File("Z:/53611.wav");
FileInputStream fis;
byte[] buffer=null;
try {
fis = new FileInputStream(file);
buffer= new byte[fis.available()];
fis.read(buffer);
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("audio/vnd.wave");
try{
response.getOutputStream().write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我在 webcontent 下的主文件夹中有 Home.jsp
<embed id="player" style="height:100%;width:100%" src="PlayFile.html" controller="true" autoplay="true" autostart="True" type="audio/wav" />
和 url 映射
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
但它不起作用(没有收到 PlayFile.html
处理程序的请求)
最佳答案
我相信 src 需要指向 Controller 中的 RequestMapping URL。
@RequestMapping(value = "/audio", method = RequestMethod.GET)
public ModelAndView getAudio(HttpServletResponse) { ... response.getOutputStream().write(buffer); ... }
然后在 src 中引用它,如下所示:
<embed id="player" style="height:100%;width:100%"src="<your-context-path>/audio" controller="true" autoplay="true" autostart="True" type="audio/wav" />
关于java - spring如何在响应流中发送音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863229/