From df0252952fa8b064a2fc5295d1d79bb117b9b1aa Mon Sep 17 00:00:00 2001 From: taojinlong Date: Thu, 13 Mar 2025 00:35:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=20ftp=20?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E5=90=8D=E7=A7=B0=E7=9A=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=A4=B1=E8=B4=A5=20(#15335)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: taojinlong --- .../datasource/provider/ExcelUtils.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java b/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java index 6f1a43facf..6015cd38ec 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/provider/ExcelUtils.java @@ -30,6 +30,8 @@ import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import java.io.*; +import java.net.URL; +import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; @@ -761,6 +763,12 @@ public class ExcelUtils { DEException.throwException(Translator.get("i18n_invalid_address")); } } + if(StringUtils.isNotEmpty(remoteExcelRequest.getUserName())){ + username = remoteExcelRequest.getUserName(); + } + if(StringUtils.isNotEmpty(remoteExcelRequest.getPasswd())){ + password = remoteExcelRequest.getPasswd(); + } filePath = filePath.startsWith("/") ? filePath.substring(1) : filePath; String suffix = filePath.substring(filePath.lastIndexOf(".") + 1); if (!Arrays.asList("csv", "xlsx", "xls").contains(suffix)) { @@ -772,20 +780,26 @@ public class ExcelUtils { fileNames.put("tranName", tranName); try { - String command = "curl -o " + localFilePath; // 在Unix/Linux系统中 - if (StringUtils.isNotEmpty(remoteExcelRequest.getUserName()) && StringUtils.isNotEmpty(remoteExcelRequest.getPasswd())) { - command = command + " -u " + remoteExcelRequest.getUserName() + ":" + remoteExcelRequest.getPasswd(); + URL url; + if(StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)){ + url= new URL("ftp://" + username + ":" +password + "@" + serverAddress + "/" + filePath); + }else { + url= new URL("ftp://" + serverAddress + "/" + filePath); } - command = command + " " + remoteExcelRequest.getUrl(); - Process process = Runtime.getRuntime().exec(command); - int exitValue = process.waitFor(); - if (exitValue != 0) { - DEException.throwException(Translator.get("i18n_file_download_failed")); + + URLConnection conn = url.openConnection(); + InputStream inputStream = conn.getInputStream(); + FileOutputStream outputStream = new FileOutputStream(localFilePath); + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); } + inputStream.close(); + outputStream.close(); + } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - throw new RuntimeException(e); + DEException.throwException(Translator.get("i18n_file_download_failed")); } return fileNames; }