feat: 【数据源】数据源新增对远程 Excel/CSV 的支持 #14681

This commit is contained in:
taojinlong
2025-03-04 21:30:55 +08:00
committed by taojinlong
parent b18c221f1d
commit 1fa5415fa3
27 changed files with 2194 additions and 140 deletions

View File

@@ -2,7 +2,7 @@ package io.dataease.constant;
public enum DataSourceType {oracle(1), sqlServer(2), TiDB(3), hive(4), impala(5), mariadb(6), StarRocks(7), ds_doris(8), pg(9), mongo(10), ck(11), db2(12), redshift(13), es(14), API(15),
Excel(16), influxdb(17), sls(18), kingbase(19), mongobi(20), maxcompute(21), presto(22),
dm(23), kylin(24), folder(25), doris(26), mysql(27), APILark(28);
dm(23), kylin(24), folder(25), doris(26), mysql(27), APILark(28), ExcelRemote(29);
private final Integer flag;

View File

@@ -30,10 +30,7 @@ import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@@ -369,6 +366,61 @@ public class HttpClientUtil {
return EntityUtils.toString(response.getEntity(), config.getCharset());
}
public static Map<String, String> downloadFile(String url, HttpClientConfig config, String path) {
Map<String, String> name = new HashMap<>();
try (CloseableHttpClient httpClient = buildHttpClient(url)) {
HttpGet httpGet = new HttpGet(url);
// 设置请求配置
httpGet.setConfig(config.buildRequestConfig());
// 设置请求头
config.getHeader().forEach(httpGet::addHeader);
HttpResponse response = httpClient.execute(httpGet);
String fileName = extractFileName(response, url);
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
String tranName = UUID.randomUUID().toString() + "." + suffix;
name.put("fileName", fileName);
name.put("tranName", tranName);
try {
FileOutputStream outputStream = new FileOutputStream(path + tranName);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = response.getEntity().getContent().read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} finally {
}
} catch (Exception e) {
logger.error("HttpClient查询失败", e);
throw new RuntimeException("HttpClient查询失败: " + e.getMessage(), e);
}
return name;
}
private static String extractFileName(HttpResponse response, String url) {
String fileName = "";
String disposition = response.getHeaders("Content-Disposition").toString();
if (disposition != null) {
int filenameIndex = disposition.indexOf("filename=");
if (filenameIndex > 0) {
fileName = disposition.substring(filenameIndex + 9)
.replaceAll("\"", "") // 去除引号
.trim();
}
}
if (fileName.isEmpty()) {
fileName = url.contains("/")
? url.substring(url.lastIndexOf('/') + 1)
: "download_" + System.currentTimeMillis();
}
if (fileName.trim().isEmpty()) {
fileName = "download_" + System.currentTimeMillis();
}
return fileName;
}
public static byte[] downloadBytes(String url) {
HttpClientConfig config = new HttpClientConfig();
return HttpClientUtil.downFromRemote(url, config);
@@ -383,7 +435,6 @@ public class HttpClientUtil {
// 设置请求头
config.getHeader().forEach(httpGet::addHeader);
HttpResponse response = httpClient.execute(httpGet);
try (InputStream inputStream = response.getEntity().getContent();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {