2017-02-04 31 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详细说明如何处理范围请求。