private void ossPathDownload(HttpServletResponse response, HttpServletRequest request, String ossPath) {
log.info("oss下载path={}",ossPath);
ObjectMetadata objectMetadata = amazonS3.getObjectMetadata(bucketName,ossPath);
long contentLength = 0L;
long p = 0L;
long toLength = 0L;
int rangeSwitch = 0; // 0,从头开始的全文下载;1,从某字节开始的下载(bytes=27000-);2,从某字节开始到某字节结束的下载(bytes=27000-39000)
long fileLength = objectMetadata.getContentLength();
String rangBytes = "";
// 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(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, rangBytes.indexOf("-"));
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", new Long(contentLength).toString());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName,ossPath);
// 断点开始
// 响应的格式是:
// 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("/")
.append(new Long(fileLength).toString()).toString();
response.setHeader("Content-Range", contentRange);
getObjectRequest = getObjectRequest.withRange(p);
} else if (rangeSwitch == 2) {
String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
response.setHeader("Content-Range", contentRange);
getObjectRequest = getObjectRequest.withRange(p,toLength);;
} else {
String contentRange = new StringBuffer("bytes ").append("0-")
.append(fileLength - 1).append("/")
.append(fileLength).toString();
response.setHeader("Content-Range", contentRange);
}
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
S3Object s3Object = amazonS3.getObject(getObjectRequest);
try (InputStream inputStream = s3Object.getObjectContent();
OutputStream outputStream = response.getOutputStream()){
String realFileName = URLEncoder.encode(ossPath.substring(ossPath.lastIndexOf("/")+1), "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + realFileName);
response.addHeader("fileSize",fileLength+"");
response.addHeader("cache-control",cacheControl);
IOUtils.copy(inputStream,outputStream);
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
Amazon S3 文件系统断点下载
最新推荐文章于 2025-03-18 12:38:08 发布