java视频ios无法播放_java处理苹果浏览器safari无法播放视频流(Accept-Ranges)部分代码参考可以针对自己业务进行修改
java.nio.file.Path path = Paths.get(PropertiesUtil.getValue("file_root_path"), model.getFile_path() + File.separator + model.getId() + "." + model.getFile_suffix());
File file = path.toFile();
String ifModifiedSince = request.getHeader("If-Modified-Since");
if (ifModifiedSince != null && !ifModifiedSince.equals("")) {
Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
long fileLastModifiedSeconds = file.lastModified() / 1000;
if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
Calendar time = new GregorianCalendar();
response.setHeader("Date", dateFormatter.format(time.getTime()));
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
}
String rangeString = request.getHeader("Range");
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
response.setHeader("Accept-Ranges","bytes");
long fileLength = randomAccessFile.length();
String contentType = Files.probeContentType(file.toPath());
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[MB_1];
FileInputStream inputStream = this.toStream(file);
BufferedInputStream bufferedInputStream = this.toBuffered(inputStream);
int length = -1;
while ((length = bufferedInputStream.read(buffer, 0, 1024)) != -1) {
md.update(buffer, 0, length);
}
bufferedInputStream.close();
byte[] md5Bytes = md.digest();
BigInteger bigInt = new BigInteger(1, md5Bytes);
response.setHeader("ETag", bigInt.toString(16));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
response.setHeader("Content-Type", contentType);
int start = 0, end = 0;
if(rangeString != null && rangeString.startsWith("bytes=")){
String[] values = rangeString.split("=")[1].split("-");
start = Integer.parseInt(values[0]);
if(values.length > 1){
end = Integer.parseInt(values[1]);
}
}
int requestSize = 0;
if(end != 0 && end > start){
requestSize = end - start + 1;
} else {
requestSize = MB_1;
}
if(StringUtils.isEmpty(rangeString)){
response.setHeader("Content-length", fileLength + "");
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.CHINA);
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
Calendar time = new GregorianCalendar();
response.setHeader("Date", dateFormatter.format(time.getTime()));
time.add(Calendar.SECOND, 60);
response.setHeader("Expires", dateFormatter.format(time.getTime()));
response.setHeader("Cache-Control", "private, max-age=" + 60);
response.setHeader("Last-Modified", dateFormatter.format(new Date(file.lastModified())));
response.setStatus(HttpServletResponse.SC_OK);
}else {
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
long requestStart = 0, requestEnd = 0;
String[] ranges = rangeString.split("=");
if(ranges.length > 1){
String[] rangeDatas = ranges[1].split("-");
requestStart = Integer.parseInt(rangeDatas[0]);
if(rangeDatas.length > 1){
requestEnd = Integer.parseInt(rangeDatas[1]);
}
}
long length = 0;
if(requestEnd > 0){
length = requestEnd - requestStart + 1;
response.setHeader("Content-length", "" + length);
response.setHeader("Content-Range", "bytes " + requestStart + "-" + requestEnd + "/" + fileLength);
}else{
length = fileLength - requestStart;
response.setHeader("Content-length", "" + length);
response.setHeader("Content-Range", "bytes "+ requestStart + "-" + (fileLength - 1) + "/" + fileLength);
}
byte[] bytes = new byte[1024 * 1024];
OutputStream out = response.getOutputStream();
int needSize = requestSize;
randomAccessFile.seek(start);
while(needSize > 0){
int len = randomAccessFile.read(bytes);
if(needSize < bytes.length){
out.write(bytes, 0, needSize);
} else {
out.write(bytes, 0, len);
if(len < bytes.length){
break;
}
}
needSize -= bytes.length;
}
out.close();
randomAccessFile.close();
}