wenj you hua
This commit is contained in:
@@ -160,4 +160,100 @@ public class AliConfig {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式下载OSS文件,直接写入输出流,避免一次性加载整个文件到内存
|
||||
*
|
||||
* @param objectName OSS对象名称
|
||||
* @param outputStream 输出流
|
||||
* @return 是否下载成功
|
||||
*/
|
||||
public static boolean ossDownloadStream(String objectName, java.io.OutputStream outputStream) {
|
||||
// 配置参数
|
||||
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;
|
||||
}
|
||||
|
||||
// 获取文件对象
|
||||
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
||||
inputStream = ossObject.getObjectContent();
|
||||
|
||||
// 使用缓冲区进行流式传输
|
||||
byte[] buffer = new byte[8192]; // 8KB缓冲区
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
try {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
// 定期刷新输出流,避免缓冲区溢出
|
||||
if (bytesRead == 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -101,6 +101,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
|
||||
<select id="selectShareInfoListPage" resultType="com.ruoyi.common.core.domain.entity.ShareInfo">
|
||||
SELECT
|
||||
si.id as id,
|
||||
si.share_num as shareNum,
|
||||
su.username as username,
|
||||
su.nickname as nickname,
|
||||
su.head_img as headImg,
|
||||
|
||||
Reference in New Issue
Block a user