老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了
下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了
寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧.
業務場景:視頻分段傳輸、視頻多段傳輸(理論上配合前端能實作視頻預覽功能, 沒有嘗試過)
下面是API測驗圖
-
請求頭設定
-
回傳結果
-
回應頭結果
-
這是我寫給前端同學的檔案,炊訓看看吧...擺爛了
- 若存在快取則設定請求頭:If-None-Match ETAG
如果不存在快取:直接不設定該請求頭 - 如果想把一次Range請求分成多次進行,那么就要設定該請求頭(可以不設定,不設定直接過驗證, 設定的話比較規范)
設定請求頭:If-Match ETAG(若錯誤的ETAG,回傳412,SC_PRECONDITION_FAILED) - 設定Range請求頭:
比如檔案總大小100
標準格式:bytes=-20/20 表示后20個位元組;bytes=20-100/80 表示20-100總計80個位元組
bytes=20-40/20,60-80/20 表示一個Range請求回傳兩個檔案塊,這也是Range請求存在的意義
若Range請求不規范,則回傳416,SC_REQUESTED_RANGE_NOT_SATISFIABLE - If-Range請求頭,可以不設定;If-Range 頭欄位通常用于斷點續傳的下載程序中,用來自從上次中斷后,確保下載的資源沒有發生改變,
If-Range ETAG 如果ETAG不相等,那么直接回傳全部的檔案即 bytes:0-size(不進行分段) - 設定Accept請求頭,不設定或者不為video/mp4則默認attachment
inline是斷點傳輸需要的,而attachment就是出現另存為對話框(檔案下載) - 回應頭需要注意的就是ETAG是快取的身份標識,Expires是快取的過期時間
package org.demo.util;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.demo.constant.EntityConstant;
import org.demo.mapper.VideoMapper;
import org.demo.pojo.Video;
import org.demo.service.MinioService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by kevin on 10/02/15.
* See full code here : https://github.com/davinkevin/Podcast-Server/blob/d927d9b8cb9ea1268af74316cd20b7192ca92da7/src/main/java/lan/dk/podcastserver/utils/multipart/MultipartFileSender.java
* Updated by limecoder on 23/04/19
*/
@Slf4j
@Component(value = "https://www.cnblogs.com/LimeCoder/archive/2023/04/19/multipartFileSender")
@RequiredArgsConstructor
@Scope("prototype")
public class MultipartFileSender {
private static final int DEFAULT_BUFFER_SIZE = 20480; // ..bytes = 20KB.
private static final long DEFAULT_EXPIRE_TIME = 604800000L; // ..ms = 1 week.
private static final String MULTIPART_BOUNDARY = "MULTIPART_BYTERANGES";
private static final String PATTERN = "^bytes=\\d*-\\d*(/\\d*)?(,\\d*-\\d*(/\\d*)?)*$";
private final HttpServletRequest request;
private final HttpServletResponse response;
private final VideoMapper videoMapper;
private final MinioService minioService;
public void sent(Long videoId) throws Exception {
if (response == null || request == null) {
log.warn("http-request/http-response 注入失敗");
return;
}
Video video = videoMapper.selectById(videoId);
/*
* 處理視頻不存在的情況
* */
if (video == null) {
log.error("videoId doesn't exist at database : {}", videoId);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Long size = video.getSize();
String md5 = video.getMd5();
// 處理快取資訊 ---------------------------------------------------
/*
* If-None-Match是快取請求頭,如果快取的值與檔案的md5相同或者值為*,那么就直接提示前端直接使用快取即可
* 并將md5再次回傳給前端
* */
// If-None-Match header should contain "*" or ETag. If so, then return 304.
String ifNoneMatch = request.getHeader("If-None-Match");
if (ifNoneMatch != null && HttpUtils.matches(ifNoneMatch, md5)) {
response.setHeader("ETag", md5); // Required in 304.
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// 確保Range請求合法 ----------------------------------------------------
/*
* 對于 GET 和 HEAD 方法,搭配 Range首部使用,可以用來保證新請求的范圍與之前請求的范圍是對同一份資源的請求,
* 如果 ETag 無法匹配,那么需要回傳 416 (Range Not Satisfiable,范圍請求無法滿足) 回應,
* */
// If-Match header should contain "*" or ETag. If not, then return 412.
String ifMatch = request.getHeader("If-Match");
if (ifMatch != null && !HttpUtils.matches(ifMatch, md5)) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
// 驗證和決議Range請求頭 -------------------------------------------------------------
// Prepare some variables. The full Range represents the complete file.
Range full = new Range(0, size - 1, size);
List<Range> ranges = new ArrayList<>();
// Validate and process Range and If-Range headers.
String range = request.getHeader("Range");
if (range != null) {
/*
* 如果Range請求頭不滿足規范格式,那么發送錯誤請求
* */
// Range header should match format "bytes=n-n,n-n,n-n...". If not, then return 416.
if (!range.matches(PATTERN)) {
response.setHeader("Content-Range", "bytes */" + size); // Required in 416.
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
/*
* If-Range 頭欄位通常用于斷點續傳的下載程序中,用來自從上次中斷后,確保下載的資源沒有發生改變,
* */
String ifRange = request.getHeader("If-Range");
if (ifRange != null && !ifRange.equals(md5)) {
// 如果資源發生了改變,直接將資料全部回傳
ranges.add(full);
}
/*
* 如果If-Range請求頭是合法的,也就是視頻資料并沒有更新
* 例子:bytes:10-80,bytes:80-180
* */
// If any valid If-Range header, then process each part of byte range.
if (ranges.isEmpty()) {
// substring去除bytes:
for (String part : range.substring(6).split(",")) {
// Assuming a file with size of 100, the following examples returns bytes at:
// 50-80 (50 to 80), 40- (40 to size=100), -20 (size-20=80 to size=100).
//去除多余空格
part = part.trim();
/*
* 解決20-80及20-80/60的切割問題
* */
long start = Range.subLong(part, 0, part.indexOf("-"));
int index1 = part.indexOf("/");
int index2 = part.length();
int index = index2 > index1 && index1 > 0 ? index1 : index2;
long end = Range.subLong(part, part.indexOf("-") + 1, index);
// 如果是-開頭的情況 -20
if (start == -1) {
start = size - end;
end = size - 1;
// 如果是20但沒有-的情況,或者end> size - 1的情況
} else if (end == -1 || end > size - 1) {
end = size - 1;
}
/*
* 如果范圍不合法, 80-10
* */
// Check if Range is syntactically valid. If not, then return 416.
if (start > end) {
response.setHeader("Content-Range", "bytes */" + size); // Required in 416.
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
// Add range.
ranges.add(new Range(start, end, size));
}
}
}
// Prepare and initialize response --------------------------------------------------------
// Get content type by file name and set content disposition.
String disposition = "inline";
// If content type is unknown, then set the default value.
// For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
// To add new content types, add new mime-mapping entry in web.xml.
String contentType = "video/mp4";
/*
* 經過測驗當accept為"video/mp4"是inline, 其他情況都是attachment
* */
// Else, expect for images, determine content disposition. If content type is supported by
// the browser, then set to inline, else attachment which will pop a 'save as' dialogue.
String accept = request.getHeader("Accept");
disposition = accept != null && HttpUtils.accepts(accept, contentType) ? "inline" : "attachment";
log.debug("Content-Type : {}", contentType);
// Initialize response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setHeader("Content-Type", contentType);
String videoPath = video.getVideoPath();
response.setHeader("Content-Disposition", disposition + ";filename=\"" + videoPath.substring(videoPath.lastIndexOf('/') + 1) + "\"");
log.debug("Content-Disposition: {}, fileName: {}", disposition, videoPath.substring(videoPath.lastIndexOf('/') + 1));
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("ETag", md5);
// 設定快取過期時間
response.setDateHeader("Expires", System.currentTimeMillis() + DEFAULT_EXPIRE_TIME);
// Send requested file (part(s)) to client ------------------------------------------------
/*
* 注意minioService okhttp3經過測驗最大只能一次傳8kb, 而bufferedInputStream的默認快取區恰好8kb
* */
// Prepare streams.
try (InputStream input = new BufferedInputStream(minioService.getDownloadInputStream(EntityConstant.VIDEO_BUCKET, videoPath));
ServletOutputStream output = response.getOutputStream()) {
if (ranges.isEmpty() || ranges.get(0) == full) {
// Return full file.
log.debug("回傳全部的視頻檔案,不進行劃分");
response.setContentType(contentType);
response.setHeader("Content-Range", "bytes " + full.start + "-" + full.end + "/" + full.total);
response.setHeader("Content-Length", String.valueOf(full.length));
Range.copy(input, output, size, full.start, full.length);
} else if (ranges.size() == 1) {
// Return single part of file.
Range r = ranges.get(0);
log.info("Return 1 part of file : from ({}) to ({})", r.start, r.end);
response.setContentType(contentType);
response.setHeader("Content-Range", "bytes " + r.start + "-" + r.end + "/" + r.total);
response.setHeader("Content-Length", String.valueOf(r.length));
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
// Copy single part range.
Range.copy(input, output, size, r.start, r.length);
} else {
/* 發送多種資料的多部分物件集合:
多部分物件集合包含:
1、multipart/form-data
在web表單檔案上傳時使用
2、multipart/byteranges
狀態碼206回應報文包含了多個范圍的內容時使用,*/
// Return multiple parts of file.
response.setContentType("multipart/byteranges; boundary=" + MULTIPART_BOUNDARY);
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
// Cast back to ServletOutputStream to get the easy println methods.
// Copy multi part range.
for (Range r : ranges) {
log.debug("Return multi part of file : from ({}) to ({})", r.start, r.end);
// Add multipart boundary and header fields for every range.
output.println();
output.println("--" + MULTIPART_BOUNDARY);
output.println("Content-Type: " + contentType);
output.println("Content-Range: bytes " + r.start + "-" + r.end + "/" + r.total);
// Copy single part range of multi part range.
Range.copy(input, output, size, r.start, r.length);
}
// End with multipart boundary.
output.println();
output.println("--" + MULTIPART_BOUNDARY + "--");
}
}
}
private static class Range {
long start;
long end;
long length;
long total;
/**
* Construct a byte range.
* @param start Start of the byte range.
* @param end End of the byte range.
* @param total Total length of the byte source.
*/
public Range(long start, long end, long total) {
this.start = start;
this.end = end;
this.length = end - start + 1;
this.total = total;
}
public static long subLong(String value, int beginIndex, int endIndex) {
String substring = value.substring(beginIndex, endIndex);
return (substring.length() > 0) ? Long.parseLong(substring) : -1;
}
private static void copy(InputStream input, OutputStream output, long inputSize, long start, long length) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int read;
if (inputSize == length) {
// Write full range.
while ((read = input.read(buffer)) > 0) {
output.write(buffer, 0, read);
output.flush();
}
} else {
input.skip(start);
long toRead = length;
while ((read = input.read(buffer)) > 0) {
if ((toRead -= read) > 0) {
output.write(buffer, 0, read);
output.flush();
} else {
output.write(buffer, 0, (int) toRead + read);
output.flush();
break;
}
}
}
}
}
private static class HttpUtils {
/**
* Returns true if the given accept header accepts the given value.
* @param acceptHeader The accept header.
* @param toAccept The value to be accepted.
* @return True if the given accept header accepts the given value.
*/
public static boolean accepts(String acceptHeader, String toAccept) {
String[] acceptValues = acceptHeader.split("\\s*(,|;)\\s*");
Arrays.sort(acceptValues);
return Arrays.binarySearch(acceptValues, toAccept) > -1
|| Arrays.binarySearch(acceptValues, toAccept.replaceAll("/.*$", "/*")) > -1
|| Arrays.binarySearch(acceptValues, "*/*") > -1;
}
/**
* Returns true if the given match header matches the given value.
* @param matchHeader The match header.
* @param toMatch The value to be matched.
* @return True if the given match header matches the given value.
*/
public static boolean matches(String matchHeader, String toMatch) {
String[] matchValues = matchHeader.split("\\s*,\\s*");
Arrays.sort(matchValues);
return Arrays.binarySearch(matchValues, toMatch) > -1
|| Arrays.binarySearch(matchValues, "*") > -1;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/550533.html
標籤:其他