2010-01-24 15 views
0

我有一個應用程序使用html5讓用戶在服務器上監聽一些音頻樣本。如果我指定的路徑在服務器上的實際的音頻文件,一切按預期工作:瀏覽器處理真實文件與servlet生成文件不同

<audio src="/audio/english/banana_1.mp3" controls> 
    Your browser does not support the <code>audio</code> element. 
</audio> 

但是,如果我點到的資源,實際上是產生音頻文件一個servlet,音頻不似乎玩:

<audio src="/app/dbAudio" controls> 
    Your browser does not support the <code>audio</code> element. 
</audio> 

我已經確認,這個servlet寫出所需的音頻文件;我已經下載並直接播放。我也已將響應contentType設置爲audio/mpeg和audio/x-mpeg,但不設骰子。是否還需要設置其他標題才能使其工作?謝謝你的幫助。

@RequestMapping(value = "/dbAudio", method = RequestMethod.GET) 
    public void getAudioFromDB(HttpServletRequest request, 
     HttpServletResponse response) throws Exception{ 

     response.setContentType("audio/mpeg"); 
     // Uncommenting the next allows me to download the resource directly and 
     // confirm that it is a well formed audio file 
     // response.setHeader("Content-disposition", "attachment; filename=banana.mp3"); 

     OutputStream o = response.getOutputStream(); 

     Resource r = ctx.getResources("/audio/english/banana_1.mp3")[0]; 
     InputStream s = r.getInputStream(); 

     int chunk = 0; 
     while(true){ 
      chunk = s.read(); 
      if(chunk == -1) break; 
      o.write(chunk); 
     } 
     o.flush();   
    } 
+1

獲取並比較兩個請求的響應標頭。如果你使用Stuck,請將它們添加到你的問題中。 – BalusC 2010-01-24 07:34:05

回答

1

試試這個snippet

+0

呃,你能否告訴我爲什麼會這樣呢? – BalusC 2010-01-24 07:45:06

+1

我不是專家,但也許由於內容的長度? http://www.w3.org/Protocols/HTTP/Object_Headers.html#content-length – JRL 2010-01-24 07:50:39

+0

太棒了,這個鏈接還指出了響應的長度。這解決了我的問題。謝謝。 – 2010-01-24 07:57:30