mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-09-13 16:34:53 +00:00
优化:区分 HTTP 错误,返回明确原因 (#752)
* 优化:区分 HTTP 错误,返回明确原因 * 修复 缓存关闭共享的 client,会导致后续请求失败 * 修复 缓存关闭共享的 client,会导致后续请求失败 --------- Co-authored-by: 高雄 <admin@cxcp.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import org.apache.commons.io.FileUtils;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -46,9 +47,8 @@ public class DownloadUtils {
|
||||
}
|
||||
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
|
||||
String realPath = getRelFilePath(fileName, fileAttribute);
|
||||
// 获取文件后缀用于校验
|
||||
final String fileSuffix = fileAttribute.getSuffix();
|
||||
// 判断是否非法地址
|
||||
|
||||
if (KkFileUtils.isIllegalFileName(realPath)) {
|
||||
response.setCode(1);
|
||||
response.setContent(null);
|
||||
@@ -61,17 +61,17 @@ public class DownloadUtils {
|
||||
response.setMsg("下载失败:不支持的类型!" + urlStr);
|
||||
return response;
|
||||
}
|
||||
if (fileAttribute.isCompressFile()) { //压缩包文件 直接赋予路径 不予下载
|
||||
if (fileAttribute.isCompressFile()) {
|
||||
response.setContent(fileDir + fileName);
|
||||
response.setMsg(fileName);
|
||||
return response;
|
||||
}
|
||||
// 如果文件是否已经存在、且不强制更新,则直接返回文件路径
|
||||
if (KkFileUtils.isExist(realPath) && !fileAttribute.forceUpdatedCache()) {
|
||||
response.setContent(realPath);
|
||||
response.setMsg(fileName);
|
||||
return response;
|
||||
}
|
||||
|
||||
try {
|
||||
URL url = WebUtils.normalizedURL(urlStr);
|
||||
if (!fileAttribute.getSkipDownLoad()) {
|
||||
@@ -79,39 +79,59 @@ public class DownloadUtils {
|
||||
File realFile = new File(realPath);
|
||||
CloseableHttpClient httpClient = HttpRequestUtils.createConfiguredHttpClient();
|
||||
String finalUrlStr = urlStr;
|
||||
HttpRequestUtils.executeHttpRequest(url, httpClient, fileAttribute, responseWrapper -> {
|
||||
// 获取响应头中的Content-Type
|
||||
String contentType = responseWrapper.getContentType();
|
||||
|
||||
// 如果是Office/设计文件,需要校验MIME类型
|
||||
final boolean[] hasMimeError = {false};
|
||||
final String[] mimeErrorMessage = {null};
|
||||
|
||||
HttpRequestUtils.executeHttpRequest(url, httpClient, fileAttribute, responseWrapper -> {
|
||||
String contentType = responseWrapper.getContentType();
|
||||
if (WebUtils.isMimeCheckRequired(fileSuffix)) {
|
||||
if (!WebUtils.isValidMimeType(contentType, fileSuffix)) {
|
||||
logger.error("文件类型错误,期望二进制文件但接收到文本类型,url: {}, Content-Type: {}",
|
||||
finalUrlStr, contentType);
|
||||
responseWrapper.setHasError(true);
|
||||
hasMimeError[0] = true;
|
||||
mimeErrorMessage[0] = "期望二进制文件但接收到文本类型,Content-Type: " + contentType;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
FileUtils.copyToFile(responseWrapper.getInputStream(), realFile);
|
||||
});
|
||||
|
||||
if (hasMimeError[0]) {
|
||||
response.setCode(1);
|
||||
response.setContent(null);
|
||||
response.setMsg(mimeErrorMessage[0]);
|
||||
return response;
|
||||
}
|
||||
|
||||
} else if (isFtpUrl(url)) {
|
||||
String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
|
||||
String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
|
||||
String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
|
||||
String ftpport = WebUtils.getUrlParameterReg(realPath, URL_PARAM_FTP_PORT);
|
||||
FtpUtils.download(fileAttribute.getUrl(), ftpport, realPath, ftpUsername, ftpPassword, ftpControlEncoding);
|
||||
} else if (isFileUrl(url)) { // 添加对file协议的支持
|
||||
} else if (isFileUrl(url)) {
|
||||
handleFileProtocol(url, realPath);
|
||||
} else {
|
||||
response.setCode(1);
|
||||
response.setMsg("url不能识别url" + urlStr);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
response.setContent(realPath);
|
||||
response.setMsg(fileName);
|
||||
return response;
|
||||
|
||||
} catch (HttpClientErrorException e) {
|
||||
logger.error("HTTP请求失败,状态码:{},url:{}", e.getStatusCode(), urlStr);
|
||||
response.setCode(1);
|
||||
response.setContent(null);
|
||||
if (e.getStatusCode().is4xxClientError()) {
|
||||
response.setMsg("文件不存在或无法访问 (HTTP " + e.getStatusCode() + ")");
|
||||
} else {
|
||||
response.setMsg("下载失败: " + e.getMessage());
|
||||
}
|
||||
return response;
|
||||
} catch (IOException | GalimatiasParseException e) {
|
||||
logger.error("文件下载失败,url:{}", urlStr);
|
||||
response.setCode(1);
|
||||
@@ -123,7 +143,11 @@ public class DownloadUtils {
|
||||
}
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
logger.error("下载文件时发生未知异常,url:{}", urlStr, e);
|
||||
response.setCode(1);
|
||||
response.setContent(null);
|
||||
response.setMsg("下载失败: " + e.getMessage());
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user