删除征信解析后的本地文件

This commit is contained in:
wkc
2026-07-20 15:55:28 +08:00
parent 90f483f427
commit dc8055ce7a
4 changed files with 111 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ import com.ruoyi.lsfx.domain.response.CreditParseInvokeResponse;
import com.ruoyi.lsfx.domain.response.CreditParsePayload;
import com.ruoyi.lsfx.domain.response.CreditParseResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@@ -33,6 +34,7 @@ import java.util.Map;
/**
* 征信维护服务实现
*/
@Slf4j
@Service
public class CcdiCreditInfoServiceImpl implements ICcdiCreditInfoService {
@@ -150,7 +152,7 @@ public class CcdiCreditInfoServiceImpl implements ICcdiCreditInfoService {
private void handleSingleFile(MultipartFile multipartFile, String userName, CallerContext caller) throws Exception {
CreditHtmlStorageService.StoredCreditHtml storedHtml = creditHtmlStorageService.save(multipartFile);
CreditParseInvokeResponse response = creditParseClient.parse(caller, storedHtml.remotePath());
CreditParseInvokeResponse response = parseAndDeleteLocalFile(caller, storedHtml);
CreditParsePayload payload = requireResponse(response).getPayload();
Map<String, Object> header = requireHeader(payload);
String personId = stringValue(header.get("query_cert_no"));
@@ -164,6 +166,25 @@ public class CcdiCreditInfoServiceImpl implements ICcdiCreditInfoService {
replaceEmployeeCredit(personId, debts, negative, userName);
}
private CreditParseInvokeResponse parseAndDeleteLocalFile(
CallerContext caller, CreditHtmlStorageService.StoredCreditHtml storedHtml) throws Exception {
CreditParseInvokeResponse response;
try {
response = creditParseClient.parse(caller, storedHtml.remotePath());
} catch (Exception parseException) {
try {
creditHtmlStorageService.delete(storedHtml);
} catch (Exception deleteException) {
parseException.addSuppressed(deleteException);
log.error("征信解析失败后清理本地文件失败", deleteException);
}
throw parseException;
}
creditHtmlStorageService.delete(storedHtml);
return response;
}
private void validateHtmlFile(MultipartFile file) {
String originalFilename = file == null ? null : file.getOriginalFilename();
if (originalFilename == null) {

View File

@@ -8,6 +8,10 @@ import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 征信 HTML 服务器落盘与远程访问地址生成。
@@ -16,6 +20,7 @@ import java.io.File;
public class CreditHtmlStorageService {
private static final String CREDIT_HTML_DIR = "credit-html";
private static final String CREDIT_HTML_PROFILE_PREFIX = "/profile/" + CREDIT_HTML_DIR + "/";
private static final String[] HTML_EXTENSIONS = {"html", "htm"};
@Value("${credit-parse.api.file-public-base-url}")
@@ -26,6 +31,25 @@ public class CreditHtmlStorageService {
return new StoredCreditHtml(profilePath, buildRemotePath(profilePath));
}
public void delete(StoredCreditHtml storedHtml) throws IOException {
String profilePath = storedHtml == null ? null : storedHtml.profilePath();
if (StringUtils.isBlank(profilePath) || !profilePath.startsWith(CREDIT_HTML_PROFILE_PREFIX)) {
throw new IllegalArgumentException("征信HTML本地文件路径非法");
}
String relativePath = profilePath.substring(CREDIT_HTML_PROFILE_PREFIX.length());
if (StringUtils.isBlank(relativePath)) {
throw new IllegalArgumentException("征信HTML本地文件路径非法");
}
Path baseDir = Paths.get(getCreditHtmlBaseDir()).toAbsolutePath().normalize();
Path targetFile = baseDir.resolve(relativePath).normalize();
if (!targetFile.startsWith(baseDir)) {
throw new IllegalArgumentException("征信HTML本地文件路径越界");
}
Files.deleteIfExists(targetFile);
}
private String getCreditHtmlBaseDir() {
return RuoYiConfig.getProfile() + File.separator + CREDIT_HTML_DIR;
}