java中怎么处理视频流_java视频流的断点续传功能

该博客介绍了如何在Java中处理视频流,特别是实现了断点续传功能。通过检查HTTP请求的Range头,根据不同的范围类型设置Content-Length和Content-Range响应头,从而实现从指定位置继续下载视频流。
摘要由CSDN通过智能技术生成

/*** 视频文件的断点续传功能

*@parampath 文件路径

*request request

*response response*/

public voidvoidPlay(String path,HttpServletRequest request,HttpServletResponse response) {

BufferedInputStream bis= null;try{

File file= newFile(path);if(file.exists()) {long p = 0L;long toLength = 0Llong contentLength = 0Lint rangeSwitch = 0; //0,从头开始的全文下载;1,从某字节开始的下载(bytes=27000-);2,从某字节开始到某字节结束的下载(bytes=27000-39000)

longfileLength;

String rangBytes= "";

fileLength=file.length();get file content

InputStream ins = FileInputStream(file);

bis= BufferedInputStream(ins);tell the client to allow accept-ranges

response.reset();

response.setHeader("Accept-Ranges","bytes");client requests a file block download start byte

String range = request.getHeader("Range");if (range != null && range.trim().length() > 0 && !"null".equals(range)) {

response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);

rangBytes= range.replaceAll("bytes=","");if (rangBytes.endsWith("-")) { bytes=270000-

rangeSwitch = 1;

p= Long.parseLong(rangBytes.substring(0,rangBytes.indexOf("-")));

contentLength= fileLength - p; 客户端请求的是270000之后的字节(包括bytes下标索引为270000的字节)

} else { bytes=270000-320000

rangeSwitch = 2;

String temp1= rangBytes.substring(0,1)">));

String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1,rangBytes.length());

p=Long.parseLong(temp1);

toLength=Long.parseLong(temp2);

contentLength= toLength - p + 1; 客户端请求的是 270000-320000 之间的字节

}

}else{

contentLength=fileLength;

}如果设设置了Content-Length,则客户端会自动进行多线程下载。如果不希望支持多线程,则不要设置这个参数。Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]

response.setHeader("Content-Length"断点开始响应的格式是:Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]

if (rangeSwitch == 1) {

String contentRange= new StringBuffer("bytes ").append(new Long(p).toString()).append("-")

.append(new Long(fileLength - 1).toString()).append("/"Long(fileLength).toString()).toString();

response.setHeader("Content-Range"else if (rangeSwitch == 2) {

String contentRange= range.replace("="," ") + "/" +Long.toString(fileLength);

response.setHeader("Content-Range"{

String contentRange= new StringBuffer("bytes ").append("0-").append(fileLength - 1).append("/")

.append(fileLength).toString();

response.setHeader("Content-Range"file.getName();

response.setContentType("application/octet-stream");

response.addHeader("Content-Disposition","attachment;filename=" +fileName);

OutputStream out=response.getOutputStream();int n = 0long readLength = 0int bsize = 1024byte[] bytes = new byte[bsize];) {针对 bytes=27000-39000 的请求,从27000开始写数据

while (readLength <= contentLength -bsize) {

n=bis.read(bytes);

readLength+=n;

out.write(bytes,0if (readLength <=contentLength) {

n= bis.read(bytes,(int) (contentLength -readLength));

out.write(bytes,n);

}

}{while ((n = bis.read(bytes)) != -1) {

out.write(bytes,n);

}

}

out.flush();

out.close();

bis.close();

}

}catch(IOException ie) {忽略 ClientAbortException 之类的异常

} (Exception e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值