StreamingOutput - 流完成写入之前的EofException

时间:2017-02-04 21:23:46

标签: java jersey jetty streaming

我试图传输本地mp3文件。我测试的歌曲长5:33s。在Chrome和edge中,歌曲在播放前不到4分钟播放。

这是我的服务

@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

我已经尝试在我的AppConfig中将ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER设置为0无效。

1 个答案:

答案 0 :(得分:0)

对于那些不希望被某些自命不凡的人阻挠的人来说,这是一个答案:)

通过取消原始请求并发送带有范围标题的第二个请求,结果显示我使用句柄content-type=audio/mp3测试的2个浏览器。

Here's another post详细说明了如何处理范围请求。