本帖最后由 kanbe_kotori 于 2014-11-10 13:59:13 编辑

解决方案 »

  1.   

    用HTTP Analyzer监控Request与Response;多分析分析看看。
      

  2.   

    问题解决了,用到了206状态。Path path = Paths.get("/Users/jiangxingshang/Documents/workspace/eclipse/MyMusic/my-music/src/main/webapp/assets/sound/1.mp3");
    try (
    SeekableByteChannel input = Files.newByteChannel(path, StandardOpenOption.READ);
    WritableByteChannel output = Channels.newChannel(resp.getOutputStream())) {
    String range = req.getHeader("range");
    int pos = 0;
    if(range != null && range.length() > 0) {
    pos = Integer.valueOf(range.replaceFirst("bytes=", "").split("-")[0]);
    }
    int len = (int)Files.size(path);
    resp.addHeader("Accept-Ranges", "bytes");
    resp.addHeader("Connection", "keep-alive");
    if(pos > 0) {
    resp.setStatus(206);
    resp.addHeader("Content-Range", String.format("bytes %d-%d/%d", pos, len - 1, len));
    input.position(pos);
    }
    long time = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS).toMillis();
    resp.addHeader("ETag", "\"" + time + "\"");
    resp.addDateHeader("Last-Modified", time);
    resp.setContentType("audio/mpeg");
    resp.addIntHeader("Content-Length", len);
    ByteBuffer bb = ByteBuffer.allocate(2048);

    while(input.read(bb) != -1) {
    bb.flip();
    output.write(bb);
    bb.clear();
    }
    } catch(IOException e) {
    e.printStackTrace();
    }