1
This commit is contained in:
@ -0,0 +1,560 @@
|
||||
package com.mosty.hczx.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.config.AuthSchemes;
|
||||
import org.apache.http.client.config.CookieSpecs;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.IOException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class HttpClientUtils {
|
||||
|
||||
|
||||
public static final String CHARSET = "UTF-8";
|
||||
private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000).setConnectionRequestTimeout(15000).build();
|
||||
|
||||
public static String doGet(String url, Map<String, String> params) {
|
||||
return doGet(url, params, CHARSET);
|
||||
}
|
||||
|
||||
public static String doGetSSL(String url, Map<String, String> params) {
|
||||
return doGetSSL(url, params, CHARSET);
|
||||
}
|
||||
|
||||
public static String doPost(String url, Map<String, String> params) {
|
||||
return doPost(url, params, CHARSET);
|
||||
}
|
||||
|
||||
public static String doPostForJson(String url, String json) {
|
||||
return doPostForJson(url, json, CHARSET);
|
||||
}
|
||||
|
||||
public static String doPostForJson(String url, String json, Map<String, String> headersMap) {
|
||||
return doPostForJsonHeaders(url, json, CHARSET, headersMap);
|
||||
}
|
||||
|
||||
public static String doPostSSLForJson(String url, String json) {
|
||||
return doPostSSLForJson(url, json, CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP GET
|
||||
*
|
||||
* @param url
|
||||
* 请求的url地址 ?之前的地址
|
||||
* @param params
|
||||
* 请求的参数
|
||||
* @param charset
|
||||
* 编码格式
|
||||
* @return 页面内容
|
||||
*/
|
||||
public static String doGet(String url, Map<String, String> params, String charset) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
// 将请求参数和url进行拼接
|
||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(REQUEST_CONFIG);
|
||||
|
||||
// 创建默认的httpClient实例.
|
||||
httpClient = HttpClients.createDefault();
|
||||
response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpGet.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Title: doGetSSL
|
||||
* @Description: get https
|
||||
* @param url
|
||||
* @param params
|
||||
* @param charset
|
||||
* @return String
|
||||
*/
|
||||
public static String doGetSSL(String url, Map<String, String> params, String charset) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
// 将请求参数和url进行拼接
|
||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(REQUEST_CONFIG);
|
||||
|
||||
// 创建默认的httpClient实例. 在调用SSL之前需要重写验证方法,取消检测SSL
|
||||
X509TrustManager trustManager = new X509TrustManager() {
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
}
|
||||
};
|
||||
SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
|
||||
ctx.init(null, new TrustManager[] { trustManager }, null);
|
||||
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
|
||||
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).setExpectContinueEnabled(Boolean.TRUE)
|
||||
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE)
|
||||
.register("https", socketFactory).build();
|
||||
// 创建ConnectionManager,添加Connection配置信息
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
|
||||
|
||||
httpGet.addHeader("AppKey", "679049566846189568");
|
||||
|
||||
response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpGet.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP POST
|
||||
*
|
||||
* @param url
|
||||
* 请求的url地址 ?之前的地址
|
||||
* @param params
|
||||
* 请求的参数
|
||||
* @param charset
|
||||
* 编码格式
|
||||
* @return 页面内容
|
||||
*/
|
||||
public static String doPost(String url, Map<String, String> params, String charset) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
//log.info("doPost url: " + url);
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
List<NameValuePair> pairs = null;
|
||||
if (params != null && !params.isEmpty()) {
|
||||
pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
//log.info("doPost pairs: " + JacksonUtils.objToJson(pairs));
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
if (pairs != null && pairs.size() > 0) {
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
||||
}
|
||||
httpPost.setConfig(REQUEST_CONFIG);
|
||||
// 创建默认的httpClient实例.
|
||||
httpClient = HttpClients.createDefault();
|
||||
response = httpClient.execute(httpPost);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK) {
|
||||
httpPost.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
log.info("doPost HttpEntity: " + result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP POST 参数为json
|
||||
*
|
||||
* @param url
|
||||
* @param json
|
||||
* @param charset
|
||||
* @return
|
||||
*/
|
||||
public static String doPostForJson(String url, String json, String charset) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
// log.info("doPostForJson url: " + url);
|
||||
// log.info("doPostForJson json: " + json);
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
StringEntity stringentity = new StringEntity(json, charset);
|
||||
stringentity.setContentType("application/json");
|
||||
|
||||
httpPost.setEntity(stringentity);
|
||||
httpPost.setConfig(REQUEST_CONFIG);
|
||||
// 创建默认的httpClient实例.
|
||||
httpClient = HttpClients.createDefault();
|
||||
// 执行请求
|
||||
response = httpClient.execute(httpPost);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK) {
|
||||
httpPost.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
// log.info("doPostForJson HttpEntity: " + result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP POST 参数为json
|
||||
*
|
||||
* @param url
|
||||
* @param json
|
||||
* @param charset
|
||||
* @return
|
||||
*/
|
||||
public static String doPostForJsonHeaders(String url, String json, String charset, Map<String, String> headersMap) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
log.info("doPostForJson url: " + url);
|
||||
//log.info("doPostForJson json: " + json);
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
StringEntity stringentity = new StringEntity(json, charset);
|
||||
stringentity.setContentType("application/json");
|
||||
|
||||
//请求头部
|
||||
if(null != headersMap && !headersMap.isEmpty()){
|
||||
Iterator<Map.Entry<String, String>> itr = headersMap.entrySet().iterator();
|
||||
while(itr.hasNext()){
|
||||
Map.Entry<String, String> ent = itr.next();
|
||||
httpPost.addHeader(ent.getKey(), ent.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
httpPost.setEntity(stringentity);
|
||||
httpPost.setConfig(REQUEST_CONFIG);
|
||||
// 创建默认的httpClient实例.
|
||||
httpClient = HttpClients.createDefault();
|
||||
// 执行请求
|
||||
response = httpClient.execute(httpPost);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK) {
|
||||
httpPost.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
log.info("doPostForJson HttpEntity: " + result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Title: doPostSSLForJson
|
||||
* @Description: post https
|
||||
* @param url
|
||||
* @param json
|
||||
* @param charset
|
||||
* @return String
|
||||
*/
|
||||
public static String doPostSSLForJson(String url, String json, String charset) {
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
StringEntity stringentity = new StringEntity(json, charset);
|
||||
stringentity.setContentType("application/json");
|
||||
|
||||
httpPost.setEntity(stringentity);
|
||||
httpPost.setConfig(REQUEST_CONFIG);
|
||||
|
||||
// 在调用SSL之前需要重写验证方法,取消检测SSL
|
||||
X509TrustManager trustManager = new X509TrustManager() {
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
}
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
}
|
||||
};
|
||||
SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
|
||||
ctx.init(null, new TrustManager[] { trustManager }, null);
|
||||
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
|
||||
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).setExpectContinueEnabled(Boolean.TRUE)
|
||||
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
|
||||
// 创建ConnectionManager,添加Connection配置信息
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
|
||||
|
||||
// 执行请求
|
||||
response = httpClient.execute(httpPost);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK) {
|
||||
httpPost.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* doGetForJson(json方式的get请求)
|
||||
* @param url
|
||||
* @param params
|
||||
* @param charset
|
||||
* @return
|
||||
*/
|
||||
public static String doGetForJson(String url, Map<String, String> params, String charset) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
// 将请求参数和url进行拼接
|
||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(REQUEST_CONFIG);
|
||||
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
|
||||
|
||||
// 创建默认的httpClient实例.
|
||||
httpClient = HttpClients.createDefault();
|
||||
response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpGet.abort();
|
||||
return "HttpClient, error status code : " + statusCode;
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("2".contains("1,2"));
|
||||
}
|
||||
|
||||
}
|
307
mosty-hczx/src/main/java/com/mosty/hczx/utils/ImageUtils.java
Normal file
307
mosty-hczx/src/main/java/com/mosty/hczx/utils/ImageUtils.java
Normal file
@ -0,0 +1,307 @@
|
||||
package com.mosty.hczx.utils;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import sun.misc.BASE64Decoder;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import javax.net.ssl.*;
|
||||
import java.awt.*;
|
||||
import java.awt.color.ColorSpace;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* 图片处理类<br>
|
||||
*
|
||||
* @author Esacpe
|
||||
* @version 2014-8-22
|
||||
*/
|
||||
|
||||
public class ImageUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 将图片缩放处理方法<br>
|
||||
*
|
||||
* @param source 源图片对象
|
||||
* @param targetW 转换后的宽度
|
||||
* @param targetH 转换后的高度
|
||||
* @param sameScale 是否等比例缩放
|
||||
* @return BufferedImage
|
||||
*/
|
||||
public static BufferedImage resize(BufferedImage source, int targetW,
|
||||
int targetH, boolean sameScale) { // targetW,targetH分别表示目标长和宽
|
||||
int type = source.getType();
|
||||
BufferedImage target = null;
|
||||
double sx = (double) targetW / source.getWidth();
|
||||
double sy = (double) targetH / source.getHeight();
|
||||
if (sameScale) { // 需要等比例缩放
|
||||
if (sx > sy) {
|
||||
sx = sy;
|
||||
targetW = (int) (sx * source.getWidth());
|
||||
} else {
|
||||
sy = sx;
|
||||
targetH = (int) (sy * source.getHeight());
|
||||
}
|
||||
}
|
||||
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
|
||||
ColorModel cm = source.getColorModel();
|
||||
WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
|
||||
targetH);
|
||||
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
|
||||
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
|
||||
} else {
|
||||
target = new BufferedImage(targetW, targetH, type);
|
||||
}
|
||||
Graphics2D g = target.createGraphics();
|
||||
// smoother than exlax:
|
||||
g.setRenderingHint(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY);
|
||||
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
|
||||
g.dispose();
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片放大与缩小<br>
|
||||
*
|
||||
* @param sourceByte 源图片的字节数组
|
||||
* @param width 转换后的宽度
|
||||
* @param height 转换后的高度
|
||||
* @param sameScale 是否等比例缩放
|
||||
* @return byte[]
|
||||
*/
|
||||
public static byte[] convertImageSize(byte[] sourceByte, int width,
|
||||
int height, boolean sameScale) throws Exception {
|
||||
byte[] returnValue = null;
|
||||
if (sourceByte != null && sourceByte.length > 0) {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(sourceByte);
|
||||
// BufferedImage srcImage = getReadImage(in);
|
||||
BufferedImage srcImage = null;
|
||||
try {
|
||||
srcImage = ImageIO.read(in); // RGB
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (srcImage != null) {
|
||||
BufferedImage srcImageTarget = resize(srcImage, width, height,
|
||||
sameScale);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
boolean flag = ImageIO.write(srcImageTarget, "JPEG", out);
|
||||
returnValue = out.toByteArray();
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片放大与缩小后另存<br>
|
||||
*
|
||||
* @param fromFileStr 来源文件名
|
||||
* @param saveToFileStr 目标文件名
|
||||
* @param width 转换后的宽度
|
||||
* @param height 转换后的高度
|
||||
* @param sameScale 是否等比例缩放
|
||||
*/
|
||||
public static void saveImageAsJpg(String fromFileStr, String saveToFileStr,
|
||||
int width, int height, boolean sameScale) throws Exception {
|
||||
String imgType = "JPEG";
|
||||
if (fromFileStr.toLowerCase().endsWith(".png")) {
|
||||
imgType = "PNG";
|
||||
}
|
||||
File saveFile = new File(saveToFileStr);
|
||||
File fromFile = new File(fromFileStr);
|
||||
BufferedImage srcImage = getReadImage(fromFile);
|
||||
if (srcImage != null) {
|
||||
if (width > 0 || height > 0) {
|
||||
srcImage = resize(srcImage, width, height, sameScale);
|
||||
}
|
||||
ImageIO.write(srcImage, imgType, saveFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件取得bufferedImage对象(自动识别RGB与CMYK)<br>
|
||||
*
|
||||
* @param file 来源文件名
|
||||
* @return BufferedImage
|
||||
*/
|
||||
private static BufferedImage getReadImage(File file) throws Exception {
|
||||
BufferedImage srcImage = null;
|
||||
ImageInputStream input = ImageIO.createImageInputStream(file); // 只能接收File或FileInputStream,ByteArrayInputStream无效
|
||||
Iterator readers = ImageIO.getImageReaders(input);
|
||||
if (readers == null || !readers.hasNext()) {
|
||||
throw new RuntimeException("1 No ImageReaders found");
|
||||
}
|
||||
ImageReader reader = (ImageReader) readers.next();
|
||||
reader.setInput(input);
|
||||
String format = reader.getFormatName();
|
||||
if ("JPEG".equalsIgnoreCase(format) || "JPG".equalsIgnoreCase(format)) {
|
||||
try {
|
||||
srcImage = ImageIO.read(file); // RGB
|
||||
} catch (Exception e) {
|
||||
Raster raster = reader.readRaster(0, null);// CMYK
|
||||
srcImage = createJPEG4(raster);
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
return srcImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* RGB彩色空间转换为CMYK<br>
|
||||
*
|
||||
* @param raster
|
||||
* @return BufferedImage
|
||||
*/
|
||||
private static BufferedImage createJPEG4(Raster raster) {
|
||||
int w = raster.getWidth();
|
||||
int h = raster.getHeight();
|
||||
byte[] rgb = new byte[w * h * 3]; // 彩色空间转换
|
||||
float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
|
||||
float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
|
||||
float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
|
||||
float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
|
||||
for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
|
||||
float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
|
||||
double val = y + 1.402 * (cr - 128) - k;
|
||||
val = (val - 128) * .65f + 128;
|
||||
rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
|
||||
: (byte) (val + 0.5);
|
||||
val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
|
||||
val = (val - 128) * .65f + 128;
|
||||
rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
|
||||
: (byte) (val + 0.5);
|
||||
val = y + 1.772 * (cb - 128) - k;
|
||||
val = (val - 128) * .65f + 128;
|
||||
rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
|
||||
: (byte) (val + 0.5);
|
||||
}
|
||||
raster = Raster.createInterleavedRaster(new DataBufferByte(rgb,
|
||||
rgb.length), w, h, w * 3, 3, new int[]{0, 1, 2}, null);
|
||||
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
|
||||
ColorModel cm = new ComponentColorModel(cs, false, true,
|
||||
Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
|
||||
return new BufferedImage(cm, (WritableRaster) raster, true, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 网络图片转成base64 https
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encodeImageToBase64Https(String url) throws Exception {
|
||||
String base64 = null;
|
||||
SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
|
||||
sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());
|
||||
URL urls = new URL(url);
|
||||
HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
|
||||
public boolean verify(String s, SSLSession sslsession) {
|
||||
System.out.println("WARNING: Hostname is not matched for cert.");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
|
||||
//之后任何Https协议网站皆能正常访问,同第一种情况
|
||||
HttpURLConnection conn = null;
|
||||
try {
|
||||
conn = (HttpURLConnection) urls.openConnection();
|
||||
//设置请求方式为"GET"
|
||||
conn.setRequestMethod("GET");
|
||||
//超时响应时间为5秒
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
if (conn.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM) {
|
||||
String location = conn.getHeaderField("Location");
|
||||
String s = encodeImageToBase64Https(location.trim());
|
||||
if (org.apache.commons.lang3.StringUtils.isNotBlank(s)) {
|
||||
return s;
|
||||
}
|
||||
|
||||
} else {
|
||||
//通过输入流获取图片数据
|
||||
InputStream inStream = conn.getInputStream();
|
||||
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
//创建一个Buffer字符串
|
||||
byte[] buffer = new byte[1024];
|
||||
//每次读取的字符串长度,如果为-1,代表全部读取完毕
|
||||
int len = 0;
|
||||
//使用一个输入流从buffer里把数据读取出来
|
||||
while ((len = inStream.read(buffer)) != -1) {
|
||||
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
|
||||
outStream.write(buffer, 0, len);
|
||||
}
|
||||
//关闭输入流
|
||||
inStream.close();
|
||||
byte[] data = outStream.toByteArray();
|
||||
//对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
base64 = encoder.encode(data).replaceAll("[\\s*\t\n\r]", "");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new Exception("解析失败!");
|
||||
}
|
||||
return base64;
|
||||
}
|
||||
|
||||
/**
|
||||
* 网络图片转成base64
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encodeImageToBase64(String url) throws Exception {
|
||||
//打开链接
|
||||
URL urls = new URL(url);
|
||||
HttpURLConnection conn = null;
|
||||
try {
|
||||
conn = (HttpURLConnection) urls.openConnection();
|
||||
//设置请求方式为"GET"
|
||||
conn.setRequestMethod("GET");
|
||||
//超时响应时间为5秒
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
//通过输入流获取图片数据
|
||||
InputStream inStream = conn.getInputStream();
|
||||
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
//创建一个Buffer字符串
|
||||
byte[] buffer = new byte[1024];
|
||||
//每次读取的字符串长度,如果为-1,代表全部读取完毕
|
||||
int len = 0;
|
||||
//使用一个输入流从buffer里把数据读取出来
|
||||
while ((len = inStream.read(buffer)) != -1) {
|
||||
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
|
||||
outStream.write(buffer, 0, len);
|
||||
}
|
||||
//关闭输入流
|
||||
inStream.close();
|
||||
byte[] data = outStream.toByteArray();
|
||||
//对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
String base64 = encoder.encode(data).replaceAll("[\\s*\t\n\r]", "");
|
||||
return base64;//返回Base64编码过的字节数组字符串
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new Exception("解析失败!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String argv[]) throws Exception {
|
||||
|
||||
}
|
||||
}
|
204
mosty-hczx/src/main/java/com/mosty/hczx/utils/JacksonUtils.java
Normal file
204
mosty-hczx/src/main/java/com/mosty/hczx/utils/JacksonUtils.java
Normal file
@ -0,0 +1,204 @@
|
||||
package com.mosty.hczx.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JacksonUtils {
|
||||
private final static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private JacksonUtils() {
|
||||
|
||||
}
|
||||
|
||||
public static ObjectMapper getInstance() {
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* javaBean、列表数组转换为json字符串
|
||||
*/
|
||||
public static String objToJson(Object obj) throws Exception {
|
||||
return objectMapper.writeValueAsString(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* javaBean、列表数组转换为json字符串,忽略空值
|
||||
*/
|
||||
public static String objToJsonIgnoreNull(Object obj) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
return mapper.writeValueAsString(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* json 转JavaBean
|
||||
*/
|
||||
|
||||
public static <T> T jsonToPojo(String jsonString, Class<T> clazz) throws Exception {
|
||||
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
return objectMapper.readValue(jsonString, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* json字符串转换为map
|
||||
*/
|
||||
public static <T> Map<String, Object> jsonToMap(String jsonString) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
return mapper.readValue(jsonString, Map.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* json字符串转换为map
|
||||
*/
|
||||
public static <T> Map<String, T> jsonToMap(String jsonString, Class<T> clazz) throws Exception {
|
||||
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) objectMapper.readValue(jsonString, new TypeReference<Map<String, T>>() {
|
||||
});
|
||||
Map<String, T> result = new HashMap<String, T>();
|
||||
for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
|
||||
result.put(entry.getKey(), mapToPojo(entry.getValue(), clazz));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 深度转换json成map
|
||||
*
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> jsonToMapDeeply(String json) throws Exception {
|
||||
return jsonToMapRecursion(json, objectMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把json解析成list,如果list内部的元素存在jsonString,继续解析
|
||||
*
|
||||
* @param json
|
||||
* @param mapper
|
||||
* 解析工具
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static List<Object> jsonToListRecursion(String json, ObjectMapper mapper) throws Exception {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Object> list = mapper.readValue(json, List.class);
|
||||
|
||||
for (Object obj : list) {
|
||||
if (obj != null && obj instanceof String) {
|
||||
String str = (String) obj;
|
||||
if (str.startsWith("[")) {
|
||||
obj = jsonToListRecursion(str, mapper);
|
||||
} else if (obj.toString().startsWith("{")) {
|
||||
obj = jsonToMapRecursion(str, mapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把json解析成map,如果map内部的value存在jsonString,继续解析
|
||||
*
|
||||
* @param json
|
||||
* @param mapper
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static Map<String, Object> jsonToMapRecursion(String json, ObjectMapper mapper) throws Exception {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, Object> map = mapper.readValue(json, Map.class);
|
||||
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
Object obj = entry.getValue();
|
||||
if (obj != null && obj instanceof String) {
|
||||
String str = ((String) obj);
|
||||
|
||||
if (str.startsWith("[")) {
|
||||
List<?> list = jsonToListRecursion(str, mapper);
|
||||
map.put(entry.getKey(), list);
|
||||
} else if (str.startsWith("{")) {
|
||||
Map<String, Object> mapRecursion = jsonToMapRecursion(str, mapper);
|
||||
map.put(entry.getKey(), mapRecursion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 与javaBean json数组字符串转换为列表
|
||||
*/
|
||||
public static <T> List<T> jsonToList(String jsonArrayStr, Class<T> clazz) throws Exception {
|
||||
JavaType javaType = getCollectionType(ArrayList.class, clazz);
|
||||
List<T> lst = (List<T>) objectMapper.readValue(jsonArrayStr, javaType);
|
||||
return lst;
|
||||
}
|
||||
|
||||
/**
|
||||
* 深度转换json成列表
|
||||
*/
|
||||
public static List<Object> jsonToListDeeply(String jsonArrayStr) throws Exception {
|
||||
return jsonToListRecursion(jsonArrayStr, objectMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取泛型的Collection Type
|
||||
*
|
||||
* @param collectionClass
|
||||
* 泛型的Collection
|
||||
* @param elementClasses
|
||||
* 元素类
|
||||
* @return JavaType Java类型
|
||||
* @since 1.0
|
||||
*/
|
||||
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
|
||||
return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* map 转JavaBean
|
||||
*/
|
||||
public static <T> T mapToPojo(Map map, Class<T> clazz) {
|
||||
return objectMapper.convertValue(map, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* map 转json
|
||||
*
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static String mapToJson(Map map) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(map);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* map 转JavaBean
|
||||
*/
|
||||
public static <T> T objToPojo(Object obj, Class<T> clazz) {
|
||||
return objectMapper.convertValue(obj, clazz);
|
||||
}
|
||||
|
||||
}
|
87
mosty-hczx/src/main/java/com/mosty/hczx/utils/MD5Utils.java
Normal file
87
mosty-hczx/src/main/java/com/mosty/hczx/utils/MD5Utils.java
Normal file
@ -0,0 +1,87 @@
|
||||
package com.mosty.hczx.utils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class MD5Utils {
|
||||
private static MessageDigest md5 = null;
|
||||
static {
|
||||
try {
|
||||
md5 = MessageDigest.getInstance("MD5");
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于获取一个String的md5值
|
||||
*
|
||||
* @param string
|
||||
* @return
|
||||
*/
|
||||
public static String getMd5(String str) {
|
||||
byte[] bs = md5.digest(str.getBytes());
|
||||
StringBuilder sb = new StringBuilder(40);
|
||||
for (byte x : bs) {
|
||||
if ((x & 0xff) >> 4 == 0) {
|
||||
sb.append("0").append(Integer.toHexString(x & 0xff));
|
||||
} else {
|
||||
sb.append(Integer.toHexString(x & 0xff));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getMd5("abc@123"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过网站域名URL获取该网站的源码
|
||||
*
|
||||
* @param url
|
||||
* @return String
|
||||
* @throws Exception
|
||||
*/
|
||||
public static InputStream getURLinStream(String pathUrl) throws Exception {
|
||||
URL url = new URL(pathUrl);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
return conn.getInputStream(); // 通过输入流获取html二进制数据
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取md5
|
||||
*
|
||||
* @param path
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getUrlMd5(String url) throws Exception {
|
||||
try {
|
||||
InputStream fis = getURLinStream(url);
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] buffer = new byte[1024];
|
||||
int length = -1;
|
||||
while ((length = fis.read(buffer, 0, 1024)) != -1) {
|
||||
md.update(buffer, 0, length);
|
||||
}
|
||||
BigInteger bigInt = new BigInteger(1, md.digest());
|
||||
return bigInt.toString(16);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.mosty.hczx.utils;
|
||||
|
||||
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
public class MyX509TrustManager implements X509TrustManager {
|
||||
|
||||
// 检查客户端证书
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
|
||||
// 检查服务器端证书
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
|
||||
// 返回受信任的X509证书数组
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package com.mosty.hczx.utils;
|
||||
|
||||
import net.sourceforge.pinyin4j.PinyinHelper;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Created by 张海军 on 2022/4/12 10:52
|
||||
*
|
||||
* @ClassName PinYinUtil
|
||||
*/
|
||||
public class PinYinUtil {
|
||||
|
||||
|
||||
private PinYinUtil() {}
|
||||
|
||||
/**
|
||||
* 获取中文拼音首字母,其他字符不变
|
||||
*
|
||||
* @param str
|
||||
* @return String
|
||||
*/
|
||||
public static String getShortPinyin(String str) {
|
||||
return getShortPinyin(str, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中文拼音首字母
|
||||
* @param str
|
||||
* @param retain 为true保留其他字符
|
||||
* @return String
|
||||
*/
|
||||
public static String getShortPinyin(String str, boolean retain) {
|
||||
return getPinyin(str, true, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中文拼音,其他字符不变
|
||||
*
|
||||
* @param str
|
||||
* @return String
|
||||
*/
|
||||
public static String getFullPinyin(String str) {
|
||||
return getFullPinyin(str, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中文拼音
|
||||
* @param str
|
||||
* @param retain 为true保留其他字符
|
||||
* @return String
|
||||
*/
|
||||
public static String getFullPinyin(String str, boolean retain) {
|
||||
return getPinyin(str, false, retain);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中文拼音
|
||||
*
|
||||
* @param str
|
||||
* @param shortPinyin 为true获取中文拼音首字母
|
||||
* @param retain 为true保留其他字符
|
||||
* @return String
|
||||
*/
|
||||
public static String getPinyin(String str, boolean shortPinyin, boolean retain) {
|
||||
if (StringUtils.isBlank(str)) {
|
||||
return "";
|
||||
}
|
||||
StringBuffer pinyinBuffer = new StringBuffer();
|
||||
char[] arr = str.toCharArray();
|
||||
for (char c : arr) {
|
||||
String[] temp = PinyinHelper.toHanyuPinyinStringArray(c);
|
||||
if (ArrayUtils.isNotEmpty(temp)) {
|
||||
if (StringUtils.isNotBlank(temp[0])) {
|
||||
if (shortPinyin) {
|
||||
pinyinBuffer.append(temp[0].charAt(0));
|
||||
} else {
|
||||
pinyinBuffer.append(temp[0].replaceAll("\\d", ""));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (retain) {
|
||||
pinyinBuffer.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return pinyinBuffer.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
1105
mosty-hczx/src/main/java/com/mosty/hczx/utils/StringUtils.java
Normal file
1105
mosty-hczx/src/main/java/com/mosty/hczx/utils/StringUtils.java
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user