fix file download buffer

This commit is contained in:
ran.shang
2025-08-27 14:21:44 +08:00
parent cb83b3a0ca
commit e33f5c1953
4 changed files with 7 additions and 6 deletions

View File

@@ -19,9 +19,10 @@ public interface IFileStorage {
* 下载 * 下载
* @param filePath * @param filePath
* @param os * @param os
* @param fileSize 文件大小
* @author sr on 2022-03-01 * @author sr on 2022-03-01
*/ */
void download(String filePath, OutputStream os); void download(String filePath, OutputStream os, Long fileSize);
/** /**
* @param filePath * @param filePath

View File

@@ -103,7 +103,7 @@ public class FileServiceImpl implements FileService {
String.format("attachment; filename=\"%1$s\"; filename*=utf-8''%1$s", encodFilename)); String.format("attachment; filename=\"%1$s\"; filename*=utf-8''%1$s", encodFilename));
} }
try (OutputStream outputStream = response.getOutputStream()) { try (OutputStream outputStream = response.getOutputStream()) {
fileStorage.download(fileRecord.getFilePath(), outputStream); fileStorage.download(fileRecord.getFilePath(), outputStream, fileRecord.getFileSize());
} }
} }

View File

@@ -65,10 +65,10 @@ public class AliFileStorage implements IFileStorage {
} }
@Override @Override
public void download(String filePath, OutputStream os) { public void download(String filePath, OutputStream os, Long fileSize) {
try(InputStream is = download(filePath)) { try(InputStream is = download(filePath)) {
try { try {
IOUtils.copy(is, os); IOUtils.copy(is, os, fileSize != null ? fileSize.intValue() : 4096);
} catch (ClientAbortException e) { } catch (ClientAbortException e) {
log.warn("客户端主动断开连接: {}", e.getMessage()); log.warn("客户端主动断开连接: {}", e.getMessage());
} }

View File

@@ -31,9 +31,9 @@ public class LocalFileStorage implements IFileStorage {
} }
@Override @Override
public void download(String filePath, OutputStream os) { public void download(String filePath, OutputStream os, Long fileSize) {
try (InputStream inputStream = download(filePath)) { try (InputStream inputStream = download(filePath)) {
IOUtils.copy(inputStream, os); IOUtils.copy(inputStream, os, fileSize != null ? fileSize.intValue() : 4096);
} catch (IOException e) { } catch (IOException e) {
log.info("下载异常", e); log.info("下载异常", e);
throw new BusinessException("下载异常" + e.getMessage()); throw new BusinessException("下载异常" + e.getMessage());