fix 修复 文件下载 设置content-length无效问题
This commit is contained in:
parent
e1a26b0388
commit
e8acfac091
@ -32,6 +32,7 @@ import java.net.URL;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* S3 存储协议 所有兼容S3协议的云厂商均支持
|
* S3 存储协议 所有兼容S3协议的云厂商均支持
|
||||||
@ -239,10 +240,11 @@ public class OssClient {
|
|||||||
*
|
*
|
||||||
* @param key 文件在 Amazon S3 中的对象键
|
* @param key 文件在 Amazon S3 中的对象键
|
||||||
* @param out 输出流
|
* @param out 输出流
|
||||||
|
* @param consumer 自定义处理逻辑
|
||||||
* @return 输出流中写入的字节数(长度)
|
* @return 输出流中写入的字节数(长度)
|
||||||
* @throws OssException 如果下载失败,抛出自定义异常
|
* @throws OssException 如果下载失败,抛出自定义异常
|
||||||
*/
|
*/
|
||||||
public long download(String key, OutputStream out) {
|
public void download(String key, OutputStream out, Consumer<Long> consumer) {
|
||||||
try {
|
try {
|
||||||
// 构建下载请求
|
// 构建下载请求
|
||||||
DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
|
DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
|
||||||
@ -258,7 +260,10 @@ public class OssClient {
|
|||||||
Download<ResponseInputStream<GetObjectResponse>> responseFuture = transferManager.download(downloadRequest);
|
Download<ResponseInputStream<GetObjectResponse>> responseFuture = transferManager.download(downloadRequest);
|
||||||
// 输出到流中
|
// 输出到流中
|
||||||
try (ResponseInputStream<GetObjectResponse> responseStream = responseFuture.completionFuture().join().result()) { // auto-closeable stream
|
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) {
|
} catch (Exception e) {
|
||||||
throw new OssException("文件下载失败,错误信息:[" + e.getMessage() + "]");
|
throw new OssException("文件下载失败,错误信息:[" + e.getMessage() + "]");
|
||||||
|
@ -178,8 +178,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
|
|||||||
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
|
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
|
||||||
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
|
||||||
OssClient storage = OssFactory.instance(sysOss.getService());
|
OssClient storage = OssFactory.instance(sysOss.getService());
|
||||||
long contentLength = storage.download(sysOss.getFileName(), response.getOutputStream());
|
storage.download(sysOss.getFileName(), response.getOutputStream(), response::setContentLengthLong);
|
||||||
response.setContentLengthLong(contentLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user