Commit 9832278f authored by zhangzhenbang's avatar zhangzhenbang

初始化

parent faf20d54
Pipeline #141 canceled with stages
...@@ -107,6 +107,11 @@ ...@@ -107,6 +107,11 @@
<version>1.2.12</version> <version>1.2.12</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.yingxin.beijingvehicleflow.config;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* 远程HTTP配置
*
* @author gaozhentao
* @version 1.0
* @date 2020/2/12
*/
@Configuration
public class RestConfig {
@Value("${restTemplate.connect-config.connectTimeout}")
private int connectTimeout;
@Value("${restTemplate.connect-config.readTimeout}")
private int readTimeout;
@Value("${restTemplate.connect-config.connectionRequestTimeout}")
private int connectionRequestTimeout;
@Value("${restTemplate.connect-pool.maxTotal}")
private int maxTotal;
@Value("${restTemplate.connect-pool.defaultMaxPerRoute}")
private int defaultMaxPerRoute;
@Value("${restTemplate.connect-pool.validateAfterInactivity}")
private int validateAfterInactivity;
/**
* 让spring管理RestTemplate,参数相关配置
*
* @param builder
* @return
*/
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = builder.build();
restTemplate.setRequestFactory(clientHttpRequestFactory());
return restTemplate;
}
/**
* 客户端请求链接策略
*
* @return
*/
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setHttpClient(httpClientBuilder().build());
clientHttpRequestFactory.setConnectTimeout(connectTimeout);
clientHttpRequestFactory.setReadTimeout(readTimeout);
clientHttpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeout);
return clientHttpRequestFactory;
}
/**
* 设置HTTP连接管理器,连接池相关配置管理
*
* @return 客户端链接管理器
*/
@Bean
public HttpClientBuilder httpClientBuilder() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(poolingConnectionManager());
return httpClientBuilder;
}
/**
* 链接线程池管理,可以keep-alive不断开链接请求,这样速度会更快 MaxTotal 连接池最大连接数 DefaultMaxPerRoute
* 每个主机的并发 ValidateAfterInactivity
* 可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间,如果超过,释放socket重新建立
*
* @return
*/
@Bean
public HttpClientConnectionManager poolingConnectionManager() {
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
poolingConnectionManager.setMaxTotal(maxTotal);
poolingConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
poolingConnectionManager.setValidateAfterInactivity(validateAfterInactivity);
return poolingConnectionManager;
}
}
package com.yingxin.beijingvehicleflow.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* description //TODO
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
@RestController
@CrossOrigin
@RequestMapping("/api/v1")
public class ReservationController {
}
package com.yingxin.beijingvehicleflow.entity;
import lombok.Data;
/**
* 数据库floating_population表对应实体类
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
@Data
public class FloatingPopulation {
private int id;
private String checkCoordinate;
private String checkTime;
private String submitCoordinate;
private String submitTime;
private String authCoordinate;
private String authTime;
private int workerId;
private int reservationId;
}
package com.yingxin.beijingvehicleflow.entity;
import lombok.Data;
/**
* 数据库identity_information表对应实体类
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
@Data
public class IdentityInformation {
private int id;
private String name;
private String idCardNumber;
private String phoneNumber;
private String faceImage;
private String wechatPhone;
private String wechat_openid;
private boolean isWorker;
}
package com.yingxin.beijingvehicleflow.entity;
import lombok.Data;
/**
* 数据库reservation表对应实体类
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
@Data
public class Reservation {
private int id;
private String carNumber;
private String outset;
private String destination;
private String communityContactName;
private String communityContactPhone;
private String submitCoordinate;
private String submitTime;
private String authCoordinate;
private String authTime;
private String verifyState;
private int identityId;
}
package com.yingxin.beijingvehicleflow.mapper;
/**
* description //TODO
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
public interface FloatingPopulationMapper {
}
package com.yingxin.beijingvehicleflow.mapper;
/**
* description //TODO
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
public interface IdentityInformationMapper {
}
package com.yingxin.beijingvehicleflow.mapper;
/**
* description //TODO
*
* @author 226
* @version 1.0
* @date 2020/2/23
*/
public interface ReservationMapper {
}
package com.yingxin.beijingvehicleflow.util;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 压缩图片工具类
* @auther 李晓阳
* @email lixy@yingxininfo.net
* @date 2020/2/15 10:28
*/
public class PicUtils {
private static Logger logger = LoggerFactory.getLogger(PicUtils.class);
/**
* compressImage
* @param image 图片的base64
* @param size 压缩的最大值
* 压缩图片
* @date 2020/2/15
* @return java.lang.String
*/
public static String compressImage(String image,long size){
try {
byte[] imageByte = new BASE64Decoder().decodeBuffer(image);
byte[] pics = PicUtils.compressPicForScale(imageByte, size, "x");
String authImage = new BASE64Encoder().encode(pics).trim();
String str = "\\s*|\t|\r|\n";
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(authImage);
authImage = m.replaceAll("");
authImage.replaceAll("\r|\n","");
authImage.replaceAll("\\n","");
return authImage;
} catch (IOException e) {
e.printStackTrace();
return image;
}
}
/**
* 根据指定大小压缩图片
*
* @param imageBytes 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @param imageId 影像编号
* @return 压缩质量后的图片字节数组
*/
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize, String imageId) {
if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
return imageBytes;
}
long srcSize = imageBytes.length;
double accuracy = getAccuracy(srcSize / 1024);
try {
while (imageBytes.length > desFileSize * 1024) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream)
.scale(accuracy)
.outputQuality(accuracy)
.toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
}
logger.info("【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb",
imageId, srcSize / 1024, imageBytes.length / 1024);
} catch (Exception e) {
logger.error("【图片压缩】msg=图片压缩失败!", e);
}
return imageBytes;
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
double accuracy;
if (size < 900) {
accuracy = 0.85;
} else if (size < 2047) {
accuracy = 0.6;
} else if (size < 3275) {
accuracy = 0.44;
} else {
accuracy = 0.4;
}
return accuracy;
}
/**
* 图片转化成base64字符串
* @param imgPath
* @return
*/
public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = imgPath;// 待处理的图片
InputStream in = null;
byte[] data = null;
String encode = null; // 返回Base64编码过的字节数组字符串
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
try {
// 读取图片字节数组
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
encode = encoder.encode(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return encode;
}
/**
* base64字符串转化成图片
*
* @param imgData
* 图片编码
* @param imgFilePath
* 存放到本地路径
* @return
* @throws IOException
*/
@SuppressWarnings("finally")
public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
if (imgData == null){ // 图像数据为空
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
OutputStream out = null;
try {
out = new FileOutputStream(imgFilePath);
// Base64解码
byte[] b = decoder.decodeBuffer(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
return true;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment