图片转换工具开发
This commit is contained in:
4
njcn-plugin/README.md
Normal file
4
njcn-plugin/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# 本模块用于收录各种插件工具
|
||||
## HttpClient模块
|
||||
|
||||
封装RestTemplate用于远程接口访问;
|
||||
36
njcn-plugin/RestTemplate-plugin/pom.xml
Normal file
36
njcn-plugin/RestTemplate-plugin/pom.xml
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.njcn</groupId>
|
||||
<artifactId>njcn-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>RestTemplate-plugin</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.njcn</groupId>
|
||||
<artifactId>njcn-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Apache HttpClient -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.njcn.http.config;
|
||||
|
||||
|
||||
import com.njcn.http.util.RestTemplateUtil;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
* @data 2025/6/17 11:15
|
||||
*/
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
// 创建连接池管理器
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
||||
// 设置最大连接数
|
||||
connectionManager.setMaxTotal(200);
|
||||
// 设置每个主机的最大连接数
|
||||
connectionManager.setDefaultMaxPerRoute(20);
|
||||
|
||||
// 创建HttpClient
|
||||
CloseableHttpClient httpClient = HttpClients.custom()
|
||||
.setConnectionManager(connectionManager)
|
||||
.build();
|
||||
|
||||
// 创建HttpComponentsClientHttpRequestFactory
|
||||
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
|
||||
// 设置连接超时时间(毫秒)
|
||||
factory.setConnectTimeout(5000);
|
||||
// 设置读取超时时间(毫秒)
|
||||
factory.setReadTimeout(5000);
|
||||
// 设置连接不够用的等待时间(毫秒)
|
||||
factory.setConnectionRequestTimeout(2000);
|
||||
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RestTemplateUtil restTemplateUtil(RestTemplate restTemplate) {
|
||||
return new RestTemplateUtil(restTemplate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
package com.njcn.http.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
* @data 2025/6/17 11:18
|
||||
*/
|
||||
@Slf4j
|
||||
public class RestTemplateUtil {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public RestTemplateUtil(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
*/
|
||||
public <T> T get(String url, Class<T> responseType) {
|
||||
return get(url, null, null, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求(带参数)
|
||||
*/
|
||||
public <T> T get(String url, Map<String, Object> params, Class<T> responseType) {
|
||||
return get(url, params, null, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求(带参数和请求头)
|
||||
*/
|
||||
public <T> T get(String url, Map<String, Object> params, HttpHeaders headers, Class<T> responseType) {
|
||||
try {
|
||||
// 构建URL(带参数)
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
||||
if (params != null) {
|
||||
params.forEach(builder::queryParam);
|
||||
}
|
||||
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
if (headers != null) {
|
||||
requestHeaders.addAll(headers);
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
|
||||
ResponseEntity<T> response = restTemplate.exchange(
|
||||
builder.build().encode().toUri(),
|
||||
HttpMethod.GET,
|
||||
requestEntity,
|
||||
responseType
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("GET请求异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("GET请求异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求(JSON数据)
|
||||
*/
|
||||
public <T> T postJson(String url, Object body, Class<T> responseType) {
|
||||
return postJson(url, body, null, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求(JSON数据,带请求头)
|
||||
*/
|
||||
public <T> T postJson(String url, Object body, HttpHeaders headers, Class<T> responseType) {
|
||||
try {
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
if (headers != null) {
|
||||
requestHeaders.addAll(headers);
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<?> requestEntity = new HttpEntity<>(body, requestHeaders);
|
||||
ResponseEntity<T> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
responseType
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("POST请求异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("POST请求异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求(表单数据)
|
||||
*/
|
||||
public <T> T postForm(String url, Map<String, Object> formData, Class<T> responseType) {
|
||||
return postForm(url, formData, null, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求(表单数据,带请求头)
|
||||
*/
|
||||
public <T> T postForm(String url, Map<String, Object> formData, HttpHeaders headers, Class<T> responseType) {
|
||||
try {
|
||||
// 构建表单数据
|
||||
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
|
||||
if (formData != null) {
|
||||
formData.forEach((key, value) -> requestBody.add(key, value));
|
||||
}
|
||||
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
if (headers != null) {
|
||||
requestHeaders.addAll(headers);
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeaders);
|
||||
ResponseEntity<T> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
responseType
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("POST表单请求异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("POST表单请求异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
*/
|
||||
public <T> T put(String url, Object body, Class<T> responseType) {
|
||||
return put(url, body, null, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求(带请求头)
|
||||
*/
|
||||
public <T> T put(String url, Object body, HttpHeaders headers, Class<T> responseType) {
|
||||
try {
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
if (headers != null) {
|
||||
requestHeaders.addAll(headers);
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<?> requestEntity = new HttpEntity<>(body, requestHeaders);
|
||||
ResponseEntity<T> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.PUT,
|
||||
requestEntity,
|
||||
responseType
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("PUT请求异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("PUT请求异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
*/
|
||||
public <T> T delete(String url, Class<T> responseType) {
|
||||
return delete(url, null, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求(带请求头)
|
||||
*/
|
||||
public <T> T delete(String url, HttpHeaders headers, Class<T> responseType) {
|
||||
try {
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
if (headers != null) {
|
||||
requestHeaders.addAll(headers);
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
|
||||
ResponseEntity<T> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.DELETE,
|
||||
requestEntity,
|
||||
responseType
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("DELETE请求异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("DELETE请求异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*/
|
||||
public <T> T uploadFile(String url, File file, String fileParamName, Map<String, Object> params, Class<T> responseType) {
|
||||
try {
|
||||
// 构建表单数据
|
||||
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
|
||||
requestBody.add(fileParamName, new FileSystemResource(file));
|
||||
if (params != null) {
|
||||
params.forEach(requestBody::add);
|
||||
}
|
||||
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeaders);
|
||||
ResponseEntity<T> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
responseType
|
||||
);
|
||||
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("文件上传异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*/
|
||||
public ResponseEntity<String> uploadFile(String url, File file) {
|
||||
try {
|
||||
// 构建表单数据
|
||||
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
|
||||
requestBody.add("file", new FileSystemResource(file));
|
||||
|
||||
// 构建请求头
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
|
||||
// 发送请求
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeaders);
|
||||
return restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
String.class
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("文件上传异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*/
|
||||
public byte[] downloadFile(String url) {
|
||||
try {
|
||||
ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
log.error("文件下载异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("文件下载异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.njcn.http.config.RestTemplateConfig
|
||||
23
njcn-plugin/pom.xml
Normal file
23
njcn-plugin/pom.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.njcn</groupId>
|
||||
<artifactId>BasicDependVersion</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>njcn-plugin</artifactId>
|
||||
<modules>
|
||||
<module>RestTemplate-plugin</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user