音乐优化
This commit is contained in:
@@ -256,4 +256,152 @@ public class AliConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取OSS对象的长度
|
||||
*
|
||||
* @param objectName OSS对象名称
|
||||
* @return 文件长度,如果获取失败返回null
|
||||
*/
|
||||
public static Long getOssObjectLength(String objectName) {
|
||||
// 配置参数
|
||||
String endpoint = "https://oss-cn-beijing.aliyuncs.com";
|
||||
String accessKeyId = AliKeyConfig.ACCESS_KEY_ID;
|
||||
String accessKeySecret = AliKeyConfig.ACCESS_KEY_SECRET;
|
||||
String bucketName = "wenzhuangmusic";
|
||||
|
||||
OSS ossClient = null;
|
||||
try {
|
||||
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
// 判断文件是否存在
|
||||
boolean exists = ossClient.doesObjectExist(bucketName, objectName);
|
||||
if (!exists) {
|
||||
log.error("OSS文件不存在: {}", objectName);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取文件元数据
|
||||
com.aliyun.oss.model.ObjectMetadata metadata = ossClient.getObjectMetadata(bucketName, objectName);
|
||||
return metadata.getContentLength();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取OSS文件长度失败: {}", e.getMessage());
|
||||
return null;
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 范围下载OSS文件,支持HTTP Range请求
|
||||
*
|
||||
* @param objectName OSS对象名称
|
||||
* @param outputStream 输出流
|
||||
* @param start 开始字节位置
|
||||
* @param end 结束字节位置
|
||||
* @return 是否下载成功
|
||||
*/
|
||||
public static boolean ossDownloadStreamRange(String objectName, java.io.OutputStream outputStream, long start, long end) {
|
||||
// 配置参数
|
||||
String endpoint = "https://oss-cn-beijing.aliyuncs.com";
|
||||
String accessKeyId = AliKeyConfig.ACCESS_KEY_ID;
|
||||
String accessKeySecret = AliKeyConfig.ACCESS_KEY_SECRET;
|
||||
String bucketName = "wenzhuangmusic";
|
||||
|
||||
OSS ossClient = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
// 判断文件是否存在
|
||||
boolean exists = ossClient.doesObjectExist(bucketName, objectName);
|
||||
if (!exists) {
|
||||
log.error("OSS文件不存在: {}", objectName);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 创建范围请求
|
||||
com.aliyun.oss.model.GetObjectRequest getObjectRequest = new com.aliyun.oss.model.GetObjectRequest(bucketName, objectName);
|
||||
getObjectRequest.setRange(start, end);
|
||||
|
||||
// 获取文件对象
|
||||
OSSObject ossObject = ossClient.getObject(getObjectRequest);
|
||||
inputStream = ossObject.getObjectContent();
|
||||
|
||||
// 使用缓冲区进行流式传输
|
||||
byte[] buffer = new byte[8192]; // 8KB缓冲区
|
||||
int bytesRead;
|
||||
long totalBytesRead = 0;
|
||||
long expectedBytes = end - start + 1;
|
||||
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1 && totalBytesRead < expectedBytes) {
|
||||
try {
|
||||
// 确保不超过请求的字节范围
|
||||
int bytesToWrite = (int) Math.min(bytesRead, expectedBytes - totalBytesRead);
|
||||
outputStream.write(buffer, 0, bytesToWrite);
|
||||
totalBytesRead += bytesToWrite;
|
||||
|
||||
// 定期刷新输出流
|
||||
if (bytesToWrite == buffer.length) {
|
||||
outputStream.flush();
|
||||
}
|
||||
} catch (java.io.IOException e) {
|
||||
// 检查是否为客户端断开连接的错误
|
||||
if (e.getMessage() != null &&
|
||||
(e.getMessage().contains("Broken pipe") ||
|
||||
e.getMessage().contains("Connection reset by peer") ||
|
||||
e.getMessage().contains("连接被对方重置") ||
|
||||
e.getMessage().contains("你的主机中的软件中止了一个已建立的连接") ||
|
||||
e.getMessage().contains("Software caused connection abort") ||
|
||||
e.getMessage().contains("SocketTimeoutException"))) {
|
||||
// 客户端已断开连接,记录日志但不作为错误处理
|
||||
log.info("客户端断开连接,文件传输中断: {}", e.getMessage());
|
||||
return true; // 返回true,因为这不是服务器端的错误
|
||||
} else {
|
||||
// 其他IO错误,重新抛出
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
outputStream.flush();
|
||||
return true;
|
||||
|
||||
} catch (java.io.IOException e) {
|
||||
// 检查是否为客户端断开连接的错误
|
||||
if (e.getMessage() != null &&
|
||||
(e.getMessage().contains("Broken pipe") ||
|
||||
e.getMessage().contains("Connection reset by peer") ||
|
||||
e.getMessage().contains("连接被对方重置") ||
|
||||
e.getMessage().contains("你的主机中的软件中止了一个已建立的连接") ||
|
||||
e.getMessage().contains("Software caused connection abort") ||
|
||||
e.getMessage().contains("SocketTimeoutException"))) {
|
||||
// 客户端已断开连接,记录日志但不作为错误处理
|
||||
log.info("客户端断开连接,文件传输中断: {}", e.getMessage());
|
||||
return true; // 返回true,因为这不是服务器端的错误
|
||||
} else {
|
||||
// 其他IO错误
|
||||
log.error("范围下载OSS文件失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("范围下载OSS文件失败: {}", e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
// 关闭资源
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (java.io.IOException e) {
|
||||
log.error("关闭输入流失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user