diff --git a/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java b/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java index ec9b8a4..cfdea38 100644 --- a/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java +++ b/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java @@ -1262,27 +1262,79 @@ public class HikVisionServiceImpl implements HikVisionService { //ftp图片获取 public InputStream downloadFtp(String downloadPath) { + FTPSClient ftps = null; InputStream inputStream = null; - FTPSClient ftps; + try { ftps = new FTPSClient(true); ftps.connect(ftpUrlAddress, ftpUrlPort); boolean loginRes = ftps.login(ftpUrlAccount, ftpUrlPwd); - System.out.println(loginRes); - ftps.setFileType(2); + log.info("FTP登录结果: {}", loginRes); + + if (!loginRes) { + log.error("FTP登录失败"); + return null; + } + + ftps.setFileType(2); // 二进制模式 ftps.enterLocalPassiveMode(); ftps.setControlEncoding("UTF-8"); ftps.setFileTransferMode(10); ftps.execPROT("P"); + + // 获取文件流 inputStream = ftps.retrieveFileStream(downloadPath); if (inputStream == null) { - System.out.println("[FTP] DOWNLOAD FAIL, EMPTY STREAM:" + downloadPath); + log.warn("[FTP] DOWNLOAD FAIL, EMPTY STREAM: {}", downloadPath); + return null; + } + + // 将数据读取到内存中 + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[8192]; + while ((nRead = inputStream.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); } + + // 完成文件传输操作 + boolean completed = ftps.completePendingCommand(); + if (!completed) { + log.warn("FTP文件传输未正确完成: {}", downloadPath); + } + + // 关闭原始流(重要:必须在completePendingCommand之后关闭) + inputStream.close(); + inputStream = null; // 防止finally再次关闭 + + // 创建新的InputStream返回 + byte[] fileData = buffer.toByteArray(); + log.info("成功下载文件: {}, 大小: {} 字节", downloadPath, fileData.length); + return new ByteArrayInputStream(fileData); + } catch (Exception e) { - log.error("error" + e); + log.error("FTP下载错误: " + e.getMessage(), e); + return null; + } finally { + // 关闭原始输入流 + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException ignored) {} + } + + // 关闭FTP连接 + if (ftps != null && ftps.isConnected()) { + try { + ftps.logout(); + ftps.disconnect(); + log.info("FTP连接已关闭"); + } catch (IOException e) { + log.error("FTP断开连接错误: " + e.getMessage(), e); + } + } } - return inputStream; } @@ -1471,6 +1523,9 @@ public class HikVisionServiceImpl implements HikVisionService { } else if (AlgConstants.INFRA_CAMERA_REVERSE.equals(imageType)) { //相机反算红外 InputStream inputStreamPath = downloadFtp(imagePath); + if(StringUtils.isNull(inputStreamPath)){ + log.error("inputStreamPath图片为空"); + } //调取实时测温温度 //传值,相机 ip