最多同时加载5张图片

This commit is contained in:
2025-10-13 10:48:53 +08:00
parent 740a638444
commit 2f80c450c5

View File

@ -14,6 +14,7 @@ import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -74,30 +75,39 @@ public class DeviceExportService {
} }
// 在DeviceExportService中添加并发控制
private static final Semaphore imageLoadSemaphore = new Semaphore(5); // 最多同时加载5张图片
private void handleDevicePic(Device device, DeviceExcelExportDTO dto) { private void handleDevicePic(Device device, DeviceExcelExportDTO dto) {
String picUrl = device.getDevicePic(); String picUrl = device.getDevicePic();
log.info("处理设备图片设备ID: {}, 图片URL: {}", device.getId(), picUrl); log.debug("处理设备图片设备ID: {}, 图片URL: {}", device.getId(), picUrl);
if (picUrl != null && !picUrl.trim().isEmpty()) { if (picUrl != null && !picUrl.trim().isEmpty()) {
try { try {
// 自动将HTTP转换为HTTPS以避免重定向问题 // 获取加载图片的许可
if (picUrl.startsWith("http://")) { imageLoadSemaphore.acquire();
picUrl = "https://" + picUrl.substring(7);
log.info("自动将HTTP转换为HTTPS: {}", picUrl); try {
// 自动将HTTP转换为HTTPS以避免重定向问题
if (picUrl.startsWith("http://")) {
picUrl = "https://" + picUrl.substring(7);
log.debug("自动将HTTP转换为HTTPS: {}", picUrl);
}
// 尝试创建URL对象会自动验证格式
URL url = new URL(picUrl);
dto.setDevicePic(url);
log.debug("成功设置设备图片URL到DTO");
} finally {
// 释放许可
imageLoadSemaphore.release();
} }
// 尝试创建URL对象会自动验证格式
URL url = new URL(picUrl);
dto.setDevicePic(url);
log.info("成功设置设备图片URL到DTO");
} catch (Exception e) { } catch (Exception e) {
// 不是有效URL时设置为null log.warn("设置设备图片失败设备ID: {}, URL: {}, 错误: {}", device.getId(), picUrl, e.getMessage());
log.info("设置设备图片失败设备ID: {}, URL: {}, 错误: {}", device.getId(), picUrl, e.getMessage());
dto.setDevicePic(null); dto.setDevicePic(null);
} }
} else { } else {
log.info("设备没有设置图片设备ID: {}", device.getId()); log.debug("设备没有设置图片设备ID: {}", device.getId());
dto.setDevicePic(null); dto.setDevicePic(null);
} }
} }