fix 修复 文件下载 设置content-length无效问题

dev
疯狂的狮子Li 11 months ago
parent ab56ec5fb7
commit c7a51847bd

@ -32,6 +32,7 @@ import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.function.Consumer;
/**
* S3 S3
@ -242,7 +243,7 @@ public class OssClient {
* @return
* @throws OssException
*/
public long download(String key, OutputStream out) {
public void download(String key, OutputStream out, Consumer<Long> consumer) {
try {
// 构建下载请求
DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
@ -258,7 +259,10 @@ public class OssClient {
Download<ResponseInputStream<GetObjectResponse>> responseFuture = transferManager.download(downloadRequest);
// 输出到流中
try (ResponseInputStream<GetObjectResponse> responseStream = responseFuture.completionFuture().join().result()) { // auto-closeable stream
return responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread
if (consumer != null) {
consumer.accept(responseStream.response().contentLength());
}
responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread
}
} catch (Exception e) {
throw new OssException("文件下载失败,错误信息:[" + e.getMessage() + "]");

@ -156,8 +156,7 @@ public class SysOssServiceImpl implements ISysOssService {
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
OssClient storage = OssFactory.instance(sysOss.getService());
long contentLength = storage.download(sysOss.getFileName(), response.getOutputStream());
response.setContentLengthLong(contentLength);
storage.download(sysOss.getFileName(), response.getOutputStream(), response::setContentLengthLong);
}
/**

Loading…
Cancel
Save