main
parent
793afa3e19
commit
e4f537a87d
|
@ -0,0 +1,103 @@
|
||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>rtos-wh</artifactId>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>utils</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
<version>2.7.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<version>2.7.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.itextpdf</groupId>
|
||||||
|
<artifactId>html2pdf</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 中文字体支持 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.itextpdf</groupId>
|
||||||
|
<artifactId>font-asian</artifactId>
|
||||||
|
<version>7.1.13</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.26</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>models</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- excel依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>easyexcel</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- easyexcel 依赖 必须,不然报错,导出不了excel -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cglib</groupId>
|
||||||
|
<artifactId>cglib</artifactId>
|
||||||
|
<version>3.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.minio</groupId>
|
||||||
|
<artifactId>minio</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.7</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bestvike</groupId>
|
||||||
|
<artifactId>linq</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>top.jfunc.json</groupId>
|
||||||
|
<artifactId>Json-Gson</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>2.14.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>2.14.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跨域配置:重写WebMvcConfigurer即可
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class CorsConfig implements WebMvcConfigurer {
|
||||||
|
/**
|
||||||
|
* 跨域支持方法
|
||||||
|
* @param registry 开启跨域注册
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(CorsRegistry registry){
|
||||||
|
//addMapping 添加可跨域的请求地址
|
||||||
|
registry.addMapping("/**")
|
||||||
|
//设置跨域 域名权限 规定由某一个指定的域名+端口能访问跨域项目
|
||||||
|
.allowedOrigins("*")
|
||||||
|
//.allowedOrigins("http://localhost:8080")
|
||||||
|
//是否开启cookie跨域
|
||||||
|
.allowCredentials(false)
|
||||||
|
//规定能够跨域访问的方法类型
|
||||||
|
.allowedMethods("GET","POST","DELETE","PUT","OPTIONS")
|
||||||
|
//添加验证请求头信息 token
|
||||||
|
.allowedHeaders("*")
|
||||||
|
//预请求存活时间,在此期间不在发送预请求
|
||||||
|
.maxAge(3600);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求拦截:将请求路径映射到安全的目标路径
|
||||||
|
* @param registry 注册实例
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
////addResourceHandler:请求路径
|
||||||
|
////addResourceLocations:映射路径
|
||||||
|
//registry.addResourceHandler("/**")
|
||||||
|
// .addResourceLocations("classpath:/static/");
|
||||||
|
//registry.addResourceHandler("swagger-ui.html")
|
||||||
|
// .addResourceLocations("classpath:/META-INF/resources/");
|
||||||
|
//registry.addResourceHandler("doc.html")
|
||||||
|
// .addResourceLocations("classpath:/META-INF/resources/");
|
||||||
|
//registry.addResourceHandler("/webjars/**")
|
||||||
|
// .addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import java.text.ParsePosition;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class DateTimeHelper {
|
||||||
|
/**
|
||||||
|
* 字符串转日期时间
|
||||||
|
* @param my_date_time my_date_time
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static Date DateStrToDateTime(String my_date_time){
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
ParsePosition pos = new ParsePosition(0);
|
||||||
|
return sdf.parse(my_date_time,pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串转日期
|
||||||
|
* @param my_date my_date
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static Date DateStrToDate(String my_date){
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
ParsePosition pos = new ParsePosition(0);
|
||||||
|
return sdf.parse(my_date,pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串转时间
|
||||||
|
* @param my_date my_date
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static Date DateStrToTime(String my_date){
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
|
||||||
|
ParsePosition pos = new ParsePosition(0);
|
||||||
|
return sdf.parse(my_date,pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串转LoaclDateTime
|
||||||
|
* @param my_date:日期时间字符串
|
||||||
|
* @return 日期时间对象
|
||||||
|
*/
|
||||||
|
public static LocalDateTime DateTimeStrToLocalDateTime(String my_date){
|
||||||
|
my_date=my_date.replace(" ","T");
|
||||||
|
return LocalDateTime.parse(my_date);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串转LocalDate
|
||||||
|
* @param my_date my_date
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static LocalDate DateStrToLocalDate(String my_date){
|
||||||
|
return LocalDate.parse(my_date);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串转LocalTime
|
||||||
|
* @param my_date my_date
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static LocalTime DateStrToLocalTime(String my_date){
|
||||||
|
return LocalTime.parse(my_date);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.ProtocolException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class FileDownloadUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从服务器获得一个输入流
|
||||||
|
* @param urlPath 路径
|
||||||
|
* @return 返回输入流
|
||||||
|
*/
|
||||||
|
public static InputStream getInputStream(String urlPath) {
|
||||||
|
InputStream inputStream = null;
|
||||||
|
HttpURLConnection httpURLConnection = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL(urlPath);
|
||||||
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
||||||
|
// 设置网络连接超时时间
|
||||||
|
httpURLConnection.setConnectTimeout(3000);
|
||||||
|
// 设置应用程序要从网络连接读取数据
|
||||||
|
httpURLConnection.setDoInput(true);
|
||||||
|
httpURLConnection.setRequestMethod("GET");
|
||||||
|
int responseCode = httpURLConnection.getResponseCode();
|
||||||
|
System.out.println("responseCode is:" + responseCode);
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
// 从服务器返回一个输入流
|
||||||
|
inputStream = httpURLConnection.getInputStream();
|
||||||
|
} else {
|
||||||
|
inputStream = httpURLConnection.getErrorStream();
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return inputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param resp resp
|
||||||
|
* @param inputStream inputStream
|
||||||
|
* @description: 将输入流输出到页面
|
||||||
|
*/
|
||||||
|
public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
|
||||||
|
OutputStream out = null;
|
||||||
|
try {
|
||||||
|
out = resp.getOutputStream();
|
||||||
|
int len = 0;
|
||||||
|
byte[] b = new byte[4096];
|
||||||
|
while ((len = inputStream.read(b)) != -1) {
|
||||||
|
out.write(b, 0, len);
|
||||||
|
}
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
//inputStream.close();
|
||||||
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得输入流
|
||||||
|
* @param filePath 完整的文夹路径名
|
||||||
|
* @return 返回输入流
|
||||||
|
*/
|
||||||
|
public static InputStream fileToInputStream(String filePath) {
|
||||||
|
File file = new File(filePath);
|
||||||
|
FileInputStream fileInputStream = null;
|
||||||
|
try {
|
||||||
|
fileInputStream = new FileInputStream(file);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return fileInputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,352 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import java.text.ParsePosition;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class FormatDateTime {
|
||||||
|
/**
|
||||||
|
* 日期加一天
|
||||||
|
*
|
||||||
|
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static Date getDateAddOne(Date myDate) {
|
||||||
|
Date currentTime = myDate;
|
||||||
|
Calendar calendar = new GregorianCalendar();
|
||||||
|
calendar.setTime(currentTime);
|
||||||
|
calendar.add(Calendar.DATE, 1);
|
||||||
|
currentTime = calendar.getTime();
|
||||||
|
return currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取今天文件夹日期
|
||||||
|
*
|
||||||
|
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static String getTodayStr() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
||||||
|
String dateString = formatter.format(currentTime);
|
||||||
|
return dateString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取昨天文件夹日期
|
||||||
|
*
|
||||||
|
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static String getYesTodayStr() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
Calendar calendar = new GregorianCalendar();
|
||||||
|
calendar.setTime(currentTime);
|
||||||
|
calendar.add(Calendar.DATE, -1);
|
||||||
|
currentTime = calendar.getTime();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
||||||
|
String dateString = formatter.format(currentTime);
|
||||||
|
return dateString;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取现在时间
|
||||||
|
*
|
||||||
|
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static Date getNowDate() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
String dateString = formatter.format(currentTime);
|
||||||
|
ParsePosition pos = new ParsePosition(8);
|
||||||
|
return formatter.parse(dateString, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取现在时间
|
||||||
|
*
|
||||||
|
* @return 返回字符串格式 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static String getStringDate() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return formatter.format(currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取现在时间
|
||||||
|
*
|
||||||
|
* @return 返回短时间字符串格式yyyy-MM-dd
|
||||||
|
*/
|
||||||
|
public static String getStringDateShort() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
return formatter.format(currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取时间 小时:分;秒 HH:mm:ss
|
||||||
|
*
|
||||||
|
* @return 返回字符串格式 HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static String getTimeShort() {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||||
|
Date currentTime = new Date();
|
||||||
|
return formatter.format(currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
|
||||||
|
*
|
||||||
|
* @param strDateTime strDateTime
|
||||||
|
* @return 长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static Date strToDateTime(String strDateTime) {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
ParsePosition pos = new ParsePosition(0);
|
||||||
|
return formatter.parse(strDateTime, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将换长时间格式时间转为字符串 yyyy-MM-dd HH:mm:ss
|
||||||
|
*
|
||||||
|
* @param dateDate dateDate
|
||||||
|
* @return 换长时间格式时间转为字符串 yyyy-MM-dd HH:mm:ss
|
||||||
|
*/
|
||||||
|
public static String dateTimeToStr(Date dateDate) {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return formatter.format(dateDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将短时间格式时间转换为字符串 yyyy-MM-dd
|
||||||
|
*
|
||||||
|
* @param dateDate dateDate
|
||||||
|
* @return 短时间格式时间转换为字符串 yyyy-MM-dd
|
||||||
|
*/
|
||||||
|
public static String dateToStr(Date dateDate) {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
return formatter.format(dateDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将短时间格式字符串转换为时间 yyyy-MM-dd
|
||||||
|
*
|
||||||
|
* @param strDate strDate
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static Date strToDate(String strDate) {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
ParsePosition pos = new ParsePosition(0);
|
||||||
|
return formatter.parse(strDate, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到现在时间
|
||||||
|
*
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static Date getNow() {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提取一个月中的最后一天
|
||||||
|
*
|
||||||
|
* @param day day
|
||||||
|
* @return 一个月中的最后一天
|
||||||
|
*/
|
||||||
|
public static Date getLastDate(long day) {
|
||||||
|
Date date = new Date();
|
||||||
|
long date_3_hm = date.getTime() - 3600000 * 34 * day;
|
||||||
|
return new Date(date_3_hm);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到现在时间
|
||||||
|
*
|
||||||
|
* @return 字符串 yyyyMMdd HHmmss
|
||||||
|
*/
|
||||||
|
public static String getStringToday() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
|
||||||
|
return formatter.format(currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到现在小时
|
||||||
|
*/
|
||||||
|
public static String getHour() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
String dateString = formatter.format(currentTime);
|
||||||
|
String hour;
|
||||||
|
hour = dateString.substring(11, 13);
|
||||||
|
return hour;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到现在分钟
|
||||||
|
*
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static String getTime() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
String dateString = formatter.format(currentTime);
|
||||||
|
String min;
|
||||||
|
min = dateString.substring(14, 16);
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
|
||||||
|
*
|
||||||
|
* @param sformat yyyyMMddhhmmss
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
public static String getUserDate(String sformat) {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat(sformat);
|
||||||
|
return formatter.format(currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date getDateFromLocalDate(LocalDate localDate) {
|
||||||
|
ZoneId zone = ZoneId.systemDefault();
|
||||||
|
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
|
||||||
|
return Date.from(instant);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取两个时间字符串的时间差
|
||||||
|
*
|
||||||
|
* @param startTimeStr 开始时间
|
||||||
|
* @param endTimeStr 结束时间
|
||||||
|
* @return 时间差 (秒数)
|
||||||
|
*/
|
||||||
|
public static long getTimeCha(String startTimeStr, String endTimeStr) {
|
||||||
|
if (startTimeStr == null || startTimeStr.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (endTimeStr == null || endTimeStr.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
long diffInMilliseconds = 0;
|
||||||
|
//long diffInMinutes = 0;
|
||||||
|
//long diffInHour = 0;
|
||||||
|
try {
|
||||||
|
Date startTime = format.parse(startTimeStr);
|
||||||
|
Date endTime = format.parse(endTimeStr);
|
||||||
|
diffInMilliseconds = endTime.getTime() - startTime.getTime();
|
||||||
|
//diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
|
||||||
|
//diffInHour=TimeUnit.MILLISECONDS.toHours(diffInMilliseconds);
|
||||||
|
System.out.println("Time difference in minutes: " + diffInMilliseconds);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return diffInMilliseconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将日期字符串转化为星期
|
||||||
|
* @param dateString 日期字符串
|
||||||
|
* @return 星期
|
||||||
|
*/
|
||||||
|
public static String getWeekDayStringByDateString(String dateString) {
|
||||||
|
if (dateString == null || dateString.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String weekDayString = "";
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
try {
|
||||||
|
Date date = dateFormat.parse(dateString);
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
|
||||||
|
switch (weekDay) {
|
||||||
|
case Calendar.SUNDAY:
|
||||||
|
weekDayString = "星期日";
|
||||||
|
break;
|
||||||
|
case Calendar.MONDAY:
|
||||||
|
weekDayString = "星期一";
|
||||||
|
break;
|
||||||
|
case Calendar.TUESDAY:
|
||||||
|
weekDayString = "星期二";
|
||||||
|
break;
|
||||||
|
case Calendar.WEDNESDAY:
|
||||||
|
weekDayString = "星期三";
|
||||||
|
break;
|
||||||
|
case Calendar.THURSDAY:
|
||||||
|
weekDayString = "星期四";
|
||||||
|
break;
|
||||||
|
case Calendar.FRIDAY:
|
||||||
|
weekDayString = "星期五";
|
||||||
|
break;
|
||||||
|
case Calendar.SATURDAY:
|
||||||
|
weekDayString = "星期六";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return weekDayString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将日期时间字符串转化为星期
|
||||||
|
* @param dateTimeString 日期时间字符串
|
||||||
|
* @return 星期
|
||||||
|
*/
|
||||||
|
public static String getWeekDayStringByDateTimeString(String dateTimeString) {
|
||||||
|
if (dateTimeString == null || dateTimeString.isEmpty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
//String dateTimeString = "2023-10-18 09:13:40";
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
String weekDayString = "";
|
||||||
|
try {
|
||||||
|
Date dateTime = dateFormat.parse(dateTimeString);
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(dateTime);
|
||||||
|
int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
|
||||||
|
switch (weekDay) {
|
||||||
|
case Calendar.SUNDAY:
|
||||||
|
weekDayString = "星期日";
|
||||||
|
break;
|
||||||
|
case Calendar.MONDAY:
|
||||||
|
weekDayString = "星期一";
|
||||||
|
break;
|
||||||
|
case Calendar.TUESDAY:
|
||||||
|
weekDayString = "星期二";
|
||||||
|
break;
|
||||||
|
case Calendar.WEDNESDAY:
|
||||||
|
weekDayString = "星期三";
|
||||||
|
break;
|
||||||
|
case Calendar.THURSDAY:
|
||||||
|
weekDayString = "星期四";
|
||||||
|
break;
|
||||||
|
case Calendar.FRIDAY:
|
||||||
|
weekDayString = "星期五";
|
||||||
|
break;
|
||||||
|
case Calendar.SATURDAY:
|
||||||
|
weekDayString = "星期六";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
System.out.println(weekDayString);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return weekDayString;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,103 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class GetDateTime {
|
||||||
|
/**
|
||||||
|
* 生成当前日期
|
||||||
|
*
|
||||||
|
* @return 当前日期
|
||||||
|
*/
|
||||||
|
public static Date nowDate() {
|
||||||
|
LocalDateTime localDateTime = LocalDateTime.now();
|
||||||
|
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 原始日期时间型格式转换为标准字符串型(yyyy-MM-dd HH:mm:ss)
|
||||||
|
*
|
||||||
|
* @param dateVal 原始日期时间
|
||||||
|
* @return 标准字符串型数据
|
||||||
|
*/
|
||||||
|
public static String getDateTimeToString(Date dateVal) {
|
||||||
|
//创建SimpleDateFormat对象,指定样式,如:2022-03-23 22:56:30
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
//格式转换
|
||||||
|
|
||||||
|
return sdf.format(dateVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示本系统当前时区
|
||||||
|
* @param args args
|
||||||
|
*/
|
||||||
|
public static void getSystemTimeArea(String[] args) {
|
||||||
|
Calendar ca = Calendar.getInstance();
|
||||||
|
TimeZone tz = ca.getTimeZone();
|
||||||
|
System.out.println(tz.getID()); //现在显示为:GMT+08:00,为中国时间(东八区)时间,所以,数据库连接的时区需要修改为:serverTimezone=Asia/Shanghai或者serverTimezone=GMT%2B8
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将日期转换为星期
|
||||||
|
*/
|
||||||
|
public static String dateToWeek(String datetime) throws ParseException {
|
||||||
|
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
|
||||||
|
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(datetime);
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(date);
|
||||||
|
int w = Math.max(cal.get(Calendar.DAY_OF_WEEK) - 1, 0);
|
||||||
|
return weekDays[w];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期加减天数获得新日期
|
||||||
|
* @date 原始日期
|
||||||
|
* @n 增减天数,减为负数 */
|
||||||
|
public static Date getNewDate(Date date,int n){
|
||||||
|
//定义日期时间格式
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
Calendar calstart = Calendar.getInstance();
|
||||||
|
|
||||||
|
calstart.setTime(date);//放入所要修改的日期
|
||||||
|
|
||||||
|
calstart.add(Calendar.DAY_OF_WEEK, n);//进行加减运算
|
||||||
|
|
||||||
|
System.out.println(sdf.format(calstart.getTime()));
|
||||||
|
|
||||||
|
return calstart.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算两个日期相差的天数
|
||||||
|
* @param date1 较小的日期
|
||||||
|
* @param date2 较大的日期
|
||||||
|
*/
|
||||||
|
public static Integer getDaysForTwoDate(Date date1, Date date2){
|
||||||
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
int x =0;
|
||||||
|
try {
|
||||||
|
Date star = df.parse(df.format(date1));//开始时间
|
||||||
|
Date endDay = df.parse(df.format(date2));//结束时间
|
||||||
|
Long starTime=star.getTime();
|
||||||
|
Long endTime=endDay.getTime();
|
||||||
|
Long num=endTime-starTime;//时间戳相差的毫秒数
|
||||||
|
//计算为天数
|
||||||
|
x =(int)(num/(24*60*60*1000));
|
||||||
|
System.out.println("相差天数为:"+ x);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,183 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import com.itextpdf.html2pdf.ConverterProperties;
|
||||||
|
import com.itextpdf.html2pdf.HtmlConverter;
|
||||||
|
import com.itextpdf.io.source.ByteArrayOutputStream;
|
||||||
|
import com.itextpdf.kernel.events.PdfDocumentEvent;
|
||||||
|
import com.itextpdf.kernel.font.PdfFont;
|
||||||
|
|
||||||
|
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||||
|
import com.itextpdf.kernel.geom.PageSize;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||||
|
import com.itextpdf.layout.font.FontProvider;
|
||||||
|
//import io.micrometer.common.util.StringUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Itext7转换工具类
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class HtmlToPdfUtils {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* html文件转化为 pdf文件
|
||||||
|
* @param htmlFile html文件所在相对路径 如 "C:/Users/lenovo/Desktop/teee/myTest.html";
|
||||||
|
* @param pdfFile pdf文件存储相对路径 如 "C:/Users/lenovo/Desktop/teee/x6.pdf";
|
||||||
|
* @param waterMarkText 自定义水印
|
||||||
|
*/
|
||||||
|
public static void htmlToPdf(String htmlFile,String pdfFile,String waterMarkText) {
|
||||||
|
if (htmlFile.isEmpty()||pdfFile.isEmpty())
|
||||||
|
{
|
||||||
|
log.error("错误信息:{html文件所在相对路径不能为空,且pdf文件存储相对路径不能为空}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (waterMarkText==null){
|
||||||
|
waterMarkText = "";
|
||||||
|
}
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
inputStream = new FileInputStream(htmlFile);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
OutputStream outputStream = null;
|
||||||
|
try {
|
||||||
|
outputStream = new FileOutputStream(pdfFile);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
//微软雅黑在windows系统里的位置如下,linux系统直接拷贝该文件放在linux目录下即可
|
||||||
|
// String fontPath = "src/main/resources/font/STHeiti Light.ttc,0";
|
||||||
|
String fontPath = "src/main/resources/font/simsun.ttc,0";
|
||||||
|
HtmlToPdfUtils.convertToPdf(inputStream, waterMarkText, fontPath, outputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* html转pdf
|
||||||
|
*
|
||||||
|
* @param inputStream 输入流
|
||||||
|
* @param waterMark 水印
|
||||||
|
* @param fontPath 字体路径,ttc后缀的字体需要添加<b>,0<b/>
|
||||||
|
* @param outputStream 输出流
|
||||||
|
* @date : 2021/1/15 14:07
|
||||||
|
*/
|
||||||
|
public static void convertToPdf(InputStream inputStream, String waterMark, String fontPath, OutputStream outputStream) {
|
||||||
|
PdfWriter pdfWriter = new PdfWriter(outputStream);
|
||||||
|
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
|
||||||
|
//设置为A4大小
|
||||||
|
pdfDocument.setDefaultPageSize(PageSize.A4);
|
||||||
|
//添加水印
|
||||||
|
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new WaterMarkEventHandler(waterMark));
|
||||||
|
|
||||||
|
//添加中文字体支持
|
||||||
|
ConverterProperties properties = new ConverterProperties();
|
||||||
|
FontProvider fontProvider = new FontProvider();
|
||||||
|
|
||||||
|
|
||||||
|
// 设置字体
|
||||||
|
PdfFont sysFont = null;
|
||||||
|
try {
|
||||||
|
sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
fontProvider.addFont(sysFont.getFontProgram(), "UniGB-UCS2-H");
|
||||||
|
|
||||||
|
//添加自定义字体,例如微软雅黑
|
||||||
|
// if (StringUtils.isNotBlank(fontPath)) {
|
||||||
|
// PdfFont microsoft = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H, false);
|
||||||
|
// fontProvider.addFont(microsoft.getFontProgram(), PdfEncodings.IDENTITY_H);
|
||||||
|
// }
|
||||||
|
|
||||||
|
properties.setFontProvider(fontProvider);
|
||||||
|
// 读取Html文件流,查找出当中的 或出现类似的符号空格字符
|
||||||
|
inputStream = readInputStrem(inputStream);
|
||||||
|
if (inputStream != null) {
|
||||||
|
// 生成pdf文档
|
||||||
|
try {
|
||||||
|
HtmlConverter.convertToPdf(inputStream, pdfDocument, properties);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
pdfWriter.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
pdfDocument.close();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
log.error("转换失败!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取HTML 流文件,并查询当中的 或类似符号直接替换为空格
|
||||||
|
*
|
||||||
|
* @param inputStream
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static InputStream readInputStrem(InputStream inputStream) {
|
||||||
|
// 定义一些特殊字符的正则表达式 如:
|
||||||
|
String regEx_special = "\\&[a-zA-Z]{1,10};";
|
||||||
|
try {
|
||||||
|
//<1>创建字节数组输出流,用来输出读取到的内容
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
//<2>创建缓存大小
|
||||||
|
byte[] buffer = new byte[1024]; // 1KB
|
||||||
|
//每次读取到内容的长度
|
||||||
|
int len = -1;
|
||||||
|
//<3>开始读取输入流中的内容
|
||||||
|
while ((len = inputStream.read(buffer)) != -1) { //当等于-1说明没有数据可以读取了
|
||||||
|
baos.write(buffer, 0, len); //把读取到的内容写到输出流中
|
||||||
|
}
|
||||||
|
//<4> 把字节数组转换为字符串
|
||||||
|
String content = baos.toString();
|
||||||
|
//<5>关闭输入流和输出流
|
||||||
|
// inputStream.close();
|
||||||
|
baos.close();
|
||||||
|
// log.info("读取的内容:{}", content);
|
||||||
|
// 判断HTML内容是否具有HTML的特殊字符标记
|
||||||
|
Pattern compile = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
|
||||||
|
Matcher matcher = compile.matcher(content);
|
||||||
|
String replaceAll = matcher.replaceAll("");
|
||||||
|
// log.info("替换后的内容:{}", replaceAll);
|
||||||
|
// 将字符串转化为输入流返回
|
||||||
|
InputStream stringStream = getStringStream(replaceAll);
|
||||||
|
//<6>返回结果
|
||||||
|
return stringStream;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("错误信息:{}", e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将一个字符串转化为输入流
|
||||||
|
* @param sInputString 字符串
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static InputStream getStringStream(String sInputString) {
|
||||||
|
if (sInputString != null && !sInputString.trim().equals("")) {
|
||||||
|
try {
|
||||||
|
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
|
||||||
|
return tInputStringStream;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,145 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
public class HttpClientHelper {
|
||||||
|
private static final Logger logger = LogManager.getLogger(HttpClientHelper.class);
|
||||||
|
|
||||||
|
public static <T> String forwardSend(T entity, String myUrl) throws CloneNotSupportedException {
|
||||||
|
String result;
|
||||||
|
HttpClient httpClient = null;
|
||||||
|
HttpPost httpPost = null;
|
||||||
|
HttpResponse response = null;
|
||||||
|
HttpEntity resEntity = null;
|
||||||
|
try {
|
||||||
|
// 创建实体对象,例如:
|
||||||
|
//Student student = new Student("John", 20);
|
||||||
|
// 创建HttpClient实例
|
||||||
|
httpClient = HttpClientBuilder.create().build();
|
||||||
|
// 创建HttpPost实例
|
||||||
|
//String url = "http://example.com/api/students";
|
||||||
|
String url = myUrl;
|
||||||
|
httpPost = new HttpPost(url);
|
||||||
|
// 设置请求头
|
||||||
|
httpPost.setHeader("Content-Type", "application/json");
|
||||||
|
// 将Student对象转换为JSON字符串
|
||||||
|
// String json = "{\"name\": \"" + student.getName() + "\", \"age\": " + student.getAge() + "}";
|
||||||
|
//String json = JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue);
|
||||||
|
String json = new Gson().toJson(entity);
|
||||||
|
|
||||||
|
// 设置请求体
|
||||||
|
httpPost.setEntity(new StringEntity(json));
|
||||||
|
|
||||||
|
// 发送请求
|
||||||
|
response = httpClient.execute(httpPost);
|
||||||
|
// 获取响应实体
|
||||||
|
resEntity = response.getEntity();
|
||||||
|
// 判断响应状态
|
||||||
|
if (response.getStatusLine().getStatusCode() == 200) {
|
||||||
|
// 读取响应内容
|
||||||
|
String responseBody = EntityUtils.toString(resEntity);
|
||||||
|
result = responseBody;
|
||||||
|
} else {
|
||||||
|
result = "";
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
result = "";
|
||||||
|
} finally {
|
||||||
|
httpPost.clone();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> String forwardSendNew(T entity, String myUrl) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL(myUrl);
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
//请求方式是POST
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
// Post 请求不能使用缓存
|
||||||
|
// connection.setUseCaches(false);
|
||||||
|
// http正文内,因此需要设为true(不设置会要了你的老命)
|
||||||
|
connection.setDoOutput(Boolean.TRUE);
|
||||||
|
connection.setDoInput(Boolean.TRUE);
|
||||||
|
//设置连接超时时间
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
//设置读取超时时间
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
//必须设置false,否则会自动redirect到重定向后的地址
|
||||||
|
connection.setInstanceFollowRedirects(false);
|
||||||
|
// 设置通用的请求属性
|
||||||
|
connection.setRequestProperty("accept", "*/*");
|
||||||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
|
||||||
|
|
||||||
|
// 创建要发送的实体 Student 对象,并将其转换为 JSON 字符串
|
||||||
|
//String jsonBody = JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue);
|
||||||
|
String jsonBody = new Gson().toJson(entity);
|
||||||
|
// 设置请求头部的 Content-Length 属性
|
||||||
|
int contentLength = jsonBody.getBytes(StandardCharsets.UTF_8).length;
|
||||||
|
connection.setRequestProperty("Content-Length", String.valueOf(4096));
|
||||||
|
|
||||||
|
// 获取输出流,并将 JSON 字符串写入连接
|
||||||
|
OutputStream outputStream = connection.getOutputStream();
|
||||||
|
outputStream.write(jsonBody.getBytes(StandardCharsets.UTF_8));
|
||||||
|
// outputStream.write(jsonBody.getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
// int responseCode = connection.getResponseCode();
|
||||||
|
// if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
// inputStream = connection.getInputStream();
|
||||||
|
// // 处理输入流的内容
|
||||||
|
// // 定义 BufferedReader输入流来读取URL的响应
|
||||||
|
// BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
// String line;
|
||||||
|
// while ((line = in.readLine()) != null) {
|
||||||
|
// result.append(line);
|
||||||
|
// }
|
||||||
|
// inputStream.close();
|
||||||
|
// } else {
|
||||||
|
// // 处理错误情况
|
||||||
|
// }
|
||||||
|
|
||||||
|
inputStream = connection.getInputStream();
|
||||||
|
// 处理输入流的内容
|
||||||
|
// 定义 BufferedReader输入流来读取URL的响应
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
inputStream.close();
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("错误:" + ex.getMessage());
|
||||||
|
result.append(ex.getMessage());
|
||||||
|
return result.toString();
|
||||||
|
} finally {
|
||||||
|
// 关闭流和连接
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,253 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class HttpRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定URL发送GET方法的请求
|
||||||
|
*
|
||||||
|
* @param access_token 权限请求头 必须项
|
||||||
|
* @param url 发送请求的URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return URL 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendGet(String access_token, String url, String param) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
BufferedReader in = null;
|
||||||
|
try {
|
||||||
|
String urlNameString = url + "?" + param;
|
||||||
|
URL realUrl = new URL(urlNameString);
|
||||||
|
// 打开和URL之间的连接
|
||||||
|
URLConnection connection = realUrl.openConnection();
|
||||||
|
// 设置通用的请求属性
|
||||||
|
connection.setRequestProperty("accept", "*/*");
|
||||||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
|
||||||
|
//设置权限请求头 access_token 必须项
|
||||||
|
if (access_token != null && !access_token.equals("")) {
|
||||||
|
String myToken = "Bearer " + access_token;
|
||||||
|
connection.setRequestProperty("authorization", myToken);
|
||||||
|
}
|
||||||
|
//设置连接超时时间
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
//设置读取超时时间
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
// 建立实际的连接
|
||||||
|
connection.connect();
|
||||||
|
// 获取所有响应头字段
|
||||||
|
Map<String, List<String>> map = connection.getHeaderFields();
|
||||||
|
// 遍历所有的响应头字段
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
System.out.println(key + "--->" + map.get(key));
|
||||||
|
}
|
||||||
|
// 定义 BufferedReader输入流来读取URL的响应
|
||||||
|
in = new BufferedReader(new InputStreamReader(
|
||||||
|
connection.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("发送GET请求出现异常!" + e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
// 使用finally块来关闭输入流
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e2) {
|
||||||
|
e2.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定URL发送GET方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return URL 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendGet(String url, String param) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
BufferedReader in = null;
|
||||||
|
try {
|
||||||
|
String urlNameString = url + "?" + param;
|
||||||
|
URL realUrl = new URL(urlNameString);
|
||||||
|
// 打开和URL之间的连接
|
||||||
|
URLConnection connection = realUrl.openConnection();
|
||||||
|
// 设置通用的请求属性
|
||||||
|
connection.setRequestProperty("accept", "*/*");
|
||||||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
|
||||||
|
//设置连接超时时间
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
//设置读取超时时间
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
// 建立实际的连接
|
||||||
|
connection.connect();
|
||||||
|
// 获取所有响应头字段
|
||||||
|
Map<String, List<String>> map = connection.getHeaderFields();
|
||||||
|
// 遍历所有的响应头字段
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
System.out.println(key + "--->" + map.get(key));
|
||||||
|
}
|
||||||
|
// 定义 BufferedReader输入流来读取URL的响应
|
||||||
|
in = new BufferedReader(new InputStreamReader(
|
||||||
|
connection.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("发送GET请求出现异常!" + e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
// 使用finally块来关闭输入流
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e2) {
|
||||||
|
e2.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送POST方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendPost(String access_token, String url, String param) {
|
||||||
|
PrintWriter out = null;
|
||||||
|
BufferedReader in = null;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try {
|
||||||
|
URL realUrl = new URL(url);
|
||||||
|
// 打开和URL之间的连接
|
||||||
|
URLConnection conn = realUrl.openConnection();
|
||||||
|
// 设置通用的请求属性
|
||||||
|
conn.setRequestProperty("accept", "*/*");
|
||||||
|
conn.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
//设置权限请求头 access_token 必须项
|
||||||
|
if (access_token != null && !access_token.equals("")) {
|
||||||
|
String myToken = "Bearer " + access_token;
|
||||||
|
conn.setRequestProperty("authorization", myToken);
|
||||||
|
}
|
||||||
|
//设置连接超时时间
|
||||||
|
conn.setConnectTimeout(15000);
|
||||||
|
//设置读取超时时间
|
||||||
|
conn.setReadTimeout(15000);
|
||||||
|
// 发送POST请求必须设置如下两行
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
// 获取URLConnection对象对应的输出流
|
||||||
|
out = new PrintWriter(conn.getOutputStream());
|
||||||
|
// 发送请求参数
|
||||||
|
out.print(param);
|
||||||
|
// flush输出流的缓冲
|
||||||
|
out.flush();
|
||||||
|
// 定义BufferedReader输入流来读取URL的响应
|
||||||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("发送 POST 请求出现异常!" + e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
//使用finally块来关闭输出流、输入流
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 向指定 URL 发送POST方法的请求
|
||||||
|
*
|
||||||
|
* @param url 发送请求的 URL
|
||||||
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||||
|
* @return 所代表远程资源的响应结果
|
||||||
|
*/
|
||||||
|
public static String sendPost(String url, String param) {
|
||||||
|
PrintWriter out = null;
|
||||||
|
BufferedReader in = null;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try {
|
||||||
|
URL realUrl = new URL(url);
|
||||||
|
// 打开和URL之间的连接
|
||||||
|
URLConnection conn = realUrl.openConnection();
|
||||||
|
// 设置通用的请求属性
|
||||||
|
conn.setRequestProperty("accept", "*/*");
|
||||||
|
conn.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||||
|
//设置连接超时时间
|
||||||
|
conn.setConnectTimeout(15000);
|
||||||
|
//设置读取超时时间
|
||||||
|
conn.setReadTimeout(15000);
|
||||||
|
// 发送POST请求必须设置如下两行
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
// 获取URLConnection对象对应的输出流
|
||||||
|
out = new PrintWriter(conn.getOutputStream());
|
||||||
|
// 发送请求参数
|
||||||
|
out.print(param);
|
||||||
|
// flush输出流的缓冲
|
||||||
|
out.flush();
|
||||||
|
// 定义BufferedReader输入流来读取URL的响应
|
||||||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("发送 POST 请求出现异常!" + e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
//使用finally块来关闭输出流、输入流
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,236 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件名: ${file_name}
|
||||||
|
* 版权: Copyright by wsnet
|
||||||
|
* 描述:
|
||||||
|
* 修改人: kch
|
||||||
|
* 修改时间:2023/6/13
|
||||||
|
* 跟踪单号:
|
||||||
|
* 修改单号:
|
||||||
|
* 修改内容:
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class HttpTemplate {
|
||||||
|
/**
|
||||||
|
* http get请求
|
||||||
|
*
|
||||||
|
* @param httpUrl 链接
|
||||||
|
* @return 响应数据
|
||||||
|
*/
|
||||||
|
public static String doGet(String httpUrl) {
|
||||||
|
//链接
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
InputStream is = null;
|
||||||
|
BufferedReader br = null;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try {
|
||||||
|
//创建连接
|
||||||
|
URL url = new URL(httpUrl);
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
//设置请求方式
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
//设置连接超时时间
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
//设置读取超时时间
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
//开始连接
|
||||||
|
connection.connect();
|
||||||
|
//获取响应数据
|
||||||
|
if (connection.getResponseCode() == 200) {
|
||||||
|
//获取返回的数据
|
||||||
|
is = connection.getInputStream();
|
||||||
|
if (is != null) {
|
||||||
|
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||||
|
String temp = null;
|
||||||
|
while ((temp = br.readLine()) != null) {
|
||||||
|
result.append(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (br != null) {
|
||||||
|
try {
|
||||||
|
br.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (is != null) {
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert connection != null;
|
||||||
|
connection.disconnect();// 关闭远程连接
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post请求
|
||||||
|
*
|
||||||
|
* @param httpUrl 链接
|
||||||
|
* @param param 参数
|
||||||
|
* @return 返回请求结果
|
||||||
|
*/
|
||||||
|
public static String doPost(String httpUrl, @Nullable String param) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
//连接
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
OutputStream os = null;
|
||||||
|
InputStream is = null;
|
||||||
|
BufferedReader br = null;
|
||||||
|
try {
|
||||||
|
//创建连接对象
|
||||||
|
URL url = new URL(httpUrl);
|
||||||
|
//创建连接
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
//设置请求方法
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
//设置连接超时时间
|
||||||
|
connection.setConnectTimeout(15000);
|
||||||
|
//设置读取超时时间
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
//设置是否可读取
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
//设置响应是否可读取
|
||||||
|
connection.setDoInput(true);
|
||||||
|
//设置参数类型
|
||||||
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
//拼装参数
|
||||||
|
if (param != null && !param.equals("")) {
|
||||||
|
//设置参数
|
||||||
|
os = connection.getOutputStream();
|
||||||
|
//拼装参数
|
||||||
|
os.write(param.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
//设置权限
|
||||||
|
//设置请求头等
|
||||||
|
//开启连接
|
||||||
|
//connection.connect();
|
||||||
|
//读取响应
|
||||||
|
if (connection.getResponseCode() == 200) {
|
||||||
|
is = connection.getInputStream();
|
||||||
|
if (is != null) {
|
||||||
|
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
|
||||||
|
String temp = null;
|
||||||
|
if ((temp = br.readLine()) != null) {
|
||||||
|
result.append(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//关闭连接
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (br != null) {
|
||||||
|
try {
|
||||||
|
br.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (os != null) {
|
||||||
|
try {
|
||||||
|
os.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (is != null) {
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//关闭连接
|
||||||
|
assert connection != null;
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http get请求
|
||||||
|
*
|
||||||
|
* @param httpUrl 链接
|
||||||
|
* @return 响应数据
|
||||||
|
*/
|
||||||
|
public static String doPicUpload(String httpUrl) {
|
||||||
|
//链接
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
InputStream is = null;
|
||||||
|
BufferedReader br = null;
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try {
|
||||||
|
//创建连接
|
||||||
|
URL url = new URL(httpUrl);
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
//设置请求方式
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
//设置连接超时时间
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
//设置读取超时时间
|
||||||
|
connection.setReadTimeout(15000);
|
||||||
|
|
||||||
|
// DataInputStream in = new DataInputStream(new FileInputStream(file));
|
||||||
|
// int bytes = 0;
|
||||||
|
// byte[] bufferOut = new byte[2048];
|
||||||
|
// while ((bytes = in.read(bufferOut)) != -1) {
|
||||||
|
// out.write(bufferOut, 0, bytes);
|
||||||
|
// }
|
||||||
|
//in.close();
|
||||||
|
|
||||||
|
|
||||||
|
//开始连接
|
||||||
|
connection.connect();
|
||||||
|
//获取响应数据
|
||||||
|
if (connection.getResponseCode() == 200) {
|
||||||
|
//获取返回的数据
|
||||||
|
is = connection.getInputStream();
|
||||||
|
if (is != null) {
|
||||||
|
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||||
|
String temp = null;
|
||||||
|
while ((temp = br.readLine()) != null) {
|
||||||
|
result.append(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (br != null) {
|
||||||
|
try {
|
||||||
|
br.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (is != null) {
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert connection != null;
|
||||||
|
connection.disconnect();// 关闭远程连接
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author G1F
|
||||||
|
* @date 2019/12/23 9:02
|
||||||
|
* @description 获取请求真实IP
|
||||||
|
*/
|
||||||
|
public class IpHelper {
|
||||||
|
//
|
||||||
|
public static String getIpAddr(HttpServletRequest request) {
|
||||||
|
String ipAddress = null;
|
||||||
|
try {
|
||||||
|
ipAddress = request.getHeader("x-forwarded-for");
|
||||||
|
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||||
|
ipAddress = request.getHeader("Proxy-Client-IP");
|
||||||
|
}
|
||||||
|
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||||
|
ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
||||||
|
}
|
||||||
|
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||||
|
ipAddress = request.getRemoteAddr();
|
||||||
|
if (ipAddress.equals("127.0.0.1")) {
|
||||||
|
// 根据网卡取本机配置的IP
|
||||||
|
InetAddress inet = null;
|
||||||
|
try {
|
||||||
|
inet = InetAddress.getLocalHost();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
assert inet != null;
|
||||||
|
ipAddress = inet.getHostAddress();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
||||||
|
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
|
||||||
|
if (ipAddress.indexOf(",") > 0) {
|
||||||
|
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
ipAddress = "";
|
||||||
|
}
|
||||||
|
return ipAddress;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import com.itextpdf.kernel.events.Event;
|
||||||
|
import com.itextpdf.kernel.events.IEventHandler;
|
||||||
|
import com.itextpdf.kernel.events.PdfDocumentEvent;
|
||||||
|
import com.itextpdf.kernel.font.PdfFont;
|
||||||
|
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||||
|
import com.itextpdf.kernel.geom.Rectangle;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfPage;
|
||||||
|
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||||
|
import com.itextpdf.layout.Canvas;
|
||||||
|
import com.itextpdf.layout.element.Paragraph;
|
||||||
|
import com.itextpdf.layout.property.TextAlignment;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 页码
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class PageEventHandler implements IEventHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleEvent(Event event) {
|
||||||
|
PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
|
||||||
|
PdfDocument document = documentEvent.getDocument();
|
||||||
|
PdfPage page = documentEvent.getPage();
|
||||||
|
Rectangle pageSize = page.getPageSize();
|
||||||
|
|
||||||
|
PdfFont pdfFont = null;
|
||||||
|
try {
|
||||||
|
pdfFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), document);
|
||||||
|
Canvas canvas = new Canvas(pdfCanvas, pageSize);
|
||||||
|
float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
|
||||||
|
float y = pageSize.getBottom() + 15;
|
||||||
|
Paragraph paragraph = new Paragraph("第" + document.getPageNumber(page) + "页/共" + document.getNumberOfPages() + "页")
|
||||||
|
.setFontSize(10)
|
||||||
|
.setFont(pdfFont);
|
||||||
|
canvas.showTextAligned(paragraph, x, y, TextAlignment.CENTER);
|
||||||
|
canvas.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
//import com.itextpdf.text.*;
|
||||||
|
//import com.itextpdf.text.pdf.*;
|
||||||
|
//import org.apache.commons.io.FileUtils;
|
||||||
|
//import org.apache.commons.io.IOUtils;
|
||||||
|
//import org.apache.commons.lang3.StringUtils;
|
||||||
|
//import org.apache.tomcat.util.http.fileupload.FileUtils;
|
||||||
|
//import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||||
|
//import org.junit.Test;
|
||||||
|
//import pojo.user;
|
||||||
|
//import java.io.*;
|
||||||
|
//import java.util.*;
|
||||||
|
public class PdfUtils {
|
||||||
|
// /**
|
||||||
|
// * @param map 需要填充的字段
|
||||||
|
// * @param sourceFile 原文件路径
|
||||||
|
// * @param targetFile 目标文件路径
|
||||||
|
// * @param imgURLMap 填充图片路径
|
||||||
|
// * @throws IOException
|
||||||
|
// */
|
||||||
|
// public static void genPdf(Map<String, String> map, String sourceFile, String targetFile) throws IOException {
|
||||||
|
// File templateFile = new File(sourceFile);
|
||||||
|
// fillParam(map, FileUtils.readFileToByteArray(templateFile), targetFile);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// *使用map中的参数填充pdf,map中的key和pdf表单中的field对应
|
||||||
|
// * @param fieldValueMap
|
||||||
|
// * @param file
|
||||||
|
// * @param contractFileName
|
||||||
|
// * @param imgURLMap //这边暂时吧图片给注释了,如需填充图片直接加参数即可
|
||||||
|
// */
|
||||||
|
// public static void fillParam(Map<String, String> fieldValueMap,byte[] file, String contractFileName) {
|
||||||
|
// //输出流
|
||||||
|
// FileOutputStream fos = null;
|
||||||
|
// try {
|
||||||
|
// fos = new FileOutputStream(contractFileName);
|
||||||
|
// //获取PdfReader对象,获取模板位置
|
||||||
|
// PdfReader reader = null;
|
||||||
|
// /* 将要生成的目标PDF文件名称 */
|
||||||
|
// PdfStamper stamper = null;
|
||||||
|
// BaseFont base = null;
|
||||||
|
// //取出模板中的所有字段
|
||||||
|
// AcroFields acroFields = null;
|
||||||
|
// // 获取存在resources目录下的pdf模板位置 URL
|
||||||
|
// //URL file = PdfUtils.class.getClassLoader().getResource("CONTRACT.pdf");
|
||||||
|
// try {
|
||||||
|
// reader = new PdfReader(file);
|
||||||
|
// stamper = new PdfStamper(reader, fos);
|
||||||
|
// stamper.setFormFlattening(true);
|
||||||
|
// //简体中文字体
|
||||||
|
// base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
||||||
|
// acroFields = stamper.getAcroFields();
|
||||||
|
//
|
||||||
|
// //如果图片放在resources目录下需要这么写
|
||||||
|
// // String imgUrl = new ClassPathResource("static/IMG_5809.JPG").getURL().getPath();
|
||||||
|
// //循环添加公章图片
|
||||||
|
// //for(String key : imgURLMap.keySet()) {
|
||||||
|
// // String value = imgURLMap.get(key).toString();
|
||||||
|
// // //获取图片域名
|
||||||
|
// // AcroFields.FieldPosition position = acroFields.getFieldPositions(key).get(0);
|
||||||
|
// // //通过域名获取所在页和坐标,左下角为起点
|
||||||
|
// // int pageNo = position.page;
|
||||||
|
// // Rectangle signRect = position.position;
|
||||||
|
// // float x = signRect.getLeft();
|
||||||
|
// // float y = signRect.getBottom();
|
||||||
|
// // //读图片
|
||||||
|
// // Image image = Image.getInstance(value);
|
||||||
|
// // //获取操作页面
|
||||||
|
// // PdfContentByte under = stamper.getOverContent(pageNo);
|
||||||
|
// // //根据域的大小缩放图片
|
||||||
|
// // image.scaleToFit(signRect.getWidth(),signRect.getHeight());
|
||||||
|
// // //添加图片
|
||||||
|
// // image.setAbsolutePosition(x,y);
|
||||||
|
// // under.addImage(image);
|
||||||
|
// // System.out.println("--"+key+"---"+value);
|
||||||
|
// //}
|
||||||
|
// for (String key : acroFields.getFields().keySet()) {
|
||||||
|
// acroFields.setFieldProperty(key, "textfont", base, null);
|
||||||
|
// //字体大小
|
||||||
|
// acroFields.setFieldProperty(key, "textsize", new Float(12), null);
|
||||||
|
// }
|
||||||
|
// if (fieldValueMap != null) {
|
||||||
|
// for (String fieldName : fieldValueMap.keySet()) {
|
||||||
|
//
|
||||||
|
// if (StringUtils.isNotBlank(fieldValueMap.get(fieldName))) {
|
||||||
|
// //获取map中key对应的Value是否为On,若是则勾选复选框
|
||||||
|
// if (fieldValueMap.get(fieldName).equals("On") || fieldValueMap.get(fieldName) == "On") {
|
||||||
|
// acroFields.setField(fieldName, fieldValueMap.get(fieldName),"true");
|
||||||
|
// }else{
|
||||||
|
// acroFields.setField(fieldName, fieldValueMap.get(fieldName));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// } finally {
|
||||||
|
// if (stamper != null) {
|
||||||
|
// try {
|
||||||
|
// stamper.close();
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (reader != null) {
|
||||||
|
// reader.close();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// System.out.println("填充参数异常");
|
||||||
|
// e.printStackTrace();
|
||||||
|
// } finally {
|
||||||
|
// IOUtils.closeQuietly(fos);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
public class PostFiles {
|
||||||
|
|
||||||
|
// public static void saveFile( MultipartFile filecontent){
|
||||||
|
// OutputStream os = null;
|
||||||
|
// InputStream inputStream = null;
|
||||||
|
// String fileName = null;
|
||||||
|
// try {
|
||||||
|
// inputStream = filecontent.getInputStream();
|
||||||
|
// fileName = fileconteXKfIbmTzZDnt.getOriginalFilename();
|
||||||
|
// //fileName = fileconteXKfIbmTzZDnt.getOriginalFilename();
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// try {
|
||||||
|
// String path = "C:\\test\\";
|
||||||
|
// // 2、保存到临时文件
|
||||||
|
// // 1K的数据缓冲
|
||||||
|
// byte[] bs = new byte[1024];
|
||||||
|
// // 读取到的数据长度
|
||||||
|
// int len;
|
||||||
|
// // 输出的文件流保存到本地文件
|
||||||
|
// File tempFile = new File(path);
|
||||||
|
// if (!tempFile.exists()) {
|
||||||
|
// tempFile.mkdirs();
|
||||||
|
// }
|
||||||
|
// os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
|
||||||
|
// // 开始读取
|
||||||
|
// while ((len = inputStream.read(bs)) != -1) {
|
||||||
|
// os.write(bs, 0, len);
|
||||||
|
// }
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// } finally {
|
||||||
|
// // 完毕,关闭所有链接
|
||||||
|
// try {
|
||||||
|
// os.close();
|
||||||
|
// inputStream.close();
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排队号帮助类
|
||||||
|
*/
|
||||||
|
public class QueueNumberHelper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得新排队号
|
||||||
|
* @param lastQueueNumber 前排队号
|
||||||
|
* @return 返回排队号
|
||||||
|
*/
|
||||||
|
public static String getNewQueueNumber(String lastQueueNumber) {
|
||||||
|
String queueNumber = "";
|
||||||
|
if (lastQueueNumber == null || lastQueueNumber.length() < 10) {
|
||||||
|
queueNumber = getTodayStr() + "0001";
|
||||||
|
return queueNumber;
|
||||||
|
}
|
||||||
|
String lastDateStr = lastQueueNumber.substring(0, 6);
|
||||||
|
String todayStr = getTodayStr();
|
||||||
|
//同一天
|
||||||
|
if (lastDateStr.equals(todayStr)) {
|
||||||
|
try {
|
||||||
|
long res = Long.parseLong(lastQueueNumber);
|
||||||
|
res = res + 1;
|
||||||
|
queueNumber = Long.toString(res);
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
//日期转钟的处理
|
||||||
|
queueNumber = getTodayStr() + "0001";
|
||||||
|
}
|
||||||
|
return queueNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取今天文件夹日期
|
||||||
|
*
|
||||||
|
* @return 返回时间类型 yyyyMMdd
|
||||||
|
*/
|
||||||
|
private static String getTodayStr() {
|
||||||
|
Date currentTime = new Date();
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
|
||||||
|
return formatter.format(currentTime);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
public class StringHelper {
|
||||||
|
/**
|
||||||
|
* htmlStr 转化位纯文本
|
||||||
|
* @param htmlStr htmlStr
|
||||||
|
* @return txt
|
||||||
|
*/
|
||||||
|
public static String getTxtByHtml(String htmlStr) {
|
||||||
|
do {
|
||||||
|
int a = htmlStr.indexOf("<");
|
||||||
|
int b = htmlStr.indexOf(">");
|
||||||
|
if (a != -1 && b != -1) {
|
||||||
|
htmlStr = htmlStr.substring(0, a) + htmlStr.substring(b + 1);
|
||||||
|
}
|
||||||
|
} while (htmlStr.indexOf("<") != -1 && htmlStr.indexOf(">") != -1);
|
||||||
|
|
||||||
|
return htmlStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
package com.haitongauto.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import com.itextpdf.kernel.colors.WebColors;
|
||||||
|
import com.itextpdf.kernel.events.Event;
|
||||||
|
import com.itextpdf.kernel.events.IEventHandler;
|
||||||
|
import com.itextpdf.kernel.events.PdfDocumentEvent;
|
||||||
|
import com.itextpdf.kernel.font.PdfFont;
|
||||||
|
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||||
|
import com.itextpdf.kernel.geom.Rectangle;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfPage;
|
||||||
|
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||||
|
import com.itextpdf.layout.Canvas;
|
||||||
|
import com.itextpdf.layout.element.Paragraph;
|
||||||
|
import com.itextpdf.layout.property.TextAlignment;
|
||||||
|
import com.itextpdf.layout.property.VerticalAlignment;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 水印
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class WaterMarkEventHandler implements IEventHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 水印内容
|
||||||
|
*/
|
||||||
|
private final String waterMarkContent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一页中有几列水印
|
||||||
|
*/
|
||||||
|
private final int waterMarkX;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一页中每列有多少水印
|
||||||
|
*/
|
||||||
|
private final int waterMarkY;
|
||||||
|
|
||||||
|
public WaterMarkEventHandler(String waterMarkContent) {
|
||||||
|
this(waterMarkContent, 5, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WaterMarkEventHandler(String waterMarkContent, int waterMarkX, int waterMarkY) {
|
||||||
|
this.waterMarkContent = waterMarkContent;
|
||||||
|
this.waterMarkX = waterMarkX;
|
||||||
|
this.waterMarkY = waterMarkY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleEvent(Event event) {
|
||||||
|
|
||||||
|
PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
|
||||||
|
PdfDocument document = documentEvent.getDocument();
|
||||||
|
PdfPage page = documentEvent.getPage();
|
||||||
|
Rectangle pageSize = page.getPageSize();
|
||||||
|
|
||||||
|
PdfFont pdfFont = null;
|
||||||
|
try {
|
||||||
|
pdfFont = PdfFontFactory.createFont();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), document);
|
||||||
|
|
||||||
|
Paragraph waterMark = new Paragraph(waterMarkContent).setOpacity(0.5f);
|
||||||
|
Canvas canvas = new Canvas(pdfCanvas, pageSize)
|
||||||
|
.setFontColor(WebColors.getRGBColor("lightgray"))
|
||||||
|
.setFontSize(16)
|
||||||
|
.setFont(pdfFont);
|
||||||
|
|
||||||
|
for (int i = 0; i < waterMarkX; i++) {
|
||||||
|
for (int j = 0; j < waterMarkY; j++) {
|
||||||
|
canvas.showTextAligned(waterMark, (150 + i * 300), (160 + j * 150), document.getNumberOfPages(),
|
||||||
|
TextAlignment.CENTER, VerticalAlignment.BOTTOM, 120);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
canvas.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.haitongauto.utils.check;
|
||||||
|
|
||||||
|
import com.bestvike.linq.Linq;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class CheckHelper {
|
||||||
|
/**
|
||||||
|
* 验证vins受否狗17位(6-17 位 王文文提出)
|
||||||
|
* 返回验证不通过的vins
|
||||||
|
*/
|
||||||
|
public static List<String> CheckVinLength(List<String> vinlist) {
|
||||||
|
if (vinlist == null || vinlist.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<String> myvinlist = Linq.of(vinlist).where(p -> p != null && !p.isEmpty()).toList();
|
||||||
|
if (vinlist == null || vinlist.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<String> vins = new ArrayList<>();
|
||||||
|
//找出长度不在6-17之间的(即不合格的)vin,
|
||||||
|
vins = Linq.of(myvinlist).where(o -> !(o.length() >= 6 && o.length() <= 17)).toList();
|
||||||
|
return vins;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证vins只能包含数字或英文字母 digit
|
||||||
|
* 返回验证不通过的vins
|
||||||
|
*/
|
||||||
|
public static List<String> CheckVinContainEnOrDi(List<String> vinlist) {
|
||||||
|
if (vinlist == null || vinlist.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<String> myvinlist = Linq.of(vinlist).where(p -> p != null && !p.isEmpty()).toList();
|
||||||
|
if (vinlist == null || vinlist.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<String> vins = new ArrayList<>();
|
||||||
|
//包含字母或数字的正则
|
||||||
|
String pattern = "^[a-zA-Z0-9]+$";
|
||||||
|
//找出有不包含字母或数字的
|
||||||
|
vins = Linq.of(myvinlist).where(o -> !o.matches(pattern)).toList();
|
||||||
|
return vins;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证手机号码
|
||||||
|
*
|
||||||
|
* @param phoneNumber 你要验证的手机号
|
||||||
|
* @return 返回
|
||||||
|
*/
|
||||||
|
public static Boolean checkTelPhoneNumber(String phoneNumber) {
|
||||||
|
Boolean res = false;
|
||||||
|
if (phoneNumber == null || phoneNumber.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 定义手机号的正则表达式
|
||||||
|
String regex = "^(13\\d|14[5-9]|15[0-3,5-9]|16[2,5,6]|17[0-8]|18\\d|19[0-3,5-9])\\d{8}$";
|
||||||
|
// 创建 Pattern 对象
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
// 创建 Matcher 对象
|
||||||
|
Matcher matcher = pattern.matcher(phoneNumber);
|
||||||
|
// 判断手机号是否符合正则表达式
|
||||||
|
if (matcher.matches()) {
|
||||||
|
res = true;
|
||||||
|
System.out.println("手机号验证通过");
|
||||||
|
} else {
|
||||||
|
res = false;
|
||||||
|
System.out.println("手机号验证不通过");
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.haitongauto.utils.excel;
|
||||||
|
|
||||||
|
import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.CellType;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class ExcelCustomCellWriteHeightConfig extends AbstractRowHeightStyleStrategy {
|
||||||
|
/**
|
||||||
|
* 默认高度
|
||||||
|
*/
|
||||||
|
private static final Integer DEFAULT_HEIGHT = 300;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param row row
|
||||||
|
* @param relativeRowIndex relativeRowIndex
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void setHeadColumnHeight(Row row, int relativeRowIndex) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param row row
|
||||||
|
* @param relativeRowIndex relativeRowIndex
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void setContentColumnHeight(Row row, int relativeRowIndex) {
|
||||||
|
Iterator<Cell> cellIterator = row.cellIterator();
|
||||||
|
if (!cellIterator.hasNext()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 默认为 1行高度
|
||||||
|
int maxHeight = 1;
|
||||||
|
while (cellIterator.hasNext()) {
|
||||||
|
Cell cell = cellIterator.next();
|
||||||
|
if (cell.getCellTypeEnum() == CellType.STRING) {
|
||||||
|
String value = cell.getStringCellValue();
|
||||||
|
int len = value.length();
|
||||||
|
int num = 0;
|
||||||
|
if (len > 50) {
|
||||||
|
num = len % 50 > 0 ? len / 50 : len / 2 - 1;
|
||||||
|
}
|
||||||
|
if (num > 0) {
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
value = value.substring(0, (i + 1) * 50 + i) + "\n" + value.substring((i + 1) * 50 + i, len + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (value.contains("\n")) {
|
||||||
|
int length = value.split("\n").length;
|
||||||
|
maxHeight = Math.max(maxHeight, length) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row.setHeight((short) ((maxHeight) * DEFAULT_HEIGHT));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.haitongauto.utils.excel;
|
||||||
|
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||||
|
import com.alibaba.excel.metadata.Head;
|
||||||
|
import com.alibaba.excel.metadata.data.CellData;
|
||||||
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||||
|
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||||
|
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
|
||||||
|
//import org.apache.commons.collections.CollectionUtils;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
public class ExcelCustomCellWriteWidthConfig extends AbstractColumnWidthStyleStrategy {
|
||||||
|
|
||||||
|
private final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param writeSheetHolder writeSheetHolder
|
||||||
|
* @param cellDataList cellDataList
|
||||||
|
* @param cell cell
|
||||||
|
* @param head head
|
||||||
|
* @param integer integer
|
||||||
|
* @param isHead isHead
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) {
|
||||||
|
//boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
|
||||||
|
boolean needSetWidth = isHead ||cellDataList!=null||cellDataList.size()>0;
|
||||||
|
if (needSetWidth) {
|
||||||
|
Map<Integer, Integer> maxColumnWidthMap = CACHE.computeIfAbsent(writeSheetHolder.getSheetNo(), k -> new HashMap<>());
|
||||||
|
|
||||||
|
Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
|
||||||
|
// 单元格文本长度大于60换行
|
||||||
|
if (columnWidth >= 0) {
|
||||||
|
if (columnWidth > 60) {
|
||||||
|
columnWidth = 60;
|
||||||
|
}
|
||||||
|
Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
|
||||||
|
if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
|
||||||
|
maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
|
||||||
|
Sheet sheet = writeSheetHolder.getSheet();
|
||||||
|
sheet.setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算长度
|
||||||
|
*
|
||||||
|
* @param cellDataList cellDataList
|
||||||
|
* @param cell cell
|
||||||
|
* @param isHead isHead
|
||||||
|
* @return return
|
||||||
|
*/
|
||||||
|
private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
|
||||||
|
if (isHead) {
|
||||||
|
return cell.getStringCellValue().getBytes().length;
|
||||||
|
} else {
|
||||||
|
CellData<?> cellData = cellDataList.get(0);
|
||||||
|
CellDataTypeEnum type = cellData.getType();
|
||||||
|
if (type == null) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
switch (type) {
|
||||||
|
case STRING:
|
||||||
|
// 换行符(数据需要提前解析好)
|
||||||
|
int index = cellData.getStringValue().indexOf("\n");
|
||||||
|
return index != -1 ?
|
||||||
|
cellData.getStringValue().substring(0, index).getBytes().length + 1 : cellData.getStringValue().getBytes().length + 1;
|
||||||
|
case BOOLEAN:
|
||||||
|
return cellData.getBooleanValue().toString().getBytes().length;
|
||||||
|
case NUMBER:
|
||||||
|
return cellData.getNumberValue().toString().getBytes().length;
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,239 @@
|
||||||
|
package com.haitongauto.utils.excel;
|
||||||
|
|
||||||
|
import com.alibaba.excel.EasyExcel;
|
||||||
|
import com.alibaba.excel.ExcelWriter;
|
||||||
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||||
|
import com.haitongauto.utils.FormatDateTime;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class ExcelGenerateHelper {
|
||||||
|
/**
|
||||||
|
* 导出 Excel 文件, 有表头
|
||||||
|
*
|
||||||
|
* @param dateList 想要导出的数据列表
|
||||||
|
* @param clazz 待导出数据的类型
|
||||||
|
* @param filePath 导出后的excel文件的存储路径
|
||||||
|
* @param <T> 类型
|
||||||
|
*/
|
||||||
|
public static <T> void outPutExcel(List<T> dateList, Class<T> clazz, String filePath) {
|
||||||
|
// 默认导出地址 obj.getClass().getName() 使用反射获取类的名称
|
||||||
|
//String fileName = "C:\\Users\\lenovo\\Desktop\\图片工具" + "\\" + clazz.getSimpleName() + ".xlsx";
|
||||||
|
String fileName = "";
|
||||||
|
if (filePath != null && !filePath.isEmpty()) {
|
||||||
|
fileName = filePath + clazz.getSimpleName() + ".xlsx";
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Excel文件: " + clazz.getSimpleName() + ".xlsx 开始生成");
|
||||||
|
|
||||||
|
// 创建一个输出流,将文件输出
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
outputStream = new FileOutputStream(fileName);
|
||||||
|
// EasyExcel.write(outputStream) 为了使用流将数据导出
|
||||||
|
ExcelWriter writer = EasyExcel.write(outputStream).build();
|
||||||
|
// writerSheet 中 sheetNo 表示导出的是 Excel 中第几页数据,sheetName 表示导出的该页数据名称
|
||||||
|
// head 表示 Excel 数据需要映射的类
|
||||||
|
WriteSheet sheet = EasyExcel.writerSheet(0, "Excel表第0页的名称")
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteWidthConfig()) /*自适应列宽*/
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteHeightConfig()) /*自适应行高(根据自己情况选择使用,我这里没用到)*/
|
||||||
|
.head(clazz)//根据实体类设置表头
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 开始写入数据
|
||||||
|
writer.write(dateList, sheet);
|
||||||
|
// 刷新数据
|
||||||
|
writer.finish();
|
||||||
|
outputStream.flush();
|
||||||
|
System.out.println("Excel文件生成成功 ---> 生成位置: " + fileName);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (outputStream != null) {
|
||||||
|
// 关闭流
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param dateList 数据
|
||||||
|
* @param clazz 类型
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @param <T> 类型
|
||||||
|
* @return 返回文件名(全路径)
|
||||||
|
*/
|
||||||
|
public static <T> String outPutExcelReturnFileName(List<T> dateList, Class<T> clazz, String filePath) {
|
||||||
|
|
||||||
|
// 默认导出地址 obj.getClass().getName() 使用反射获取类的名称
|
||||||
|
//String fileName = "C:\\Users\\lenovo\\Desktop\\图片工具" + "\\" + clazz.getSimpleName() + ".xlsx";
|
||||||
|
String fileName = "";
|
||||||
|
if (filePath != null && !filePath.isEmpty()) {
|
||||||
|
fileName = filePath + clazz.getSimpleName() + FormatDateTime.getStringDate() + ".xlsx";
|
||||||
|
} else {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
System.out.println("Excel文件: " + clazz.getSimpleName() + ".xlsx 开始生成");
|
||||||
|
// 创建一个输出流,将文件输出
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
|
try {
|
||||||
|
outputStream = new FileOutputStream(fileName);
|
||||||
|
// EasyExcel.write(outputStream) 为了使用流将数据导出
|
||||||
|
ExcelWriter writer = EasyExcel.write(outputStream).build();
|
||||||
|
// writerSheet 中 sheetNo 表示导出的是 Excel 中第几页数据,sheetName 表示导出的该页数据名称
|
||||||
|
// head 表示 Excel 数据需要映射的类
|
||||||
|
WriteSheet sheet = EasyExcel.writerSheet(0, "Excel表第0页的名称")
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteWidthConfig()) /*自适应列宽*/
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteHeightConfig()) /*自适应行高(根据自己情况选择使用,我这里没用到)*/
|
||||||
|
|
||||||
|
.head(clazz)//根据实体类设置表头
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 开始写入数据
|
||||||
|
writer.write(dateList, sheet);
|
||||||
|
// 刷新数据
|
||||||
|
writer.finish();
|
||||||
|
outputStream.flush();
|
||||||
|
System.out.println("Excel文件生成成功 ---> 生成位置: " + fileName);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (outputStream != null) {
|
||||||
|
// 关闭流
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出 Excel 文件, 不带表头的
|
||||||
|
*
|
||||||
|
* @param dateList 想要导出的数据列表
|
||||||
|
* @param clazz 待导出数据的类型
|
||||||
|
* @param filePath 导出后的excel文件的存储路径
|
||||||
|
* @param <T> 类型
|
||||||
|
*/
|
||||||
|
public static <T> void outPutExcelUnHear(List<T> dateList, Class<T> clazz, String filePath) {
|
||||||
|
|
||||||
|
// 默认导出地址 obj.getClass().getName() 使用反射获取类的名称
|
||||||
|
//String fileName = "C:\\Users\\lenovo\\Desktop\\图片工具" + "\\" + clazz.getSimpleName() + ".xlsx";
|
||||||
|
String fileName = "";
|
||||||
|
if (filePath != null && !filePath.isEmpty()) {
|
||||||
|
fileName = filePath + clazz.getSimpleName() + ".xlsx";
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Excel文件: " + clazz.getSimpleName() + ".xlsx 开始生成");
|
||||||
|
|
||||||
|
// 创建一个输出流,将文件输出
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
outputStream = new FileOutputStream(fileName);
|
||||||
|
// EasyExcel.write(outputStream) 为了使用流将数据导出
|
||||||
|
ExcelWriter writer = EasyExcel.write(outputStream).build();
|
||||||
|
// writerSheet 中 sheetNo 表示导出的是 Excel 中第几页数据,sheetName 表示导出的该页数据名称
|
||||||
|
// head 表示 Excel 数据需要映射的类
|
||||||
|
WriteSheet sheet = EasyExcel.writerSheet(0, "Excel表第0页的名称")
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteWidthConfig()) /*自适应列宽*/
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteHeightConfig()) /*自适应行高(根据自己情况选择使用,我这里没用到)*/
|
||||||
|
//.head(clazz)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 开始写入数据
|
||||||
|
writer.write(dateList, sheet);
|
||||||
|
// 刷新数据
|
||||||
|
writer.finish();
|
||||||
|
outputStream.flush();
|
||||||
|
System.out.println("Excel文件生成成功 ---> 生成位置: " + fileName);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (outputStream != null) {
|
||||||
|
// 关闭流
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param dateList 数据
|
||||||
|
* @param clazz 类型
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @param <T> 类型
|
||||||
|
* @return 文件名(全路径)
|
||||||
|
*/
|
||||||
|
public static <T> String outPutExcelUnHearReturnFileName(List<T> dateList, Class<T> clazz, String filePath) {
|
||||||
|
|
||||||
|
// 默认导出地址 obj.getClass().getName() 使用反射获取类的名称
|
||||||
|
String fileName = "";
|
||||||
|
if (filePath != null && !filePath.isEmpty()) {
|
||||||
|
fileName = filePath + clazz.getSimpleName() + FormatDateTime.getStringDate() + ".xlsx";
|
||||||
|
} else {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Excel文件: " + clazz.getSimpleName() + ".xlsx 开始生成");
|
||||||
|
|
||||||
|
// 创建一个输出流,将文件输出
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
outputStream = new FileOutputStream(fileName);
|
||||||
|
// EasyExcel.write(outputStream) 为了使用流将数据导出
|
||||||
|
ExcelWriter writer = EasyExcel.write(outputStream).build();
|
||||||
|
// writerSheet 中 sheetNo 表示导出的是 Excel 中第几页数据,sheetName 表示导出的该页数据名称
|
||||||
|
// head 表示 Excel 数据需要映射的类
|
||||||
|
WriteSheet sheet = EasyExcel.writerSheet(0, "Excel表第0页的名称")
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteWidthConfig()) /*自适应列宽*/
|
||||||
|
.registerWriteHandler(new ExcelCustomCellWriteHeightConfig()) /*自适应行高(根据自己情况选择使用,我这里没用到)*/
|
||||||
|
//.head(clazz)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 开始写入数据
|
||||||
|
writer.write(dateList, sheet);
|
||||||
|
// 刷新数据
|
||||||
|
writer.finish();
|
||||||
|
outputStream.flush();
|
||||||
|
System.out.println("Excel文件生成成功 ---> 生成位置: " + fileName);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (outputStream != null) {
|
||||||
|
// 关闭流
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,223 @@
|
||||||
|
package com.haitongauto.utils.http;
|
||||||
|
|
||||||
|
|
||||||
|
import okhttp3.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class OkHttpUtils {
|
||||||
|
|
||||||
|
// 创建OkHttpClient对象
|
||||||
|
private static final OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(30, TimeUnit.SECONDS) // 连接超时时间
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS) // 读取超时时间
|
||||||
|
.writeTimeout(30, TimeUnit.SECONDS) // 写入超时时间
|
||||||
|
.build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET请求
|
||||||
|
* @param url 请求URL
|
||||||
|
* @return 响应体字符串
|
||||||
|
* @throws IOException 请求或响应过程中发生的错误
|
||||||
|
*/
|
||||||
|
public static String get(String url) throws IOException {
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.build();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
return response.body().string();
|
||||||
|
} else {
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String get(String url, Map<String, String> headers) throws IOException {
|
||||||
|
Request.Builder builder = new Request.Builder()
|
||||||
|
.url(url);
|
||||||
|
if (headers != null && headers.size() > 0) {
|
||||||
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
|
builder.addHeader(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Request request = builder.build();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
return response.body().string();
|
||||||
|
} else {
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST请求
|
||||||
|
* @param url 请求URL
|
||||||
|
* @param requestBody 请求体
|
||||||
|
* @param headers 请求头
|
||||||
|
* @return 响应体字符串
|
||||||
|
* @throws IOException 请求或响应过程中发生的错误
|
||||||
|
*/
|
||||||
|
public static String post(String url, RequestBody requestBody, Map<String, String> headers) throws IOException {
|
||||||
|
Request.Builder builder = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(requestBody);
|
||||||
|
if (headers != null && headers.size() > 0) {
|
||||||
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
|
builder.addHeader(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Request request = builder.build();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
return response.body().string();
|
||||||
|
} else {
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造JSON请求体
|
||||||
|
* @param jsonStr JSON字符串
|
||||||
|
* @return JSON请求体
|
||||||
|
*/
|
||||||
|
public static RequestBody buildJsonRequestBody(String jsonStr) {
|
||||||
|
return RequestBody.create(MediaType.parse("application/json"), jsonStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造表单请求体
|
||||||
|
* @param formParams 表单参数
|
||||||
|
* @return 表单请求体
|
||||||
|
*/
|
||||||
|
public static RequestBody buildFormRequestBody(Map<String, String> formParams) {
|
||||||
|
FormBody.Builder builder = new FormBody.Builder();
|
||||||
|
if (formParams != null && formParams.size() > 0) {
|
||||||
|
for (Map.Entry<String, String> entry : formParams.entrySet()) {
|
||||||
|
builder.add(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造Multipart请求体
|
||||||
|
* @param multipartParams Multipart参数
|
||||||
|
* @return Multipart请求体
|
||||||
|
*/
|
||||||
|
public static RequestBody buildMultipartRequestBody(Map<String, Object> multipartParams) {
|
||||||
|
MultipartBody.Builder builder = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM);
|
||||||
|
if (multipartParams != null && multipartParams.size() > 0) {
|
||||||
|
for (Map.Entry<String, Object> entry : multipartParams.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
Object value = entry.getValue();
|
||||||
|
if (value instanceof String) {
|
||||||
|
builder.addFormDataPart(key, (String) value);
|
||||||
|
} else if (value instanceof byte[]) {
|
||||||
|
builder.addFormDataPart(key, null,
|
||||||
|
RequestBody.create(MediaType.parse("application/octet-stream"), (byte[]) value));
|
||||||
|
} else if (value instanceof RequestBody) {
|
||||||
|
builder.addFormDataPart(key, null, (RequestBody) value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造Multipart请求体,支持上传文件
|
||||||
|
* @param multipartParams Multipart参数
|
||||||
|
* @return Multipart请求体
|
||||||
|
*/
|
||||||
|
public static RequestBody buildMultipartRequestBodyWithFiles(Map<String, Object> multipartParams) {
|
||||||
|
MultipartBody.Builder builder = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM);
|
||||||
|
if (multipartParams != null && multipartParams.size() > 0) {
|
||||||
|
for (Map.Entry<String, Object> entry : multipartParams.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
Object value = entry.getValue();
|
||||||
|
if (value instanceof String) {
|
||||||
|
builder.addFormDataPart(key, (String) value);
|
||||||
|
} else if (value instanceof byte[]) {
|
||||||
|
builder.addFormDataPart(key, null,
|
||||||
|
RequestBody.create(MediaType.parse("application/octet-stream"), (byte[]) value));
|
||||||
|
} else if (value instanceof RequestBody) {
|
||||||
|
builder.addFormDataPart(key, null, (RequestBody) value);
|
||||||
|
} else if (value instanceof UploadFile) { // 支持上传文件
|
||||||
|
UploadFile file = (UploadFile) value;
|
||||||
|
builder.addFormDataPart(key, file.getName(),
|
||||||
|
RequestBody.create(MediaType.parse(file.getMimeType()), file.getFile()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param url 请求URL
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @param headers 请求头
|
||||||
|
* @return 响应体字符串
|
||||||
|
* @throws IOException 请求或响应过程中发生的错误
|
||||||
|
*/
|
||||||
|
public static String uploadFile(String url, UploadFile file, Map<String, String> headers) throws IOException {
|
||||||
|
RequestBody requestBody = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("file", file.getName(),
|
||||||
|
RequestBody.create(MediaType.parse(file.getMimeType()), file.getFile()))
|
||||||
|
.build();
|
||||||
|
Request.Builder builder = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.post(requestBody);
|
||||||
|
if (headers != null && headers.size() > 0) {
|
||||||
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
|
builder.addHeader(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Request request = builder.build();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
return response.body().string();
|
||||||
|
} else {
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装文件上传参数
|
||||||
|
*/
|
||||||
|
public static class UploadFile {
|
||||||
|
private final String name;
|
||||||
|
private final String mimeType;
|
||||||
|
private final byte[] file;
|
||||||
|
|
||||||
|
public UploadFile(String name, String mimeType, byte[] file) {
|
||||||
|
this.name = name;
|
||||||
|
this.mimeType = mimeType;
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMimeType() {
|
||||||
|
return mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getFile() {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.haitongauto.utils.minio;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class MinioProp {
|
||||||
|
/**
|
||||||
|
* 连接url
|
||||||
|
*/
|
||||||
|
private String endpoint;
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private String accesskey;
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private String secretKey;
|
||||||
|
/**
|
||||||
|
* 桶名称
|
||||||
|
*/
|
||||||
|
private String bucket;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,113 @@
|
||||||
|
package com.haitongauto.utils.minio;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import io.minio.errors.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MinioUtils {
|
||||||
|
|
||||||
|
public MinioUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MinioClient client;
|
||||||
|
|
||||||
|
private static MinioProp minioProp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建bucket
|
||||||
|
*
|
||||||
|
* @param bucketName bucket名称
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void createBucket(String bucketName) throws InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException, NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, InvalidResponseException, InternalException, RegionConflictException {
|
||||||
|
if (!client.bucketExists(bucketName)) {
|
||||||
|
client.makeBucket(bucketName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param file 文件
|
||||||
|
* @return 返回结果
|
||||||
|
*/
|
||||||
|
public static String uploadFile(MultipartFile file) throws InvalidPortException, InvalidEndpointException {
|
||||||
|
|
||||||
|
// 判断上传文件是否为空
|
||||||
|
if (null == file || 0 == file.getSize()) {
|
||||||
|
try {
|
||||||
|
throw new Exception("上传文件不能为空");
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String fileUrl = "";
|
||||||
|
//minio服务配置
|
||||||
|
if (minioProp == null||minioProp.getEndpoint()==null||minioProp.getEndpoint().equals("")) {
|
||||||
|
minioProp = ymlReader();
|
||||||
|
}
|
||||||
|
//minio客户端访问对象
|
||||||
|
if (client == null) {
|
||||||
|
client = new MinioClient(minioProp.getEndpoint());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 判断存储桶是否存在,如果不存在创建桶
|
||||||
|
createBucket(minioProp.getBucket());
|
||||||
|
// 文件名
|
||||||
|
String originalFilename = file.getOriginalFilename();
|
||||||
|
// 新的文件名 = 存储桶名称_时间戳+【10000-99999】随机数字.后缀名
|
||||||
|
//Random random = new Random();
|
||||||
|
//String fileName = minioProp.getBucket() + "_" + System.currentTimeMillis() + (random.nextInt(99999 - 10000) + 10000 + 1) + originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||||
|
String fileName = "img_" + file.getOriginalFilename();
|
||||||
|
// 开始上传
|
||||||
|
client.putObject(minioProp.getBucket(), fileName, file.getInputStream(), file.getContentType());
|
||||||
|
fileUrl = minioProp.getEndpoint() + "/" + minioProp.getBucket() + "/" + fileName;
|
||||||
|
return fileUrl;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
fileUrl="";
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return 返回配置
|
||||||
|
*/
|
||||||
|
public static MinioProp ymlReader() {
|
||||||
|
MinioProp minioProp = new MinioProp();
|
||||||
|
try {
|
||||||
|
Yaml yaml = new Yaml();
|
||||||
|
final File initialFile = new File("utils/src/main/resources/utils.yml");
|
||||||
|
final InputStream in = new DataInputStream(Files.newInputStream(initialFile.toPath()));
|
||||||
|
Map<String, Object> map = yaml.load(in);
|
||||||
|
Object minio = map.get("minio");
|
||||||
|
if (minio != null && !minio.equals("")) {
|
||||||
|
minioProp = JSON.parseObject(JSON.toJSONString(minio), MinioProp.class);
|
||||||
|
}
|
||||||
|
System.out.println(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return minioProp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="WARN">
|
||||||
|
<Appenders>
|
||||||
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Root level="debug">
|
||||||
|
<AppenderRef ref="Console"/>
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
|
@ -0,0 +1,15 @@
|
||||||
|
# minio 文件存储配置信息 https://rtops4.haitongauto.com/minio/
|
||||||
|
minio:
|
||||||
|
endpoint: http://192.168.61.130:20001
|
||||||
|
accesskey: hfyth
|
||||||
|
secretKey: hfyth2022
|
||||||
|
bucket: rtos-saas-dev
|
||||||
|
|
||||||
|
# minio 文件存储配置信息(本地)
|
||||||
|
#minio:
|
||||||
|
# endpoint: http://192.168.0.16:9000
|
||||||
|
# accesskey: admin
|
||||||
|
# secretKey: admin@123
|
||||||
|
# bucket: rtos-saas-dev
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
import com.haitongauto.utils.QueueNumberHelper;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class QueueNumTest {
|
||||||
|
@Test
|
||||||
|
public void geQueueNum() {
|
||||||
|
String queueNum = QueueNumberHelper.getNewQueueNumber("2308211003");
|
||||||
|
String aaa="";
|
||||||
|
|
||||||
|
|
||||||
|
// AppointForward appointForward=new AppointForward();
|
||||||
|
// appointForward.setId("1");
|
||||||
|
// appointForward.setOpenId("oBFJt5KzWJPJHHRpYPEOWvTUvqjA");
|
||||||
|
// appointForward.setTruckNo("沪A88888");
|
||||||
|
// appointForward.setOrderTm("2024-1-1 10:20:00");
|
||||||
|
// appointForward.setOrderType("外贸出口");
|
||||||
|
// appointForward.setWhafType("外高桥");
|
||||||
|
// appointForward.setGodType("商品车");
|
||||||
|
// appointForward.setGodNum(2);
|
||||||
|
// appointForward.setPhone("13118057744");
|
||||||
|
// List<VinDetail> dataList=new ArrayList<>();
|
||||||
|
// VinDetail vinDetail1=new VinDetail();
|
||||||
|
// vinDetail1.setVin("NSTOOS00000000001");
|
||||||
|
// vinDetail1.setPotNm("达曼");
|
||||||
|
// vinDetail1.setVlsNm("阿拉伯海");
|
||||||
|
// vinDetail1.setBrdNm("江淮");
|
||||||
|
// dataList.add(vinDetail1);
|
||||||
|
// VinDetail vinDetail2=new VinDetail();
|
||||||
|
// vinDetail2.setVin("NSTOOS00000000002");
|
||||||
|
// vinDetail2.setPotNm("达曼");
|
||||||
|
// vinDetail2.setVlsNm("阿拉伯海");
|
||||||
|
// vinDetail2.setBrdNm("江淮");
|
||||||
|
// dataList.add(vinDetail2);
|
||||||
|
// appointForward.setDataList(dataList);
|
||||||
|
//
|
||||||
|
// String res= HttpClientHelper.forwardSendNew(appointForward,"http://192.168.161.81:8090/rtopswebapi/api/NsOrder/execTruckOrder");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module version="4">
|
||||||
|
<component name="SonarLintModuleSettings">
|
||||||
|
<option name="uniqueId" value="a9e3ceb0-5a91-4ccf-9c26-4cc1b72d28ff" />
|
||||||
|
</component>
|
||||||
|
</module>
|
Loading…
Reference in New Issue