java - spring如何在响应流中发送音频

标签 java spring servlets spring-mvc audio-streaming

我有一个嵌入标签,例如

<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/

相关文章:

java - 创建一个通用方法来查找对象列表中某个属性的最大值

java - 为什么移位运算符>>中的负数结果与正常除法不同?

java - 未调用 Servlet 过滤器

html - 如何通过将 String 从 servlet 传递给它来设置 HTML 文本区域的内容

java - 为什么使用 j_username 和 SPRING_SECURITY_LAST_USERNAME 变量?

java - 记录 java outofmemoryerror stacktrace 控制台输出

java - 导入的maven项目尝试使用jre系统库

Spring 服务和存储库层约定

Java:如何理解神秘的堆栈跟踪?

java - 在另一个项目中重用 Spring 测试上下文