java servlet对范围请求的响应失败

时间:2018-04-09 07:04:58

标签: java servlets http-headers

我正在尝试创建一个可以满足范围请求的媒体服务器。最初,没有部分内容(发送完整的视频数据),服务器工作。但是,现在它不起作用。我能够获得范围标题的开始和结束索引。我在浏览器中收到响应状态和响应类型,但没有收到其他标头和响应正文。

以下是代码:

String range = request.getHeader("Range") ;
RandomAccessFile file = new RandomAccessFile(path , "r") ;
                    if(range != null) {
                        range = range.trim().substring("range=".length()) ;
                        String [] index = range.split("-") ;
                        byte [] buffer = new byte[2048] ;
                        DataOutputStream dos = new DataOutputStream(response.getOutputStream()) ;
                        if(index.length > 1) {
                            long start = Long.parseLong(index[0]) ;
                            long end = Long.parseLong(index[1]) ;
                            long contentLength = end - start + 1 ;
                            System.out.println("start: " + start);
                            System.out.println("end: " + end);
                            if(start < file.length() && end < file.length() && end > start) {
                                response.setContentType(type);
                                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                                file.seek(start);
                                long bytesleft = contentLength ; 
                                while(file.getFilePointer() <= end) {
                                    if(bytesleft >= buffer.length) {
                                        file.readFully(buffer);
                                        dos.write(buffer);
                                        dos.flush();
                                        bytesleft = bytesleft - buffer.length ;
                                    }
                                    else {
                                        file.readFully(buffer , 0 , (int) bytesleft);
                                        dos.write(buffer , 0 , (int) bytesleft);
                                        dos.flush();
                                    }
                                }
                                response.setHeader("Accept-Ranges", "bytes");
                                response.setContentLengthLong(contentLength);
                                response.setHeader("Content-Range", new StringBuilder("bytes ").append(start).append("-").append(end).append("/").append(file.length()).toString());
                            }
                            else {
                                response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
                            }
                        }
                        else {

                            long start = Long.parseLong(index[0]) ;
                            long end = file.length() - 1 ;
                            long contentLength = end - start + 1 ;
                            System.out.println("start: " + start);
                            System.out.println("end: " + end);
                            if(start < file.length()) {
                                response.setContentType(type);
                                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                                file.seek(start);
                                long bytesleft = contentLength ; 
                                while(file.getFilePointer() <= end) {
                                    if(bytesleft >= buffer.length) {
                                        file.readFully(buffer);
                                        dos.write(buffer);
                                        dos.flush();
                                        bytesleft = bytesleft - buffer.length ;
                                    }
                                    else {
                                        file.readFully(buffer , 0 , (int) bytesleft);
                                        dos.write(buffer , 0 , (int) bytesleft);
                                        dos.flush();
                                    }
                                }
                                response.setHeader("Accept-Ranges", "bytes");
                                response.setContentLengthLong(contentLength);
                                response.setHeader("Content-Range", new StringBuilder("bytes ").append(start).append("-").append(end).append("/").append(file.length()).toString());
                            }
                            else {
                                response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
                            }
                        }
                    }
                    file.close();

输出是:

start: 0
end: 43541165

1 个答案:

答案 0 :(得分:0)

我发现了错误。基本上,我应该在编写文件内容之前设置标题,现在它已成功运行。

这是新代码:

String range = request.getHeader("Range") ;
RandomAccessFile file = new RandomAccessFile(path , "r") ;
                    if(range != null) {
                        range = range.trim().substring("range=".length()) ;
                        String [] index = range.split("-") ;
                        byte [] buffer = new byte[2048] ;
                        DataOutputStream dos = new DataOutputStream(response.getOutputStream()) ;
                        if(index.length > 1) {
                            long start = Long.parseLong(index[0]) ;
                            long end = Long.parseLong(index[1]) ;
                            long contentLength = end - start + 1 ;
                            System.out.println("start: " + start);
                            System.out.println("end: " + end);
                            if(start < file.length() && end < file.length() && end > start) {
                                response.setContentType(type);
                                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                                response.setHeader("Accept-Ranges", "bytes");
                                response.setContentLengthLong(contentLength);
                                response.setHeader("Content-Range", new StringBuilder("bytes ").append(start).append("-").append(end).append("/").append(file.length()).toString());
                                file.seek(start);
                                long bytesleft = contentLength ; 
                                while(file.getFilePointer() <= end) {
                                    if(bytesleft >= buffer.length) {
                                        file.readFully(buffer);
                                        dos.write(buffer);
                                        dos.flush();
                                        bytesleft = bytesleft - buffer.length ;
                                    }
                                    else {
                                        file.readFully(buffer , 0 , (int) bytesleft);
                                        dos.write(buffer , 0 , (int) bytesleft);
                                        dos.flush();
                                    }
                                }

                            }
                            else {
                                response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
                            }
                        }
                        else {

                            long start = Long.parseLong(index[0]) ;
                            long end = file.length() - 1 ;
                            long contentLength = end - start + 1 ;
                            System.out.println("start: " + start);
                            System.out.println("end: " + end);
                            if(start < file.length()) {
                                response.setContentType(type);
                                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                                response.setHeader("Accept-Ranges", "bytes");
                                response.setContentLengthLong(contentLength);
                                response.setHeader("Content-Range", new StringBuilder("bytes ").append(start).append("-").append(end).append("/").append(file.length()).toString());
                                file.seek(start);
                                long bytesleft = contentLength ; 
                                while(file.getFilePointer() <= end) {
                                    if(bytesleft >= buffer.length) {
                                        file.readFully(buffer);
                                        dos.write(buffer);
                                        dos.flush();
                                        bytesleft = bytesleft - buffer.length ;
                                    }
                                    else {
                                        file.readFully(buffer , 0 , (int) bytesleft);
                                        dos.write(buffer , 0 , (int) bytesleft);
                                        dos.flush();
                                    }
                                }

                            }
                            else {
                                response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
                            }
                        }
                    }
                    file.close();