2017-02-04 39 views
-2

我試圖流本地mp3文件。我正在測試的歌曲長度爲5:33。在鉻和邊緣歌曲播放不到4分鐘之前停止。StreamingOutput - 流寫完之前的EofException

這裏是我的服務

@Path ("clips") 
public class ClipService { 

    @GET 
    @Path(...) 
    @Produces("audio/mp3") 
    public Response streamAudio() { 

     Clip clip = ... 
     StreamingOutput output = buildStreamingOutput(clip); 
     return Response.ok(output).header(HttpHeaders.CONTENT_LENGTH,   clip.getLength()).build(); 
    } 

    private StreamingOutput buildStreamingOutput(Clip clip) 
     throws Exception { 

     return new StreamingOutput() { 
      @Override 
      public void write(final OutputStream out) 
       throws IOException, WebApplicationException { 

       byte[] buffer = new byte[1024]; 
       int bytesRead; 
       try (InputStream in = clip.getInputStream()) { 
        while ((bytesRead = in.read(buffer)) > 0) { 
         out.write(buffer, 0, bytesRead); 
        } 
        out.flush(); 
       } 
      } 
     }; 
    } 
} 

剪輯類

public class Clip { 

    private InputStream inputStream; // The file 
    private long length; // File.length() 

而且我得到的錯誤...

SEVERE: An I/O error has occurred while writing a response message entity to the container output stream. 
org.glassfish.jersey.server.internal.process.MappableException: org.eclipse.jetty.io.EofException 
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:92) 
    ... 
Caused by: org.eclipse.jetty.io.EofException 
    at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:200) 
    ... 
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine 
    at sun.nio.ch.SocketDispatcher.write0(Native Method) 
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51) 
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) 
    at sun.nio.ch.IOUtil.write(IOUtil.java:51) 
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471) 
    at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:178) 
    ... 67 more 

我試過設置ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER爲0我的AppConfig無濟於事。

+0

您沒有關於誰低估了你的任何信息,但看看堆棧跟蹤。 'EofException'是由'IOException'引發的,所以'IOException'是這裏的實際問題,所以我的編輯是正確的,並且很有幫助,而且我的編輯不是。不要這樣做。我試圖幫助你。 – EJP

+0

我將爲Jetty開發人員在閱讀時發生文件結尾的情況添加內容。在寫作時拋棄文件是荒謬的。最好不要把這個例外包裝起來,或者甚至不抓住它。 – EJP

+0

你需要停止猜測並開始閱讀。我已經清楚地說明了我編輯的原因和動機,以及你的標題有什麼問題。 '缺少標點符號'是一個荒謬的理由,拒絕整個編輯,直接從您的堆棧跟蹤複製/粘貼。如果你不希望你的問題由能夠解釋「IOException」的人解答,那麼你就可以用正確的方法解決它。我已經將這場編輯大戰引發了版主的注意。 – EJP

回答

0

下面是對於那些你們誰不希望被某些自命不凡受阻的回答什麼,而不是:)

通過取消原來的請求和發送第二原來的2個瀏覽器我用手柄content-type=audio/mp3測試請求範圍標題。

Here's another post詳細說明如何處理範圍請求。