修复从远程下载rds-binlog文件到本地时响应头获取不到Content-Length导致的NPE的问题 (#4760)

This commit is contained in:
施昱平
2023-07-18 16:16:50 +08:00
committed by GitHub
parent bfb8cf4190
commit 04a2b6f941

View File

@@ -22,6 +22,7 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
@@ -209,7 +210,9 @@ public class BinlogDownloadQueue {
private static void saveFile(File parentFile, String fileName, HttpResponse response) throws IOException {
InputStream is = response.getEntity().getContent();
long totalSize = Long.parseLong(response.getFirstHeader("Content-Length").getValue());
boolean isChunked = response.getEntity().isChunked();
Header contentLengthHeader = response.getFirstHeader("Content-Length");
long totalSize = isChunked || contentLengthHeader == null ? 0 : Long.parseLong(contentLengthHeader.getValue());
if (response.getFirstHeader("Content-Disposition") != null) {
fileName = response.getFirstHeader("Content-Disposition").getValue();
fileName = StringUtils.substringAfter(fileName, "filename=");
@@ -262,6 +265,7 @@ public class BinlogDownloadQueue {
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
copySize += len;
if (totalSize > 0){
long progress = copySize * 100 / totalSize;
if (progress >= nextPrintProgress) {
logger.info("download " + file.getName() + " progress : " + progress
@@ -269,6 +273,7 @@ public class BinlogDownloadQueue {
nextPrintProgress += 10;
}
}
}
logger.info("download file " + file.getName() + " end!");
fos.flush();
} finally {