forked from github/dataease
Merge pull request #9714 from dataease/pr@dev-v2@feat_report_lark
feat(X-Pack): 定时报告-飞书报告
This commit is contained in:
commit
de6c3d4b91
@ -135,6 +135,11 @@
|
|||||||
<version>${itextpdf.version}</version>
|
<version>${itextpdf.version}</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.vladsch.flexmark</groupId>
|
||||||
|
<artifactId>flexmark-all</artifactId>
|
||||||
|
<version>${flexmark.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
2
de-xpack
2
de-xpack
@ -1 +1 @@
|
|||||||
Subproject commit 5934d4191552efad9f82d4710880ca2f2d3f4fcf
|
Subproject commit b550857633fc2ce85f895a2953661fe3954ecf06
|
1
pom.xml
1
pom.xml
@ -47,6 +47,7 @@
|
|||||||
<angus-mail.version>2.0.3</angus-mail.version>
|
<angus-mail.version>2.0.3</angus-mail.version>
|
||||||
<mysql-connector-j.version>8.2.0</mysql-connector-j.version>
|
<mysql-connector-j.version>8.2.0</mysql-connector-j.version>
|
||||||
<itextpdf.version>8.0.4</itextpdf.version>
|
<itextpdf.version>8.0.4</itextpdf.version>
|
||||||
|
<flexmark.version>0.62.2</flexmark.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -2,6 +2,10 @@ package io.dataease.api.communicate.api;
|
|||||||
|
|
||||||
import io.dataease.api.communicate.dto.MessageDTO;
|
import io.dataease.api.communicate.dto.MessageDTO;
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
@ -10,4 +14,7 @@ public interface CommunicateApi {
|
|||||||
|
|
||||||
@PostMapping("/send")
|
@PostMapping("/send")
|
||||||
void send(@RequestBody MessageDTO dto);
|
void send(@RequestBody MessageDTO dto);
|
||||||
|
|
||||||
|
@GetMapping("/down/{fileId}/{fileName}/{suffix}")
|
||||||
|
ResponseEntity<ByteArrayResource> down(@PathVariable("fileId") String fileId, @PathVariable("fileName") String fileName, @PathVariable("suffix") String suffix) throws Exception;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package io.dataease.api.lark.api;
|
|||||||
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
||||||
import io.dataease.api.lark.dto.LarkEnableEditor;
|
import io.dataease.api.lark.dto.LarkEnableEditor;
|
||||||
import io.dataease.api.lark.dto.LarkTokenRequest;
|
import io.dataease.api.lark.dto.LarkTokenRequest;
|
||||||
|
import io.dataease.api.lark.vo.LarkGroupVO;
|
||||||
import io.dataease.api.lark.vo.LarkInfoVO;
|
import io.dataease.api.lark.vo.LarkInfoVO;
|
||||||
import io.dataease.api.lark.dto.LarkSettingCreator;
|
import io.dataease.api.lark.dto.LarkSettingCreator;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
@ -38,4 +39,8 @@ public interface LarkApi {
|
|||||||
@Operation(summary = "飞书绑定", hidden = true)
|
@Operation(summary = "飞书绑定", hidden = true)
|
||||||
@PostMapping("/bind")
|
@PostMapping("/bind")
|
||||||
void bind(@RequestBody LarkTokenRequest request);
|
void bind(@RequestBody LarkTokenRequest request);
|
||||||
|
|
||||||
|
@Operation(summary = "获取飞书群组", hidden = true)
|
||||||
|
@GetMapping("/getGroup")
|
||||||
|
LarkGroupVO getGroup();
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package io.dataease.api.lark.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LarkGroupItem implements Serializable {
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = -3458959523154279946L;
|
||||||
|
|
||||||
|
private String chat_id;
|
||||||
|
private String name;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package io.dataease.api.lark.vo;
|
||||||
|
|
||||||
|
import io.dataease.api.lark.dto.LarkGroupItem;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LarkGroupVO implements Serializable {
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 39710350567348130L;
|
||||||
|
|
||||||
|
private boolean valid;
|
||||||
|
private List<LarkGroupItem> groupList;
|
||||||
|
}
|
@ -65,6 +65,11 @@
|
|||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
<version>${httpclient.version}</version>
|
<version>${httpclient.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpmime</artifactId>
|
||||||
|
<version>${httpclient.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -18,6 +18,7 @@ public class StaticResourceConstants {
|
|||||||
public static String MAP_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "map";
|
public static String MAP_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "map";
|
||||||
public static String CUSTOM_MAP_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "geo";
|
public static String CUSTOM_MAP_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "geo";
|
||||||
public static String APPEARANCE_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "appearance";
|
public static String APPEARANCE_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "appearance";
|
||||||
|
public static String REPORT_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "report";
|
||||||
|
|
||||||
public static String MAP_URL = "/map";
|
public static String MAP_URL = "/map";
|
||||||
public static String GEO_URL = "/geo";
|
public static String GEO_URL = "/geo";
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package io.dataease.utils;
|
package io.dataease.utils;
|
||||||
|
|
||||||
|
import io.dataease.exception.DEException;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@ -9,6 +10,8 @@ import java.nio.channels.FileChannel;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
|
|
||||||
@ -51,9 +54,10 @@ public class FileUtils {
|
|||||||
|
|
||||||
public static void validateExist(String path) {
|
public static void validateExist(String path) {
|
||||||
File dir = new File(path);
|
File dir = new File(path);
|
||||||
if (dir.exists()) return ;
|
if (dir.exists()) return;
|
||||||
dir.mkdirs();
|
dir.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将文件名解析成文件的上传路径
|
* 将文件名解析成文件的上传路径
|
||||||
*/
|
*/
|
||||||
@ -62,7 +66,7 @@ public class FileUtils {
|
|||||||
String suffix = getExtensionName(file.getOriginalFilename());
|
String suffix = getExtensionName(file.getOriginalFilename());
|
||||||
try {
|
try {
|
||||||
validateExist(filePath);
|
validateExist(filePath);
|
||||||
String fileName = name + "." + suffix;
|
String fileName = name + "." + suffix;
|
||||||
String path = filePath + fileName;
|
String path = filePath + fileName;
|
||||||
// getCanonicalFile 可解析正确各种路径
|
// getCanonicalFile 可解析正确各种路径
|
||||||
File dest = new File(path).getCanonicalFile();
|
File dest = new File(path).getCanonicalFile();
|
||||||
@ -78,45 +82,46 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
public static void copyFolder(String sourcePath,String targetPath) throws Exception{
|
|
||||||
|
public static void copyFolder(String sourcePath, String targetPath) throws Exception {
|
||||||
//源文件夹路径
|
//源文件夹路径
|
||||||
File sourceFile = new File(sourcePath);
|
File sourceFile = new File(sourcePath);
|
||||||
//目标文件夹路径
|
//目标文件夹路径
|
||||||
File targetFile = new File(targetPath);
|
File targetFile = new File(targetPath);
|
||||||
|
|
||||||
if(!sourceFile.exists()){
|
if (!sourceFile.exists()) {
|
||||||
throw new Exception("文件夹不存在");
|
throw new Exception("文件夹不存在");
|
||||||
}
|
}
|
||||||
if(!sourceFile.isDirectory()){
|
if (!sourceFile.isDirectory()) {
|
||||||
throw new Exception("源文件夹不是目录");
|
throw new Exception("源文件夹不是目录");
|
||||||
}
|
}
|
||||||
if(!targetFile.exists()){
|
if (!targetFile.exists()) {
|
||||||
targetFile.mkdirs();
|
targetFile.mkdirs();
|
||||||
}
|
}
|
||||||
if(!targetFile.isDirectory()){
|
if (!targetFile.isDirectory()) {
|
||||||
throw new Exception("目标文件夹不是目录");
|
throw new Exception("目标文件夹不是目录");
|
||||||
}
|
}
|
||||||
|
|
||||||
File[] files = sourceFile.listFiles();
|
File[] files = sourceFile.listFiles();
|
||||||
if(files == null || files.length == 0){
|
if (files == null || files.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(File file : files){
|
for (File file : files) {
|
||||||
//文件要移动的路径
|
//文件要移动的路径
|
||||||
String movePath = targetFile+File.separator+file.getName();
|
String movePath = targetFile + File.separator + file.getName();
|
||||||
if(file.isDirectory()){
|
if (file.isDirectory()) {
|
||||||
//如果是目录则递归调用
|
//如果是目录则递归调用
|
||||||
copyFolder(file.getAbsolutePath(),movePath);
|
copyFolder(file.getAbsolutePath(), movePath);
|
||||||
}else {
|
} else {
|
||||||
//如果是文件则复制文件
|
//如果是文件则复制文件
|
||||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
|
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
|
||||||
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(movePath));
|
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(movePath));
|
||||||
|
|
||||||
byte[] b = new byte[1024];
|
byte[] b = new byte[1024];
|
||||||
int temp = 0;
|
int temp = 0;
|
||||||
while((temp = in.read(b)) != -1){
|
while ((temp = in.read(b)) != -1) {
|
||||||
out.write(b,0,temp);
|
out.write(b, 0, temp);
|
||||||
}
|
}
|
||||||
out.close();
|
out.close();
|
||||||
in.close();
|
in.close();
|
||||||
@ -125,12 +130,12 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String copy(File source, String targetDir) throws IOException{
|
public static String copy(File source, String targetDir) throws IOException {
|
||||||
String name = source.getName();
|
String name = source.getName();
|
||||||
String destPath = null;
|
String destPath = null;
|
||||||
if (targetDir.endsWith("/") || targetDir.endsWith("\\")){
|
if (targetDir.endsWith("/") || targetDir.endsWith("\\")) {
|
||||||
destPath = targetDir + name;
|
destPath = targetDir + name;
|
||||||
}else{
|
} else {
|
||||||
destPath = targetDir + "/" + name;
|
destPath = targetDir + "/" + name;
|
||||||
}
|
}
|
||||||
File DestFile = new File(destPath);
|
File DestFile = new File(destPath);
|
||||||
@ -159,7 +164,7 @@ public class FileUtils {
|
|||||||
try {
|
try {
|
||||||
FileReader fileReader = new FileReader(file);
|
FileReader fileReader = new FileReader(file);
|
||||||
Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
|
Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
|
||||||
int ch=0;
|
int ch = 0;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
while ((ch = reader.read()) != -1) {
|
while ((ch = reader.read()) != -1) {
|
||||||
sb.append((char) ch);
|
sb.append((char) ch);
|
||||||
@ -176,11 +181,92 @@ public class FileUtils {
|
|||||||
|
|
||||||
public static void deleteFile(String path) {
|
public static void deleteFile(String path) {
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
if (file.exists()){
|
if (file.exists()) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
Arrays.stream(file.listFiles()).forEach(item -> deleteFile(item.getAbsolutePath()));
|
Arrays.stream(file.listFiles()).forEach(item -> deleteFile(item.getAbsolutePath()));
|
||||||
}
|
}
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean exist(String path) {
|
||||||
|
File file = new File(path);
|
||||||
|
return file.exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> listFileNames(String path) {
|
||||||
|
File file = new File(path);
|
||||||
|
if (!file.exists()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
|
||||||
|
assert files != null;
|
||||||
|
|
||||||
|
return Arrays.stream(files).map(File::getName).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSuffix(String fileName) {
|
||||||
|
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getPrefix(String fileName) {
|
||||||
|
return fileName.substring(0, fileName.lastIndexOf("."));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] readBytes(String path) {
|
||||||
|
File file = new File(path);
|
||||||
|
if (!file.exists() || !file.isFile()) {
|
||||||
|
DEException.throwException("文件不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] buffer = new byte[4096];
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
int bytesRead;
|
||||||
|
if ((bytesRead = fis.read(buffer)) == -1) {
|
||||||
|
bytes = bos.toByteArray();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bos.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
} catch (Throwable var9) {
|
||||||
|
try {
|
||||||
|
bos.close();
|
||||||
|
} catch (Throwable var8) {
|
||||||
|
var9.addSuppressed(var8);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw var9;
|
||||||
|
}
|
||||||
|
|
||||||
|
bos.close();
|
||||||
|
} catch (Throwable var10) {
|
||||||
|
try {
|
||||||
|
fis.close();
|
||||||
|
} catch (Throwable var7) {
|
||||||
|
var10.addSuppressed(var7);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw var10;
|
||||||
|
}
|
||||||
|
|
||||||
|
fis.close();
|
||||||
|
} catch (Exception var11) {
|
||||||
|
var11.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package io.dataease.utils;
|
package io.dataease.utils;
|
||||||
|
|
||||||
import io.dataease.exception.DEException;
|
import io.dataease.exception.DEException;
|
||||||
|
import org.apache.commons.collections4.MapUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||||
import org.apache.http.client.entity.EntityBuilder;
|
import org.apache.http.client.entity.EntityBuilder;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
@ -28,8 +30,12 @@ import org.apache.http.util.EntityUtils;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -48,7 +54,7 @@ public class HttpClientUtil {
|
|||||||
* @return CloseableHttpClient实例
|
* @return CloseableHttpClient实例
|
||||||
*/
|
*/
|
||||||
private static CloseableHttpClient buildHttpClient(String url) {
|
private static CloseableHttpClient buildHttpClient(String url) {
|
||||||
if(StringUtils.isEmpty(url)){
|
if (StringUtils.isEmpty(url)) {
|
||||||
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: url 不能为空!");
|
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: url 不能为空!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@ -70,6 +76,7 @@ public class HttpClientUtil {
|
|||||||
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get http请求
|
* Get http请求
|
||||||
*
|
*
|
||||||
@ -99,7 +106,7 @@ public class HttpClientUtil {
|
|||||||
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if(httpClient != null){
|
if (httpClient != null) {
|
||||||
httpClient.close();
|
httpClient.close();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -125,7 +132,7 @@ public class HttpClientUtil {
|
|||||||
httpPatch.setEntity(requestEntity);
|
httpPatch.setEntity(requestEntity);
|
||||||
HttpResponse response = httpClient.execute(httpPatch);
|
HttpResponse response = httpClient.execute(httpPatch);
|
||||||
return getResponseStr(response, config);
|
return getResponseStr(response, config);
|
||||||
}catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("HttpClient查询失败", e);
|
logger.error("HttpClient查询失败", e);
|
||||||
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
@ -171,7 +178,7 @@ public class HttpClientUtil {
|
|||||||
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
throw new DEException(SYSTEM_INNER_ERROR.code(), "HttpClient查询失败: " + e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if(httpClient != null){
|
if (httpClient != null) {
|
||||||
httpClient.close();
|
httpClient.close();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -231,14 +238,108 @@ public class HttpClientUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getResponseStr(HttpResponse response, HttpClientConfig config) throws Exception{
|
private static String getResponseStr(HttpResponse response, HttpClientConfig config) throws Exception {
|
||||||
if(response.getStatusLine().getStatusCode() >= 400){
|
if (response.getStatusLine().getStatusCode() >= 400) {
|
||||||
String msg = EntityUtils.toString(response.getEntity(), config.getCharset());
|
String msg = EntityUtils.toString(response.getEntity(), config.getCharset());
|
||||||
if(StringUtils.isEmpty(msg)){
|
if (StringUtils.isEmpty(msg)) {
|
||||||
msg = "StatusCode: " + response.getStatusLine().getStatusCode();
|
msg = "StatusCode: " + response.getStatusLine().getStatusCode();
|
||||||
}
|
}
|
||||||
throw new Exception(msg);
|
throw new Exception(msg);
|
||||||
}
|
}
|
||||||
return EntityUtils.toString(response.getEntity(), config.getCharset());
|
return EntityUtils.toString(response.getEntity(), config.getCharset());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] downloadBytes(String url) {
|
||||||
|
HttpClientConfig config = new HttpClientConfig();
|
||||||
|
return HttpClientUtil.downFromRemote(url, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] downFromRemote(String url, HttpClientConfig config) {
|
||||||
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
CloseableHttpClient httpClient = buildHttpClient(url);
|
||||||
|
|
||||||
|
try {
|
||||||
|
httpGet.setConfig(config.buildRequestConfig());
|
||||||
|
Map<String, String> header = config.getHeader();
|
||||||
|
Iterator var5 = header.keySet().iterator();
|
||||||
|
|
||||||
|
while (var5.hasNext()) {
|
||||||
|
String key = (String) var5.next();
|
||||||
|
httpGet.addHeader(key, (String) header.get(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
|
InputStream inputStream = response.getEntity().getContent();
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] var10 = outputStream.toByteArray();
|
||||||
|
return var10;
|
||||||
|
} catch (Exception var19) {
|
||||||
|
logger.error("HttpClient查询失败", var19);
|
||||||
|
throw new RuntimeException("HttpClient查询失败: " + var19.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
httpClient.close();
|
||||||
|
} catch (Exception var18) {
|
||||||
|
logger.error("HttpClient关闭连接失败", var18);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String postFile(String fileServer, byte[] bytes, String fileName, Map<String, String> param, HttpClientConfig config) {
|
||||||
|
CloseableHttpClient httpClient = buildHttpClient(fileServer);
|
||||||
|
HttpPost postRequest = new HttpPost(fileServer);
|
||||||
|
if (config == null) {
|
||||||
|
config = new HttpClientConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
postRequest.setConfig(config.buildRequestConfig());
|
||||||
|
Map<String, String> header = config.getHeader();
|
||||||
|
if (MapUtils.isNotEmpty(header)) {
|
||||||
|
Iterator var8 = header.keySet().iterator();
|
||||||
|
|
||||||
|
while (var8.hasNext()) {
|
||||||
|
String key = (String) var8.next();
|
||||||
|
postRequest.addHeader(key, (String) header.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||||
|
builder.setCharset(StandardCharsets.UTF_8);
|
||||||
|
builder.addBinaryBody("image", bytes, ContentType.DEFAULT_BINARY, fileName);
|
||||||
|
if (param != null) {
|
||||||
|
Iterator var13 = param.entrySet().iterator();
|
||||||
|
while (var13.hasNext()) {
|
||||||
|
Map.Entry<String, String> entry = (Map.Entry) var13.next();
|
||||||
|
builder.addTextBody((String) entry.getKey(), (String) entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
postRequest.setEntity((HttpEntity) builder.build());
|
||||||
|
return getResponseStr(httpClient.execute(postRequest), config);
|
||||||
|
} catch (Exception var11) {
|
||||||
|
logger.error("HttpClient查询失败", var11);
|
||||||
|
throw new RuntimeException("HttpClient查询失败: " + var11.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String upload(String url, byte[] bytes, String name, Map<String, String> paramMap, Map<String, Object> headMap) {
|
||||||
|
HttpClientConfig config = new HttpClientConfig();
|
||||||
|
addHead(config, headMap);
|
||||||
|
return HttpClientUtil.postFile(url, bytes, name, paramMap, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addHead(HttpClientConfig config, Map<String, Object> headMap) {
|
||||||
|
if (MapUtils.isEmpty(headMap)) return;
|
||||||
|
for (Map.Entry<String, Object> entry : headMap.entrySet()) {
|
||||||
|
config.addHeader(entry.getKey(), entry.getValue().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user