1.HttpUtils
JSONObject httpPost(String url, JSONObject jsonParam)
JSONObject httpPost(String url, String strParam)
JSONObject httpGet(String url)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.demo.config;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class HttpClientUtils {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class); // 日志记录
private static RequestConfig requestConfig = null;
static {
// 设置请求和传输超时时间
requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
}
/**
* post请求传输json参数
*
* @param url url地址
* @param json 参数
* @return
*/
public static JSONObject httpPost(String url, JSONObject jsonParam) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
httpPost.setConfig(requestConfig);
try {
if (null != jsonParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
try {
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), "utf-8");
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(str);
} catch (Exception e) {
logger.error("post请求提交失败:" + url, e);
}
}
} catch (IOException e) {
logger.error("post请求提交失败:" + url, e);
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}
/**
* post请求传输String参数 例如:name=Jack&sex=1&type=2
* Content-type:application/x-www-form-urlencoded
*
* @param url url地址
* @param strParam 参数
* @return
*/
public static JSONObject httpPost(String url, String strParam) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
try {
if (null != strParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(strParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
try {
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), "utf-8");
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(str);
} catch (Exception e) {
logger.error("post请求提交失败:" + url, e);
}
}
} catch (IOException e) {
logger.error("post请求提交失败:" + url, e);
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}
/**
* 发送get请求
*
* @param url 路径
* @return
*/
public static JSONObject httpGet(String url) {
// get请求返回结果
JSONObject jsonResult = null;
CloseableHttpClient client = HttpClients.createDefault();
// 发送get请求
HttpGet request = new HttpGet(url);
request.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(request);
// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 读取服务器返回过来的json字符串数据
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, "utf-8");
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(strResult);
} else {
logger.error("get请求提交失败:" + url);
}
} catch (IOException e) {
logger.error("get请求提交失败:" + url, e);
} finally {
request.releaseConnection();
}
return jsonResult;
}
}
String doGet(String url, Map<String, String> param)
String doPost(String url, Map<String, String> param)
String doPostJson(String url, String json)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
2.fileUppload
需要验证文件类型,文件内容(图片)
fileUpload(MultipartFile multipartFile)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//图片存放地址
private static final String BASE_URL = "/hdis/weixin/cardtypeimages";
//合法的文件类型
private static final List<String> ALLOW_TYPES = Arrays.asList("image/jpeg", "image/png");
//设置文件名
String name = UUID.randomUUID().toString().replace("-", "");
//获取图片地址
String suffix = "";
int lastIndexOf = multipartFile.getOriginalFilename().lastIndexOf(".");
//获取文件的后缀名 .jpg
suffix = multipartFile.getOriginalFilename().substring(lastIndexOf);
result.setResult(new Result(false, "文件上传失败"));
String imageURL = name + suffix;
//5.检验文件的类型 防止恶意文件
if (!ALLOW_TYPES.contains(contentType)) {
result.setResult(new Result(false, "文件上传失败,文件类型有误"));
//6.校验文件的内容
BufferedImage image =ImageIO.read(multipartFile.getInputStream());
if (image == null) {
result.setResult(new Result(false, "文件上传失败,文件类容有误"));
}
File file = new File(BASE_URL);
if (!file.exists()) {
file.mkdir();
}
//复制文件
multipartFile.transferTo(new File(BASE_URL, imageURL));
3.Java上传和下载(ftp)
<!-- Sftp工具 -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
public class SftpClientUtil
{
private static final Logger logger = Logger.getLogger(SftpClientUtil.class);
/** Sftp */
private ChannelSftp sftp = null;
/** 主机 */
private String host = "";
/** 端口 */
private int port = 0;
/** 用户名 */
private String username = "";
/** 密码 */
private String password = "";
/**
* 构造函数
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
*
*/
public SftpClientUtil(String host, int port, String username, String password)
{
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}
/**
* 连接sftp服务器
*
* @throws Exception
*/
public void connect() throws Exception
{
JSch jsch = new JSch();
Session sshSession = jsch.getSession(this.username, this.host, this.port);
logger.debug(SftpClientUtil.class + "Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect(20000);
logger.debug(SftpClientUtil.class + " Session connected.");
logger.debug(SftpClientUtil.class + " Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
this.sftp = (ChannelSftp) channel;
logger.debug(SftpClientUtil.class + " Connected to " + this.host + ".");
}
/**
* Disconnect with server
*
* @throws Exception
*/
public void disconnect() throws Exception
{
if (this.sftp != null)
{
if (this.sftp.isConnected())
{
this.sftp.disconnect();
} else if (this.sftp.isClosed())
{
logger.debug(SftpClientUtil.class + " sftp is closed already");
}
}
}
/**
* 下载单个文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveDirectory
* 存在本地的路径
*
* @throws Exception
*/
public void download(String directory, String downloadFile, String saveDirectory) throws Exception
{
File pathFile = new File(saveDirectory);
if (!pathFile.exists())
{
pathFile.mkdirs();
}
String saveFile = saveDirectory + "//" + downloadFile;
this.sftp.cd(directory);
File file = new File(saveFile);
this.sftp.get(downloadFile, new FileOutputStream(file));
}
/**
* 下载目录下全部文件
*
* @param directory
* 下载目录
*
* @param saveDirectory
* 存在本地的路径
*
* @throws Exception
*/
public void downloadByDirectory(String directory, String saveDirectory) throws Exception
{
String downloadFile = "";
List<String> downloadFileList = this.listFiles(directory);
Iterator<String> it = downloadFileList.iterator();
while (it.hasNext())
{
downloadFile = it.next().toString();
if (downloadFile.toString().indexOf(".") < 0)
{
continue;
}
this.download(directory, downloadFile, saveDirectory);
}
}
/**
* 新建子目录
*
* @param dst 远程服务器路径
*
* @throws Exception
*/
public void mkdir(String dst, String subDir) throws Exception
{
this.sftp.cd(dst);
try {
if(this.sftp.ls(subDir).size() > 0) {
return;
}
} catch (SftpException se) {
logger.error(se.getMessage());
}
this.sftp.mkdir(subDir);
}
/**
* 上传单个文件
*
* @param src 本地文件
* @param dst 远程服务器路径
*
* @throws Exception
*/
public void upload(String src, String dst) throws Exception
{
this.sftp.put(src, dst);
}
/**
* 列出目录下的文件
*
* @param directory
* 要列出的目录
*
* @return list 文件名列表
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
public List<String> listFiles(String directory) throws Exception
{
Vector fileList;
List<String> fileNameList = new ArrayList<String>();
fileList = this.sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext())
{
String fileName =((LsEntry) it.next()).getFilename();
if (".".equals(fileName) || "..".equals(fileName))
{
continue;
}
fileNameList.add(fileName);
}
return fileNameList;
}
public ChannelSftp getSftp()
{
return sftp;
}
public void setSftp(ChannelSftp sftp)
{
this.sftp = sftp;
}
}
4.Java数字转人民币大写
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.math.BigDecimal;
/**
* 数字转换为人民币的大写
* NumberToCN.java
*
* @author lmg
* @since 2015年11月3日
* Copyright © mizhuanglicai
*/
public class NumberToCN {
/**
* 汉语中数字大写
*/
private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖" };
/**
* 汉语中货币单位大写,这样的设计类似于占位符
*/
private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
"拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
"佰", "仟" };
/**
* 特殊字符:整
*/
private static final String CN_FULL = "整";
/**
* 特殊字符:负
*/
private static final String CN_NEGATIVE = "负";
/**
* 金额的精度,默认值为2
*/
private static final int MONEY_PRECISION = 2;
/**
* 特殊字符:零元整
*/
private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
/**
* 把输入的金额转换为汉语中人民币的大写
*
* @param numberOfMoney
* 输入的金额
* @return 对应的汉语大写
*/
public static String numberToCn(BigDecimal numberOfMoney) {
StringBuffer sb = new StringBuffer();
// -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
// positive.
int signum = numberOfMoney.signum();
// 零元整的情况
if (signum == 0) {
return CN_ZEOR_FULL;
}
//这里会进行金额的四舍五入
long number = numberOfMoney.movePointRight(MONEY_PRECISION)
.setScale(0, 4).abs().longValue();
// 得到小数点后两位值
long scale = number % 100;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
// 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
if (!(scale > 0)) {
numIndex = 2;
number = number / 100;
getZero = true;
}
if ((scale > 0) && (!(scale % 10 > 0))) {
numIndex = 1;
number = number / 10;
getZero = true;
}
int zeroSize = 0;
while (true) {
if (number <= 0) {
break;
}
// 每次获取到最后一个数
numUnit = (int) (number % 10);
if (numUnit > 0) {
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
} else {
++zeroSize;
if (!(getZero)) {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
if (numIndex == 2) {
if (number > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
getZero = true;
}
// 让number每次都去掉最后一个数
number = number / 10;
++numIndex;
}
// 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
// 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
if (!(scale > 0)) {
sb.append(CN_FULL);
}
return sb.toString();
}
}
5.Java身份证号码合法性校验与解析
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.apache.commons.lang3.StringUtils.replace;
import static org.apache.commons.lang3.StringUtils.substring;
import static org.apache.commons.lang3.time.DateFormatUtils.ISO_DATE_FORMAT;
/**
* 身份证工具类
* 验证18位身份号码是否合法
* 验证15位身份号码是否合法
* 验证台湾身份证号码是否合法
* 验证香港身份证号码是否合法
* 解析身份证号获取户籍城市编码,出生日期,性别
*/
public class IdcardUtils {
/** 中国公民身份证号码最小长度。 */
public static final int CHINA_ID_MIN_LENGTH = 15;
/** 中国公民身份证号码最大长度。 */
public static final int CHINA_ID_MAX_LENGTH = 18;
/** 省、直辖市代码表 */
public static final String cityCode[] = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50",
"51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91" };
/** 每位加权因子 */
public static final int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
/** 第18位校检码 */
public static final String verifyCode[] = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
/** 最低年限 */
public static final int MIN = 1900;
public static Map<String, String> cityCodes = new HashMap<String, String>();
/** 台湾身份首字母对应数字 */
public static Map<String, Integer> twFirstCode = new HashMap<String, Integer>();
/** 香港身份首字母对应数字 */
public static Map<String, Integer> hkFirstCode = new HashMap<String, Integer>();
static {
cityCodes.put("11", "北京");
cityCodes.put("12", "天津");
cityCodes.put("13", "河北");
cityCodes.put("14", "山西");
cityCodes.put("15", "内蒙古");
cityCodes.put("21", "辽宁");
cityCodes.put("22", "吉林");
cityCodes.put("23", "黑龙江");
cityCodes.put("31", "上海");
cityCodes.put("32", "江苏");
cityCodes.put("33", "浙江");
cityCodes.put("34", "安徽");
cityCodes.put("35", "福建");
cityCodes.put("36", "江西");
cityCodes.put("37", "山东");
cityCodes.put("41", "河南");
cityCodes.put("42", "湖北");
cityCodes.put("43", "湖南");
cityCodes.put("44", "广东");
cityCodes.put("45", "广西");
cityCodes.put("46", "海南");
cityCodes.put("50", "重庆");
cityCodes.put("51", "四川");
cityCodes.put("52", "贵州");
cityCodes.put("53", "云南");
cityCodes.put("54", "西藏");
cityCodes.put("61", "陕西");
cityCodes.put("62", "甘肃");
cityCodes.put("63", "青海");
cityCodes.put("64", "宁夏");
cityCodes.put("65", "新疆");
cityCodes.put("71", "台湾");
cityCodes.put("81", "香港");
cityCodes.put("82", "澳门");
cityCodes.put("91", "国外");
twFirstCode.put("A", 10);
twFirstCode.put("B", 11);
twFirstCode.put("C", 12);
twFirstCode.put("D", 13);
twFirstCode.put("E", 14);
twFirstCode.put("F", 15);
twFirstCode.put("G", 16);
twFirstCode.put("H", 17);
twFirstCode.put("J", 18);
twFirstCode.put("K", 19);
twFirstCode.put("L", 20);
twFirstCode.put("M", 21);
twFirstCode.put("N", 22);
twFirstCode.put("P", 23);
twFirstCode.put("Q", 24);
twFirstCode.put("R", 25);
twFirstCode.put("S", 26);
twFirstCode.put("T", 27);
twFirstCode.put("U", 28);
twFirstCode.put("V", 29);
twFirstCode.put("X", 30);
twFirstCode.put("Y", 31);
twFirstCode.put("W", 32);
twFirstCode.put("Z", 33);
twFirstCode.put("I", 34);
twFirstCode.put("O", 35);
hkFirstCode.put("A", 1);
hkFirstCode.put("B", 2);
hkFirstCode.put("C", 3);
hkFirstCode.put("R", 18);
hkFirstCode.put("U", 21);
hkFirstCode.put("Z", 26);
hkFirstCode.put("X", 24);
hkFirstCode.put("W", 23);
hkFirstCode.put("O", 15);
hkFirstCode.put("N", 14);
}
/**
* 将15位身份证号码转换为18位
*
* @param idCard
* 15位身份编码
* @return 18位身份编码
*/
private static String conver15CardTo18(String idCard) {
String idCard18 = "";
if (idCard.length() != CHINA_ID_MIN_LENGTH) {
return null;
}
if (isNum(idCard)) {
// 获取出生年月日
String birthday = idCard.substring(6, 12);
Date birthDate = null;
try {
birthDate = new SimpleDateFormat("yyMMdd").parse(birthday);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (birthDate != null)
cal.setTime(birthDate);
// 获取出生年(完全表现形式,如:2010)
String sYear = String.valueOf(cal.get(Calendar.YEAR));
idCard18 = idCard.substring(0, 6) + sYear + idCard.substring(8);
// 转换字符数组
char[] cArr = idCard18.toCharArray();
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
int iSum17 = getPowerSum(iCard);
// 获取校验位
String sVal = getCheckCode18(iSum17);
if (sVal.length() > 0) {
idCard18 += sVal;
} else {
return null;
}
}
} else {
return null;
}
return idCard18;
}
/**
* 验证身份证是否合法
*/
public static boolean validateCard(String idCard) {
String card = idCard.trim();
if (validateIdCard18(card)) {
return true;
}
if (validateIdCard15(card)) {
return true;
}
String[] cardval = validateIdCard10(card);
if (cardval != null) {
if ("true".equals(cardval[2])) {
return true;
}
}
return false;
}
/**
* 验证18位身份编码是否合法
*
* @param idCard
* 身份编码
* @return 是否合法
*/
public static boolean validateIdCard18(String idCard) {
boolean bTrue = false;
if (idCard.length() == CHINA_ID_MAX_LENGTH) {
// 前17位
String code17 = idCard.substring(0, 17);
// 第18位
String code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH);
if (isNum(code17)) {
char[] cArr = code17.toCharArray();
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
int iSum17 = getPowerSum(iCard);
// 获取校验位
String val = getCheckCode18(iSum17);
if (val.length() > 0) {
if (val.equalsIgnoreCase(code18)) {
bTrue = true;
}
}
}
}
}
return bTrue;
}
/**
* 验证15位身份编码是否合法
*
* @param idCard
* 身份编码
* @return 是否合法
*/
public static boolean validateIdCard15(String idCard) {
if (idCard.length() != CHINA_ID_MIN_LENGTH) {
return false;
}
if (isNum(idCard)) {
String proCode = idCard.substring(0, 2);
if (cityCodes.get(proCode) == null) {
return false;
}
String birthCode = idCard.substring(6, 12);
Date birthDate = null;
try {
birthDate = new SimpleDateFormat("yy").parse(birthCode.substring(0, 2));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (birthDate != null)
cal.setTime(birthDate);
if (!valiDate(cal.get(Calendar.YEAR), Integer.valueOf(birthCode.substring(2, 4)), Integer.valueOf(birthCode.substring(4, 6)))) {
return false;
}
} else {
return false;
}
return true;
}
/**
* 验证10位身份编码是否合法
*
* @param idCard
* 身份编码
* @return 身份证信息数组
* <p>
* [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false)
* 若不是身份证件号码则返回null
* </p>
*/
public static String[] validateIdCard10(String idCard) {
String[] info = new String[3];
String card = idCard.replaceAll("[\\(|\\)]", "");
if (card.length() != 8 && card.length() != 9 && idCard.length() != 10) {
return null;
}
if (idCard.matches("^[a-zA-Z][0-9]{9}$")) { // 台湾
info[0] = "台湾";
String char2 = idCard.substring(1, 2);
if (char2.equals("1")) {
info[1] = "M";
} else if (char2.equals("2")) {
info[1] = "F";
} else {
info[1] = "N";
info[2] = "false";
return info;
}
info[2] = validateTWCard(idCard) ? "true" : "false";
} else if (idCard.matches("^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$")) { // 澳门
info[0] = "澳门";
info[1] = "N";
// TODO
} else if (idCard.matches("^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$")) { // 香港
info[0] = "香港";
info[1] = "N";
info[2] = validateHKCard(idCard) ? "true" : "false";
} else {
return null;
}
return info;
}
/**
* 验证台湾身份证号码
*
* @param idCard
* 身份证号码
* @return 验证码是否符合
*/
public static boolean validateTWCard(String idCard) {
String start = idCard.substring(0, 1);
String mid = idCard.substring(1, 9);
String end = idCard.substring(9, 10);
Integer iStart = twFirstCode.get(start);
Integer sum = iStart / 10 + (iStart % 10) * 9;
char[] chars = mid.toCharArray();
Integer iflag = 8;
for (char c : chars) {
sum = sum + Integer.valueOf(c + "") * iflag;
iflag--;
}
return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.valueOf(end) ? true : false;
}
/**
* 验证香港身份证号码
* <p>
* 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35
* 最后一位校验码为0-9的数字加上字符"A","A"代表10
* </p>
* <p>
* 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效
* </p>
*
* @param idCard
* 身份证号码
* @return 验证码是否符合
*/
public static boolean validateHKCard(String idCard) {
String card = idCard.replaceAll("[\\(|\\)]", "");
Integer sum = 0;
if (card.length() == 9) {
sum = (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 9 + (Integer.valueOf(card.substring(1, 2).toUpperCase().toCharArray()[0]) - 55) * 8;
card = card.substring(1, 9);
} else {
sum = 522 + (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 8;
}
String mid = card.substring(1, 7);
String end = card.substring(7, 8);
char[] chars = mid.toCharArray();
Integer iflag = 7;
for (char c : chars) {
sum = sum + Integer.valueOf(c + "") * iflag;
iflag--;
}
if (end.toUpperCase().equals("A")) {
sum = sum + 10;
} else {
sum = sum + Integer.valueOf(end);
}
return (sum % 11 == 0) ? true : false;
}
/**
* 将字符数组转换成数字数组
*
* @param ca
* 字符数组
* @return 数字数组
*/
private static int[] converCharToInt(char[] ca) {
int len = ca.length;
int[] iArr = new int[len];
try {
for (int i = 0; i < len; i++) {
iArr[i] = Integer.parseInt(String.valueOf(ca[i]));
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return iArr;
}
/**
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值
*
* @param iArr
* @return 身份证编码。
*/
private static int getPowerSum(int[] iArr) {
int iSum = 0;
if (power.length == iArr.length) {
for (int i = 0; i < iArr.length; i++) {
for (int j = 0; j < power.length; j++) {
if (i == j) {
iSum = iSum + iArr[i] * power[j];
}
}
}
}
return iSum;
}
/**
* 将power和值与11取模获得余数进行校验码判断
*
* @param iSum
* @return 校验位
*/
private static String getCheckCode18(int iSum) {
String sCode = "";
switch (iSum % 11) {
case 10:
sCode = "2";
break;
case 9:
sCode = "3";
break;
case 8:
sCode = "4";
break;
case 7:
sCode = "5";
break;
case 6:
sCode = "6";
break;
case 5:
sCode = "7";
break;
case 4:
sCode = "8";
break;
case 3:
sCode = "9";
break;
case 2:
sCode = "x";
break;
case 1:
sCode = "0";
break;
case 0:
sCode = "1";
break;
}
return sCode;
}
/**
* 根据身份编号获取年龄
*
* @param idCard
* 身份编号
* @return 年龄
*/
public static int getAgeByIdCard(String idCard) {
int iAge = 0;
if (idCard.length() == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
String year = idCard.substring(6, 10);
Calendar cal = Calendar.getInstance();
int iCurrYear = cal.get(Calendar.YEAR);
iAge = iCurrYear - Integer.valueOf(year);
return iAge;
}
/**
* 根据生日判断是否年满18周岁
*
* @param idCard
* @return
*/
public static boolean is18YearsOld(String idCard) {
int iAge = getAgeByIdCard(idCard);
return iAge > 18 ? true : (iAge < 18 ? false : substring(replace(ISO_DATE_FORMAT.format(Calendar.getInstance()), "-", ""), 4).compareTo(
substring(getBirthByIdCard(idCard), 4)) >= 0);
}
/**
* 根据身份编号获取生日
*
* @param idCard
* 身份编号
* @return 生日(yyyyMMdd)
*/
public static String getBirthByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return idCard.substring(6, 14);
}
/**
* 根据身份编号获取生日年
*
* @param idCard
* 身份编号
* @return 生日(yyyy)
*/
public static Short getYearByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return Short.valueOf(idCard.substring(6, 10));
}
/**
* 根据身份编号获取生日月
*
* @param idCard
* 身份编号
* @return 生日(MM)
*/
public static Short getMonthByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return Short.valueOf(idCard.substring(10, 12));
}
/**
* 根据身份编号获取生日天
*
* @param idCard
* 身份编号
* @return 生日(dd)
*/
public static Short getDateByIdCard(String idCard) {
Integer len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) {
return null;
} else if (len == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
return Short.valueOf(idCard.substring(12, 14));
}
/**
* 根据身份编号获取性别
*
* @param idCard
* 身份编号
* @return 性别(M-男,F-女,N-未知)
*/
public static String getGenderByIdCard(String idCard) {
String sGender = "N";
if (idCard.length() == CHINA_ID_MIN_LENGTH) {
idCard = conver15CardTo18(idCard);
}
String sCardNum = idCard.substring(16, 17);
if (Integer.parseInt(sCardNum) % 2 != 0) {
sGender = "男";
} else {
sGender = "女";
}
return sGender;
}
/**
* 根据身份编号获取户籍省份
*
* @param idCard
* 身份编码
* @return 省级编码。
*/
public static String getProvinceByIdCard(String idCard) {
int len = idCard.length();
String sProvince = null;
String sProvinNum = "";
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
sProvinNum = idCard.substring(0, 2);
}
sProvince = cityCodes.get(sProvinNum);
return sProvince;
}
/**
* 根据身份编号获取户籍城市编码
*
* @param idCard
* 身份编码
* @return 城市编码。
*/
public static String getCitycodeByIdCard(String idCard) {
return idCard.substring(0, 6);
}
/**
* 数字验证
*
* @param val
* @return 提取的数字。
*/
private static boolean isNum(String val) {
return val == null || "".equals(val) ? false : val.matches("^[0-9]*$");
}
/**
* 验证小于当前日期 是否有效
*
* @param iYear
* 待验证日期(年)
* @param iMonth
* 待验证日期(月 1-12)
* @param iDate
* 待验证日期(日)
* @return 是否有效
*/
private static boolean valiDate(int iYear, int iMonth, int iDate) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int datePerMonth;
if (iYear < MIN || iYear >= year) {
return false;
}
if (iMonth < 1 || iMonth > 12) {
return false;
}
switch (iMonth) {
case 4:
case 6:
case 9:
case 11:
datePerMonth = 30;
break;
case 2:
boolean dm = ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0)) && (iYear > MIN && iYear < year);
datePerMonth = dm ? 29 : 28;
break;
default:
datePerMonth = 31;
}
return (iDate >= 1) && (iDate <= datePerMonth);
}
}
6.Java加解密签名和验签(RSA)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.ByteArrayOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class TestRSA {
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 64;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 75;
/**
* 获取密钥对
*
* @return 密钥对
*/
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(512);
return generator.generateKeyPair();
}
/**
* 获取私钥
*
* @param privateKey 私钥字符串
* @return
*/
public static PrivateKey getPrivateKey(String privateKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
return keyFactory.generatePrivate(keySpec);
}
/**
* 获取公钥
*
* @param publicKey 公钥字符串
* @return
*/
public static PublicKey getPublicKey(String publicKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
return keyFactory.generatePublic(keySpec);
}
/**
* RSA加密
*
* @param data 待加密数据
* @param publicKey 公钥
* @return
*/
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int inputLen = data.getBytes().length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
// 加密后的字符串
return new String(Base64.encodeBase64String(encryptedData));
}
/**
* RSA解密
*
* @param data 待解密数据
* @param privateKey 私钥
* @return
*/
public static String decrypt(String data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] dataBytes = Base64.decodeBase64(data);
int inputLen = dataBytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
// 解密后的内容
return new String(decryptedData, "UTF-8");
}
/**
* 签名
*
* @param data 待签名数据
* @param privateKey 私钥
* @return 签名
*/
public static String sign(String data, PrivateKey privateKey) throws Exception {
byte[] keyBytes = privateKey.getEncoded();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(keySpec);
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(key);
signature.update(data.getBytes());
return new String(Base64.encodeBase64(signature.sign()));
}
/**
* 验签
*
* @param srcData 原始字符串
* @param publicKey 公钥
* @param sign 签名
* @return 是否验签通过
*/
public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
byte[] keyBytes = publicKey.getEncoded();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance("MD5withRSA");
signature.initVerify(key);
signature.update(srcData.getBytes());
return signature.verify(Base64.decodeBase64(sign.getBytes()));
}
}
7.SendEmail
EmailInfo
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sdx.merak.web.mobile.util;
public class EmailInfo {
private String smtpServer;// SMTP服务器地址
private String port; // 端口
private String fromUser;// 登录SMTP服务器的用户名,发送人邮箱地址
private String fromUserPassword;// 登录SMTP服务器的密码
private String toUser; // 收件人
private String subject; // 邮件主题
private String content; // 邮件正文
public EmailInfo(String toUser, String subject, String content) {
this.toUser = toUser;
this.subject = subject;
this.content = content;
this.smtpServer = "smtp.tianxiapass.com";//选择自己的服务器
this.port = "25";
this.fromUser = "";
this.fromUserPassword = "";
}
public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getFromUser() {
return fromUser;
}
public void setFromUser(String fromUser) {
this.fromUser = fromUser;
}
public String getFromUserPassword() {
return fromUserPassword;
}
public void setFromUserPassword(String fromUserPassword) {
this.fromUserPassword = fromUserPassword;
}
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
SendEmailUtils
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class EmailUtils {
public static void sendMail(EmailInfo emailInfo) throws Exception {
Properties prop = new Properties();
prop.put("mail.host", "smtp.mxhichina.com");
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
session.setDebug(true);
Transport ts = session.getTransport();
ts.connect(emailInfo.getFromUser(), emailInfo.getFromUserPassword());
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailInfo.getFromUser()));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailInfo.getToUser()));
message.setSubject(emailInfo.getSubject());
message.setContent(emailInfo.getContent(), "text/html;charset=utf-8");
ts.sendMessage(message, message.getAllRecipients());
}
}
8.AES可逆加解密
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sdx.merak.web.mobile.util;
import org.apache.shiro.codec.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES 加密
*/
public class AesEncryptUtil {
//使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
private static String KEY = "xj0429";
private static String IV = "xj0429";
/**
* 加密方法
*
* @param data 要加密的数据
* @param key 加密key
* @param iv 加密iv
* @return 加密的结果
* @throws Exception
*/
public static String encrypt(String data, String key, String iv) throws Exception {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"NoPadding PkcsPadding
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return Base64.encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解密方法
*
* @param data 要解密的数据
* @param key 解密key
* @param iv 解密iv
* @return 解密的结果
* @throws Exception
*/
public static String desEncrypt(String data, String key, String iv) throws Exception {
try {
byte[] encrypted1 = Base64.decode(data);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 使用默认的key和iv加密
*
* @param data
* @return
* @throws Exception
*/
public static String encrypt(String data) throws Exception {
return encrypt(data, KEY, IV);
}
/**
* 使用默认的key和iv解密
*
* @param data
* @return
* @throws Exception
*/
public static String desEncrypt(String data) throws Exception {
return desEncrypt(data, KEY, IV);
}
}
9.MD5前后端加密验签
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.sdx.merak.web.mobile.util;
/**
* md5加密
*/
import org.apache.commons.codec.digest.DigestUtils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* md5加密
*/
public class MD5Utils {
private static MessageDigest mdigest = null;
private static char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static String kes = "xj0429";
private static MessageDigest getMdInst() {
if (null == mdigest) {
try {
mdigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return mdigest;
}
public static String encode(String s) {
if (null == s) {
return "";
}
try {
byte[] bytes = s.getBytes();
getMdInst().update(bytes);
byte[] md = getMdInst().digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = digits[byte0 >>> 4 & 0xf];
str[k++] = digits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @description: 1.参数使用ascll排序
* 2.排序后使用&拼接成字符串
* 3.在尾部追加key
* 4.使用MD5对拼接的字符串加密生成sign
* 5.将参数和sign传给后台
* @author:hdh
* @date:2020/8/10 13:53
**/
public static String appletsEncode(Map map) {
TreeMap<String, Object> treeMap = new TreeMap<>();
treeMap.putAll(map);
String message = "";
Set<String> key = treeMap.keySet();
for (String s : key) {
message = message + s + "=" + map.get(s) + "&";
}
message = message + "key=" + kes;
String md5Hex = DigestUtils.md5Hex(message);
return md5Hex;
}
}
10.压缩文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//Version 5 使用Pip
publicstaticvoid
zipFilePip() {
long
beginTime =
System
.currentTimeMillis();
try
(
WritableByteChannel
out
=
Channels
.newChannel(
new
FileOutputStream
(ZIP_FILE))) {
Pipe
pipe =
Pipe
.open();
//异步任务
CompletableFuture
.runAsync(()->runTask(pipe));
//获取读通道
ReadableByteChannel
readableByteChannel = pipe.source();
ByteBuffer
buffer =
ByteBuffer
.allocate(((
int
) FILE_SIZE)*
10
);
while
(readableByteChannel.read(buffer)>=
0
) {
buffer.flip();
out
.write(buffer);
buffer.clear();
}
}
catch
(
Exception
e){
e.printStackTrace();
}
printInfo(beginTime);
}
//异步任务
publicstaticvoid
runTask(
Pipe
pipe) {
try
(
ZipOutputStream
zos =
new
ZipOutputStream
(
Channels
.newOutputStream(pipe.sink()));
WritableByteChannel
out
=
Channels
.newChannel(zos)) {
System
.
out
.println(
Begin
);
for
(
int
i =
0
; i <
10
; i++) {
zos.putNextEntry(
new
ZipEntry
(i+SUFFIX_FILE));
FileChannel
jpgChannel =
new
FileInputStream
(
new
File
(JPG_FILE_PATH)).getChannel();
jpgChannel.transferTo(
0
, FILE_SIZE,
out
);
jpgChannel.close();
}
}
catch
(
Exception
e){
e.printStackTrace();
}
}
11.poi读取excel和写入excel (需要自己修改下/狗头)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.demo.config;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ExcelUtil {
/**
* 写Xls格式Excel文件
*
* @param path
* @throws Exception
*/
public static void printXls(String path) throws Exception {
try {
// 创建输出流
FileOutputStream out = new FileOutputStream(path);
// 创建Workbook
HSSFWorkbook wb = new HSSFWorkbook();
// 创建Sheet
HSSFSheet sheet = wb.createSheet();
// start 内容写入
HSSFRow row = sheet.createRow(0); // 指定列号
HSSFCell cell = row.createCell(0); // 指定行号
HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");
cell.setCellValue(richText);
// 指定行号
HSSFCell enCell = row.createCell(3);
enCell.setCellValue(new HSSFRichTextString("English font test"));
// 设置列的宽度
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
// end 内容写入
// 创建PrintSetup
HSSFPrintSetup printSetup = sheet.getPrintSetup();
// A4纸
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
wb.write(out);
out.close();
} catch (Exception e) {
throw e;
}
}
/**
* 写Xlsx格式Excel文件
*
* @param path
* @throws Exception
*/
public static void printXlsx(String path) throws Exception {
try {
// 创建输出流
FileOutputStream out = new FileOutputStream(path);
// 创建Workbook
XSSFWorkbook wb = new XSSFWorkbook();
// 创建Sheet
XSSFSheet sheet = wb.createSheet();
// start 内容写入
XSSFRow row = sheet.createRow(0); // 指定列号
XSSFCell cell = row.createCell(0); // 指定行号
XSSFRichTextString richText = new XSSFRichTextString("中文字体测试");
cell.setCellValue(richText);
// 指定行号
XSSFCell enCell = row.createCell(3);
enCell.setCellValue(new XSSFRichTextString("English font test"));
// 设置列的宽度
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
// end 内容写入
// 创建PrintSetup
XSSFPrintSetup printSetup = sheet.getPrintSetup();
// A4纸
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
wb.write(out);
out.close();
} catch (Exception e) {
throw e;
}
}
/***
* 打印Xls格式文件
*
* @param path
* @throws Exception
*/
public static void writeXls(String path) throws Exception {
HSSFCell cell = null;
HSSFCellStyle cellstyle = null;
// 创建一个输出流
OutputStream os = new FileOutputStream(path);
// 创建一个工作簿
HSSFWorkbook work = new HSSFWorkbook();
// 创建一个工作表
HSSFSheet sheet = work.createSheet("sheet1");
// 创建行(第一行从0开始)
HSSFRow row = sheet.createRow(0);
// 创建单元格(第一列从0开始)
row.createCell(0).setCellValue("第一个单元格");
row.createCell(1).setCellValue("第二个单元格");
row.createCell(2).setCellValue(new Date());
// start 列号为3内容
// 通过workbook创建一个新的单元格风格(cell style)是很重要的
cellstyle = work.createCellStyle();
cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell(3);
cell.setCellValue(new Date());
cell.setCellStyle(cellstyle);
cellstyle = work.createCellStyle();
// end 列号为3内容
// start 列号为4内容
cell = row.createCell(4);
HSSFFont cnFont = work.createFont(); // 创建字体
cnFont.setFontHeightInPoints((short) 10); // 设置字体大小
cnFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体显示
cnFont.setFontName("仿宋_GB2312"); // 字体名称
cellstyle.setFont(cnFont);
cell.setCellValue("李宁一切皆有可能!");
cell.setCellStyle(cellstyle);
// end 列号为4内容
// 创建工作表
HSSFSheet sh2 = work.createSheet("sheet2");
HSSFRow row2 = sh2.createRow((short) 0);
cell = row2.createCell(0);
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_RIGHT);
cell.setCellStyle(cellstyle);
cell.setCellValue("工作表二,第一列数据");
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
cell = row2.createCell(1);
cell.setCellStyle(cellstyle);
cell.setCellValue("==");
// 合并单元格1至5
sh2.addMergedRegion(new CellRangeAddress(0, 0, 1, 5));
// 创建一个工作表
HSSFSheet sh3 = work.createSheet("sheet3");
HSSFPrintSetup ps = sh3.getPrintSetup();
sh3.setAutobreaks(true);
sh3.setSelected(true);
ps.setFitHeight((short) 1);
ps.setFitWidth((short) 1);
HSSFRow row3 = sh3.createRow(1);
// 列号为1
cell = row3.createCell(1);
// 设置样式
cellstyle = work.createCellStyle();
cellstyle.setBorderBottom(CellStyle.BORDER_THIN);
cellstyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellstyle.setBorderLeft(CellStyle.BORDER_THIN);
cellstyle.setLeftBorderColor(HSSFColor.GREEN.index);
cellstyle.setBorderRight(CellStyle.BORDER_THIN);
cellstyle.setRightBorderColor(HSSFColor.BLUE.index);
cellstyle.setBorderTop(CellStyle.BORDER_THIN);
cellstyle.setTopBorderColor(HSSFColor.BLACK.index);
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
// 加入样式
cell.setCellStyle(cellstyle);
// 列号为2
HSSFRow row4 = sh3.createRow(2);
cell = row4.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
// 要启用新行,需要使用wrap=true设置单元格样式。
cellstyle = work.createCellStyle();
cellstyle.setWrapText(true);
cell.setCellStyle(cellstyle);
// 设置高度
row4.setHeightInPoints((2 * sh3.getDefaultRowHeightInPoints()));
sh3.autoSizeColumn((short) 2);
// 循环写入数据
for (int i = 1; i < 11; i++) {
HSSFRow row5 = sh2.createRow(i);
cell = row5.createCell(0);
cellstyle = work.createCellStyle();
HSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
cell.setCellStyle(cellstyle);
cell.setCellValue("测试");
}
cellstyle = work.createCellStyle();
HSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
for (int i = 1; i < 21; i++) {
HSSFRow row5 = sheet.createRow(i);
cell = row5.createCell(0);
cell.setCellStyle(cellstyle);
cell.setCellValue("开始");
}
work.write(os);
os.close();
}
/***
* 打印Xls格式文件
*
* @param path
* @throws Exception
*/
public static void writeXlsx(String path) throws Exception {
XSSFCell cell = null;
XSSFCellStyle cellstyle = null;
// 创建一个输出流
OutputStream os = new FileOutputStream(path);
// 创建一个工作簿
XSSFWorkbook work = new XSSFWorkbook();
// 创建一个工作表
XSSFSheet sheet = work.createSheet("sheet1");
// 创建行(第一行从0开始)
XSSFRow row = sheet.createRow(0);
// 创建单元格(第一列从0开始)
row.createCell(0).setCellValue("第一个单元格");
row.createCell(1).setCellValue("第二个单元格");
row.createCell(2).setCellValue(new Date());
// start 列号为3内容
// 通过workbook创建一个新的单元格风格(cell style)是很重要的
cellstyle = work.createCellStyle();
cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell(3);
cell.setCellValue(new Date());
cell.setCellStyle(cellstyle);
cellstyle = work.createCellStyle();
// end 列号为3内容
// start 列号为4内容
cell = row.createCell(4);
XSSFFont cnFont = work.createFont(); // 创建字体
cnFont.setFontHeightInPoints((short) 10); // 设置字体大小
cnFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体显示
cnFont.setFontName("仿宋_GB2312"); // 字体名称
cellstyle.setFont(cnFont);
cell.setCellValue("李宁一切皆有可能!");
cell.setCellStyle(cellstyle);
// end 列号为4内容
// 创建工作表
XSSFSheet sh2 = work.createSheet("sheet2");
XSSFRow row2 = sh2.createRow((short) 0);
cell = row2.createCell(0);
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_RIGHT);
cell.setCellStyle(cellstyle);
cell.setCellValue("工作表二,第一列数据");
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
cell = row2.createCell(1);
cell.setCellStyle(cellstyle);
cell.setCellValue("==");
// 合并单元格1至5
sh2.addMergedRegion(new CellRangeAddress(0, 0, 1, 5));
// 创建一个工作表
XSSFSheet sh3 = work.createSheet("sheet3");
XSSFPrintSetup ps = sh3.getPrintSetup();
sh3.setAutobreaks(true);
sh3.setSelected(true);
ps.setFitHeight((short) 1);
ps.setFitWidth((short) 1);
XSSFRow row3 = sh3.createRow(1);
// 列号为1
cell = row3.createCell(1);
// 设置样式
cellstyle = work.createCellStyle();
cellstyle.setBorderBottom(CellStyle.BORDER_THIN);
cellstyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellstyle.setBorderLeft(CellStyle.BORDER_THIN);
cellstyle.setLeftBorderColor(HSSFColor.GREEN.index);
cellstyle.setBorderRight(CellStyle.BORDER_THIN);
cellstyle.setRightBorderColor(HSSFColor.BLUE.index);
cellstyle.setBorderTop(CellStyle.BORDER_THIN);
cellstyle.setTopBorderColor(HSSFColor.BLACK.index);
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
// 加入样式
cell.setCellStyle(cellstyle);
// 列号为2
XSSFRow row4 = sh3.createRow(2);
cell = row4.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
// 要启用新行,需要使用wrap=true设置单元格样式。
cellstyle = work.createCellStyle();
cellstyle.setWrapText(true);
cell.setCellStyle(cellstyle);
// 设置高度
row4.setHeightInPoints((2 * sh3.getDefaultRowHeightInPoints()));
sh3.autoSizeColumn((short) 2);
// 循环写入数据
for (int i = 1; i < 11; i++) {
XSSFRow row5 = sh2.createRow(i);
cell = row5.createCell(0);
cellstyle = work.createCellStyle();
XSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
cell.setCellStyle(cellstyle);
cell.setCellValue("测试");
}
cellstyle = work.createCellStyle();
XSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
for (int i = 1; i < 21; i++) {
XSSFRow row5 = sheet.createRow(i);
cell = row5.createCell(0);
cell.setCellStyle(cellstyle);
cell.setCellValue("开始");
}
work.write(os);
os.close();
}
/**
* 读取Xls格式的Excel文档
*
* @param path
* @return List<String>
*/
public static List<String> readXls(String path) throws Exception {
// 创建输入流
InputStream inp = new FileInputStream(path);
HSSFWorkbook workBook = new HSSFWorkbook(inp);
// 获得Excel中工作表个数
List<String> list = new ArrayList<String>();
int sheetCount = workBook.getNumberOfSheets();
System.out.println(sheetCount);
for (int i = 0; i < sheetCount; i++) {
String shname = workBook.getSheetName(i);// 工作表的名称
HSSFSheet sheet = workBook.getSheetAt(i);// 获取工作表
int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
System.out.println(rows);
if (rows > 0) {
for (int j = 0; j < rows; j++) {
HSSFRow row = sheet.getRow(j); // 取得当前行
if (row != null) {
int cells = row.getLastCellNum(); // 获取当前行列数
for (int k = 0; k < cells; k++) {
/** 获取当前列(就是一个单元格) */
HSSFCell cell = row.getCell(k);
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String content = cell.getStringCellValue();
if (StringUtils.isNotEmpty(content)) {
// System.out.println("内容:"+content);
list.add(content);
continue;
}
}
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
String content = String.valueOf(cell.getNumericCellValue());
if (StringUtils.isNotEmpty(content)) {
// System.out.println("内容:"+content);
list.add(content);
continue;
}
}
}
}
}
}
}
}
return list;
}
/**
* 读取Xlsx格式的Excel文档
*
* @param path
* @return List<String>
*/
public static List<String> readXlsx(String path) throws Exception {
// 创建输入流
InputStream inp = new FileInputStream(path);
XSSFWorkbook workBook = new XSSFWorkbook(inp);
// 获得Excel中工作表个数
List<String> list = new ArrayList<String>();
int sheetCount = workBook.getNumberOfSheets();
System.out.println(sheetCount);
for (int i = 0; i < sheetCount; i++) {
String shname = workBook.getSheetName(i);// 工作表的名称
XSSFSheet sheet = workBook.getSheetAt(i);// 获取工作表
int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
System.out.println(rows);
if (rows > 0) {
for (int j = 0; j < rows; j++) {
XSSFRow row = sheet.getRow(j); // 取得当前行
if (row != null) {
int cells = row.getLastCellNum(); // 获取当前行列数
for (int k = 0; k < cells; k++) {
/** 获取当前列(就是一个单元格) */
XSSFCell cell = row.getCell(k);
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String content = cell.getStringCellValue();
if (StringUtils.isNotEmpty(content)) {
// System.out.println("内容:"+content);
list.add(content);
}
}
}
}
}
}
}
}
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
// 读取测试
try {
/*List<String> list = ExcelUtil.readXlsx("/Users/xianglingchuan/Downloads/test(1).xlsx");
for (String string : list) {
System.out.println(string);
}*/
List<String> list = ExcelUtil.readXls("D:\\demo\\rechargeinfo.xls");
for (String string : list) {
System.out.println(string);
}
} catch (Exception e) {
e.printStackTrace();
}
/*// 输出测试
try {
//ExcelUtil.printXls("/Users/xianglingchuan/Downloads/test_print.xls");
//ExcelUtil.printXlsx("/Users/xianglingchuan/Downloads/test_print.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
try {
//ExcelUtil.writeXls("/Users/xianglingchuan/Downloads/test_write.xls");
//ExcelUtil.printXlsx("/Users/xianglingchuan/Downloads/test_print.xlsx");
} catch (Exception e) {
e.printStackTrace();
}*/
}
}
pom
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!-- poi office -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
12.RedisTemplate序列化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/**
* 功能描述:redisTemplate配置类 重写redisTemplate 改变序列化的
*
* @author:hdh
* @date: 2021/1/12 11:33
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> myRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化(默认采用的是JDK序列化)
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//string的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用string的序列化
redisTemplate.setKeySerializer(stringRedisSerializer);
//hash的key也采用string的序列化
redisTemplate.setHashKeySerializer(stringRedisSerializer);
//value序列化的方式也采用jackson
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value序列化方式采用jackson
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
13.RedisTemplate封装工具类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/**
* 功能描述:redisTemplate工具类
*
* @author:hdh
* @date: 2021/1/12 11:33
*/
@Component
public class RedisUtil {
@Autowired
@Qualifier("myRedisTemplate")
private RedisTemplate<String, Object> redisTemplate;
/**
* 删除key
*
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* 批量删除key
*
* @param keys
*/
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
/**
* 序列化key
*
* @param key
* @return
*/
public byte[] dump(String key) {
return redisTemplate.dump(key);
}
/**
* 是否存在key
*
* @param key
* @return
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 设置过期时间
*
* @param key
* @param timeout
* @param unit
* @return
*/
public Boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
* 设置过期时间
*
* @param key
* @param date
* @return
*/
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
/**
* 查找匹配的key
*
* @param pattern
* @return
*/
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 将当前数据库的 key 移动到给定的数据库 db 当中
*
* @param key
* @param dbIndex
* @return
*/
public Boolean move(String key, int dbIndex) {
return redisTemplate.move(key, dbIndex);
}
/**
* 移除 key 的过期时间,key 将持久保持
*
* @param key
* @return
*/
public Boolean persist(String key) {
return redisTemplate.persist(key);
}
/**
* 返回 key 的剩余的过期时间
*
* @param key
* @param unit
* @return
*/
public Long getExpire(String key, TimeUnit unit) {
return redisTemplate.getExpire(key, unit);
}
/**
* 返回 key 的剩余的过期时间
*
* @param key
* @return
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
/**
* 从当前数据库中随机返回一个 key
*
* @return
*/
public String randomKey() {
return redisTemplate.randomKey();
}
/**
* 修改 key 的名称
*
* @param oldKey
* @param newKey
*/
public void rename(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
/**
* 仅当 newkey 不存在时,将 oldKey 改名为 newkey
*
* @param oldKey
* @param newKey
* @return
*/
public Boolean renameIfAbsent(String oldKey, String newKey) {
return redisTemplate.renameIfAbsent(oldKey, newKey);
}
/**
* 返回 key 所储存的值的类型
*
* @param key
* @return
*/
public DataType type(String key) {
return redisTemplate.type(key);
}
/** -------------------string相关操作--------------------- */
/**
* 设置指定 key 的值
*
* @param key
* @param value
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 获取指定 key 的值
*
* @param key
* @return
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 返回 key 中字符串值的子字符
*
* @param key
* @param start
* @param end
* @return
*/
public String getRange(String key, long start, long end) {
return redisTemplate.opsForValue().get(key, start, end);
}
/**
* 将给定 key 的值设为 value ,并返回 key 的旧值(old value)
*
* @param key
* @param value
* @return
*/
public Object getAndSet(String key, String value) {
return redisTemplate.opsForValue().getAndSet(key, value);
}
/**
* 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
*
* @param key
* @param offset
* @return
*/
public Boolean getBit(String key, long offset) {
return redisTemplate.opsForValue().getBit(key, offset);
}
/**
* 批量获取
*
* @param keys
* @return
*/
public List<Object> multiGet(Collection<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
/**
* 设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
*
* @param key 位置
* @param value 值,true为1, false为0
* @return
*/
public boolean setBit(String key, long offset, boolean value) {
return redisTemplate.opsForValue().setBit(key, offset, value);
}
/**
* 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
*
* @param key
* @param value
* @param timeout 过期时间
* @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
* 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
*/
public void setEx(String key, String value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
/**
* 只有在 key 不存在时设置 key 的值
*
* @param key
* @param value
* @return 之前已经存在返回false, 不存在返回true
*/
public boolean setIfAbsent(String key, String value) {
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
/**
* 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
*
* @param key
* @param value
* @param offset 从指定位置开始覆写
*/
public void setRange(String key, String value, long offset) {
redisTemplate.opsForValue().set(key, value, offset);
}
/**
* 获取字符串的长度
*
* @param key
* @return
*/
public Long size(String key) {
return redisTemplate.opsForValue().size(key);
}
/**
* 批量添加
*
* @param maps
*/
public void multiSet(Map<String, String> maps) {
redisTemplate.opsForValue().multiSet(maps);
}
/**
* 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
*
* @param maps
* @return 之前已经存在返回false, 不存在返回true
*/
public boolean multiSetIfAbsent(Map<String, String> maps) {
return redisTemplate.opsForValue().multiSetIfAbsent(maps);
}
/**
* 增加(自增长), 负数则为自减
*
* @param key
* @return
*/
public Long incrBy(String key, long increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
/**
* @param key
* @return
*/
public Double incrByFloat(String key, double increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
/**
* 追加到末尾
*
* @param key
* @param value
* @return
*/
public Integer append(String key, String value) {
return redisTemplate.opsForValue().append(key, value);
}
/** -------------------hash相关操作------------------------- */
/**
* 获取存储在哈希表中指定字段的值
*
* @param key
* @param field
* @return
*/
public Object hGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
/**
* 获取所有给定字段的值
*
* @param key
* @return
*/
public Map<Object, Object> hGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 获取所有给定字段的值
*
* @param key
* @param fields
* @return
*/
public List<Object> hMultiGet(String key, Collection<Object> fields) {
return redisTemplate.opsForHash().multiGet(key, fields);
}
public void hPut(String key, String hashKey, String value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
public void hPutAll(String key, Map<String, String> maps) {
redisTemplate.opsForHash().putAll(key, maps);
}
/**
* 仅当hashKey不存在时才设置
*
* @param key
* @param hashKey
* @param value
* @return
*/
public Boolean hPutIfAbsent(String key, String hashKey, String value) {
return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
}
/**
* 删除一个或多个哈希表字段
*
* @param key
* @param fields
* @return
*/
public Long hDelete(String key, Object... fields) {
return redisTemplate.opsForHash().delete(key, fields);
}
/**
* 查看哈希表 key 中,指定的字段是否存在
*
* @param key
* @param field
* @return
*/
public boolean hExists(String key, String field) {
return redisTemplate.opsForHash().hasKey(key, field);
}
/**
* 为哈希表 key 中的指定字段的整数值加上增量 increment
*
* @param key
* @param field
* @param increment
* @return
*/
public Long hIncrBy(String key, Object field, long increment) {
return redisTemplate.opsForHash().increment(key, field, increment);
}
/**
* 为哈希表 key 中的指定字段的整数值加上增量 increment
*
* @param key
* @param field
* @param delta
* @return
*/
public Double hIncrByFloat(String key, Object field, double delta) {
return redisTemplate.opsForHash().increment(key, field, delta);
}
/**
* 获取所有哈希表中的字段
*
* @param key
* @return
*/
public Set<Object> hKeys(String key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* 获取哈希表中字段的数量
*
* @param key
* @return
*/
public Long hSize(String key) {
return redisTemplate.opsForHash().size(key);
}
/**
* 获取哈希表中所有值
*
* @param key
* @return
*/
public List<Object> hValues(String key) {
return redisTemplate.opsForHash().values(key);
}
/**
* 迭代哈希表中的键值对
*
* @param key
* @param options
* @return
*/
public Cursor<Entry<Object, Object>> hScan(String key, ScanOptions options) {
return redisTemplate.opsForHash().scan(key, options);
}
/** ------------------------list相关操作---------------------------- */
/**
* 通过索引获取列表中的元素
*
* @param key
* @param index
* @return
*/
public Object lIndex(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
/**
* 获取列表指定范围内的元素
*
* @param key
* @param start 开始位置, 0是开始位置
* @param end 结束位置, -1返回所有
* @return
*/
public List<Object> lRange(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 存储在list头部
*
* @param key
* @param value
* @return
*/
public Long lLeftPush(String key, String value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* @param key
* @param value
* @return
*/
public Long lLeftPushAll(String key, String... value) {
return redisTemplate.opsForList().leftPushAll(key, value);
}
/**
* @param key
* @param value
* @return
*/
public Long lLeftPushAll(String key, Collection<String> value) {
return redisTemplate.opsForList().leftPushAll(key, value);
}
/**
* 当list存在的时候才加入
*
* @param key
* @param value
* @return
*/
public Long lLeftPushIfPresent(String key, String value) {
return redisTemplate.opsForList().leftPushIfPresent(key, value);
}
/**
* 如果pivot存在,再pivot前面添加
*
* @param key
* @param pivot
* @param value
* @return
*/
public Long lLeftPush(String key, String pivot, String value) {
return redisTemplate.opsForList().leftPush(key, pivot, value);
}
/**
* @param key
* @param value
* @return
*/
public Long lRightPush(String key, String value) {
return redisTemplate.opsForList().rightPush(key, value);
}
/**
* @param key
* @param value
* @return
*/
public Long lRightPushAll(String key, String... value) {
return redisTemplate.opsForList().rightPushAll(key, value);
}
/**
* @param key
* @param value
* @return
*/
public Long lRightPushAll(String key, Collection<String> value) {
return redisTemplate.opsForList().rightPushAll(key, value);
}
/**
* 为已存在的列表添加值
*
* @param key
* @param value
* @return
*/
public Long lRightPushIfPresent(String key, String value) {
return redisTemplate.opsForList().rightPushIfPresent(key, value);
}
/**
* 在pivot元素的右边添加值
*
* @param key
* @param pivot
* @param value
* @return
*/
public Long lRightPush(String key, String pivot, String value) {
return redisTemplate.opsForList().rightPush(key, pivot, value);
}
/**
* 通过索引设置列表元素的值
*
* @param key
* @param index 位置
* @param value
*/
public void lSet(String key, long index, String value) {
redisTemplate.opsForList().set(key, index, value);
}
/**
* 移出并获取列表的第一个元素
*
* @param key
* @return 删除的元素
*/
public Object lLeftPop(String key) {
return redisTemplate.opsForList().leftPop(key);
}
/**
* 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
*
* @param key
* @param timeout 等待时间
* @param unit 时间单位
* @return
*/
public Object lBLeftPop(String key, long timeout, TimeUnit unit) {
return redisTemplate.opsForList().leftPop(key, timeout, unit);
}
/**
* 移除并获取列表最后一个元素
*
* @param key
* @return 删除的元素
*/
public Object lRightPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
/**
* 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
*
* @param key
* @param timeout 等待时间
* @param unit 时间单位
* @return
*/
public Object lBRightPop(String key, long timeout, TimeUnit unit) {
return redisTemplate.opsForList().rightPop(key, timeout, unit);
}
/**
* 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
*
* @param sourceKey
* @param destinationKey
* @return
*/
public Object lRightPopAndLeftPush(String sourceKey, String destinationKey) {
return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
destinationKey);
}
/**
* 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
*
* @param sourceKey
* @param destinationKey
* @param timeout
* @param unit
* @return
*/
public Object lBRightPopAndLeftPush(String sourceKey, String destinationKey,
long timeout, TimeUnit unit) {
return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
destinationKey, timeout, unit);
}
/**
* 删除集合中值等于value得元素
*
* @param key
* @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素;
* index<0, 从尾部开始删除第一个值等于value的元素;
* @param value
* @return
*/
public Long lRemove(String key, long index, String value) {
return redisTemplate.opsForList().remove(key, index, value);
}
/**
* 裁剪list
*
* @param key
* @param start
* @param end
*/
public void lTrim(String key, long start, long end) {
redisTemplate.opsForList().trim(key, start, end);
}
/**
* 获取列表长度
*
* @param key
* @return
*/
public Long lLen(String key) {
return redisTemplate.opsForList().size(key);
}
/** --------------------set相关操作-------------------------- */
/**
* set添加元素
*
* @param key
* @param values
* @return
*/
public Long sAdd(String key, String... values) {
return redisTemplate.opsForSet().add(key, values);
}
/**
* set移除元素
*
* @param key
* @param values
* @return
*/
public Long sRemove(String key, Object... values) {
return redisTemplate.opsForSet().remove(key, values);
}
/**
* 移除并返回集合的一个随机元素
*
* @param key
* @return
*/
public Object sPop(String key) {
return redisTemplate.opsForSet().pop(key);
}
/**
* 将元素value从一个集合移到另一个集合
*
* @param key
* @param value
* @param destKey
* @return
*/
public Boolean sMove(String key, String value, String destKey) {
return redisTemplate.opsForSet().move(key, value, destKey);
}
/**
* 获取集合的大小
*
* @param key
* @return
*/
public Long sSize(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* 判断集合是否包含value
*
* @param key
* @param value
* @return
*/
public Boolean sIsMember(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* 获取两个集合的交集
*
* @param key
* @param otherKey
* @return
*/
public Set<Object> sIntersect(String key, String otherKey) {
return redisTemplate.opsForSet().intersect(key, otherKey);
}
/**
* 获取key集合与多个集合的交集
*
* @param key
* @param otherKeys
* @return
*/
public Set<Object> sIntersect(String key, Collection<String> otherKeys) {
return redisTemplate.opsForSet().intersect(key, otherKeys);
}
/**
* key集合与otherKey集合的交集存储到destKey集合中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long sIntersectAndStore(String key, String otherKey, String destKey) {
return redisTemplate.opsForSet().intersectAndStore(key, otherKey,
destKey);
}
/**
* key集合与多个集合的交集存储到destKey集合中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long sIntersectAndStore(String key, Collection<String> otherKeys,
String destKey) {
return redisTemplate.opsForSet().intersectAndStore(key, otherKeys,
destKey);
}
/**
* 获取两个集合的并集
*
* @param key
* @param otherKeys
* @return
*/
public Set<Object> sUnion(String key, String otherKeys) {
return redisTemplate.opsForSet().union(key, otherKeys);
}
/**
* 获取key集合与多个集合的并集
*
* @param key
* @param otherKeys
* @return
*/
public Set<Object> sUnion(String key, Collection<String> otherKeys) {
return redisTemplate.opsForSet().union(key, otherKeys);
}
/**
* key集合与otherKey集合的并集存储到destKey中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long sUnionAndStore(String key, String otherKey, String destKey) {
return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
}
/**
* key集合与多个集合的并集存储到destKey中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long sUnionAndStore(String key, Collection<String> otherKeys,
String destKey) {
return redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
}
/**
* 获取两个集合的差集
*
* @param key
* @param otherKey
* @return
*/
public Set<Object> sDifference(String key, String otherKey) {
return redisTemplate.opsForSet().difference(key, otherKey);
}
/**
* 获取key集合与多个集合的差集
*
* @param key
* @param otherKeys
* @return
*/
public Set<Object> sDifference(String key, Collection<String> otherKeys) {
return redisTemplate.opsForSet().difference(key, otherKeys);
}
/**
* key集合与otherKey集合的差集存储到destKey中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long sDifference(String key, String otherKey, String destKey) {
return redisTemplate.opsForSet().differenceAndStore(key, otherKey,
destKey);
}
/**
* key集合与多个集合的差集存储到destKey中
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long sDifference(String key, Collection<String> otherKeys,
String destKey) {
return redisTemplate.opsForSet().differenceAndStore(key, otherKeys,
destKey);
}
/**
* 获取集合所有元素
*
* @param key
* @return
*/
public Set<Object> setMembers(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 随机获取集合中的一个元素
*
* @param key
* @return
*/
public Object sRandomMember(String key) {
return redisTemplate.opsForSet().randomMember(key);
}
/**
* 随机获取集合中count个元素
*
* @param key
* @param count
* @return
*/
public List<Object> sRandomMembers(String key, long count) {
return redisTemplate.opsForSet().randomMembers(key, count);
}
/**
* 随机获取集合中count个元素并且去除重复的
*
* @param key
* @param count
* @return
*/
public Set<Object> sDistinctRandomMembers(String key, long count) {
return redisTemplate.opsForSet().distinctRandomMembers(key, count);
}
/**
* @param key
* @param options
* @return
*/
public Cursor<Object> sScan(String key, ScanOptions options) {
return redisTemplate.opsForSet().scan(key, options);
}
/**------------------zSet相关操作--------------------------------*/
/**
* 添加元素,有序集合是按照元素的score值由小到大排列
*
* @param key
* @param value
* @param score
* @return
*/
public Boolean zAdd(String key, String value, double score) {
return redisTemplate.opsForZSet().add(key, value, score);
}
/**
* @param key
* @param values
* @return
*/
public Long zAdd(String key, Set<TypedTuple<Object>> values) {
return redisTemplate.opsForZSet().add(key, values);
}
/**
* @param key
* @param values
* @return
*/
public Long zRemove(String key, Object... values) {
return redisTemplate.opsForZSet().remove(key, values);
}
/**
* 增加元素的score值,并返回增加后的值
*
* @param key
* @param value
* @param delta
* @return
*/
public Double zIncrementScore(String key, String value, double delta) {
return redisTemplate.opsForZSet().incrementScore(key, value, delta);
}
/**
* 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
*
* @param key
* @param value
* @return 0表示第一位
*/
public Long zRank(String key, Object value) {
return redisTemplate.opsForZSet().rank(key, value);
}
/**
* 返回元素在集合的排名,按元素的score值由大到小排列
*
* @param key
* @param value
* @return
*/
public Long zReverseRank(String key, Object value) {
return redisTemplate.opsForZSet().reverseRank(key, value);
}
/**
* 获取集合的元素, 从小到大排序
*
* @param key
* @param start 开始位置
* @param end 结束位置, -1查询所有
* @return
*/
public Set<Object> zRange(String key, long start, long end) {
return redisTemplate.opsForZSet().range(key, start, end);
}
/**
* 获取集合元素, 并且把score值也获取
*
* @param key
* @param start
* @param end
* @return
*/
public Set<TypedTuple<Object>> zRangeWithScores(String key, long start,
long end) {
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
}
/**
* 根据Score值查询集合元素
*
* @param key
* @param min 最小值
* @param max 最大值
* @return
*/
public Set<Object> zRangeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
}
/**
* 根据Score值查询集合元素, 从小到大排序
*
* @param key
* @param min 最小值
* @param max 最大值
* @return
*/
public Set<TypedTuple<Object>> zRangeByScoreWithScores(String key,
double min, double max) {
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
}
/**
* @param key
* @param min
* @param max
* @param start
* @param end
* @return
*/
public Set<TypedTuple<Object>> zRangeByScoreWithScores(String key,
double min, double max, long start, long end) {
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,
start, end);
}
/**
* 获取集合的元素, 从大到小排序
*
* @param key
* @param start
* @param end
* @return
*/
public Set<Object> zReverseRange(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRange(key, start, end);
}
/**
* 获取集合的元素, 从大到小排序, 并返回score值
*
* @param key
* @param start
* @param end
* @return
*/
public Set<TypedTuple<Object>> zReverseRangeWithScores(String key,
long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start,
end);
}
/**
* 根据Score值查询集合元素, 从大到小排序
*
* @param key
* @param min
* @param max
* @return
*/
public Set<Object> zReverseRangeByScore(String key, double min,
double max) {
return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
}
/**
* 根据Score值查询集合元素, 从大到小排序
*
* @param key
* @param min
* @param max
* @return
*/
public Set<TypedTuple<Object>> zReverseRangeByScoreWithScores(
String key, double min, double max) {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,
min, max);
}
/**
* @param key
* @param min
* @param max
* @param start
* @param end
* @return
*/
public Set<Object> zReverseRangeByScore(String key, double min,
double max, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max,
start, end);
}
/**
* 根据score值获取集合元素数量
*
* @param key
* @param min
* @param max
* @return
*/
public Long zCount(String key, double min, double max) {
return redisTemplate.opsForZSet().count(key, min, max);
}
/**
* 获取集合大小
*
* @param key
* @return
*/
public Long zSize(String key) {
return redisTemplate.opsForZSet().size(key);
}
/**
* 获取集合大小
*
* @param key
* @return
*/
public Long zZCard(String key) {
return redisTemplate.opsForZSet().zCard(key);
}
/**
* 获取集合中value元素的score值
*
* @param key
* @param value
* @return
*/
public Double zScore(String key, Object value) {
return redisTemplate.opsForZSet().score(key, value);
}
/**
* 移除指定索引位置的成员
*
* @param key
* @param start
* @param end
* @return
*/
public Long zRemoveRange(String key, long start, long end) {
return redisTemplate.opsForZSet().removeRange(key, start, end);
}
/**
* 根据指定的score值的范围来移除成员
*
* @param key
* @param min
* @param max
* @return
*/
public Long zRemoveRangeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
}
/**
* 获取key和otherKey的并集并存储在destKey中
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long zUnionAndStore(String key, String otherKey, String destKey) {
return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
}
/**
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long zUnionAndStore(String key, Collection<String> otherKeys,
String destKey) {
return redisTemplate.opsForZSet()
.unionAndStore(key, otherKeys, destKey);
}
/**
* 交集
*
* @param key
* @param otherKey
* @param destKey
* @return
*/
public Long zIntersectAndStore(String key, String otherKey,
String destKey) {
return redisTemplate.opsForZSet().intersectAndStore(key, otherKey,
destKey);
}
/**
* 交集
*
* @param key
* @param otherKeys
* @param destKey
* @return
*/
public Long zIntersectAndStore(String key, Collection<String> otherKeys,
String destKey) {
return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys,
destKey);
}
/**
* @param key
* @param options
* @return
*/
public Cursor<TypedTuple<Object>> zScan(String key, ScanOptions options) {
return redisTemplate.opsForZSet().scan(key, options);
}
}
14RedisGeoUtils封装工具类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Component
public class RedisGeoUtils {
@Autowired
@Qualifier("myRedisTemplate")
private RedisTemplate redisTemplate;
/**
* 添加经纬度信息
* <p>
* redis 命令:geoadd key 116.405285 39.904989 "北京"
*/
public Long geoAdd(String key, Point point, String member) {
if (redisTemplate.hasKey(key)) {
redisTemplate.opsForGeo().remove(key, member);
}
return redisTemplate.opsForGeo().add(key, point, member);
}
/**
* 查找指定key的经纬度信息,可以指定多个member,批量返回
* <p>
* redis命令:geopos key 北京
*/
public List<Point> geoGet(String key, String... members) {
return redisTemplate.opsForGeo().position(key, members);
}
/**
* 返回两个地方的距离,可以指定单位,比如米m,千米km,英里mi,英尺ft
* <p>
* redis命令:geodist key 北京 上海
*/
public Distance geoDist(String key, String member1, String member2, Metric metric) {
return redisTemplate.opsForGeo().distance(key, member1, member2, metric);
}
/**
* 根据给定的经纬度,返回半径不超过指定距离的元素
* <p>
* redis命令:georadius key 116.405285 39.904989 100 km WITHDIST WITHCOORD ASC
* COUNT 5
* includeDistance 包含距离
* includeCoordinates 包含经纬度
* sortAscending 正序排序
* limit 限定返回的记录数
*/
public GeoResults<RedisGeoCommands.GeoLocation<String>> nearByXY(String key, Circle circle, long count) {
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
.includeDistance().includeCoordinates().sortAscending().limit(count);
return redisTemplate.opsForGeo().radius(key, circle, args);
}
/**
* 根据指定的地点查询半径在指定范围内的位置
* <p>
* redis命令:georadiusbymember key 北京 100 km WITHDIST WITHCOORD ASC COUNT 5
*/
public GeoResults<RedisGeoCommands.GeoLocation<String>> nearByPlace(String key, String member, Distance distance,
long count) {
// includeDistance 包含距离
// includeCoordinates 包含经纬度
// sortAscending 正序排序
// limit 限定返回的记录数
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
.includeDistance().includeCoordinates().sortAscending().limit(count);
return redisTemplate.opsForGeo().radius(key, member, distance, args);
}
/**
* 返回的是geohash值
* <p>
* redis命令:geohash key 北京
*/
public List<String> geoHash(String key, String member) {
return redisTemplate.opsForGeo().hash(key, member);
}
}
15.雪花算法
@Configuration配置类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import com.car.oa.utils.SnowflakeIdWorker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 雪花算法订单号生成
*/
@Configuration
public class SnowflakeConfig {
@Bean
public SnowflakeIdWorker snowflakeIdWorker() {
return new SnowflakeIdWorker(0, 0);
}
}
实现类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import org.springframework.context.annotation.Configuration;
/**
* Twitter_Snowflake<br>
* SnowFlake的结构如下(每部分用-分开):<br>
* 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 -
* 000000000000 <br>
* 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br>
* 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截)
* 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。
* 41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br>
* 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br>
* 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br>
* 加起来刚好64位,为一个Long型。<br>
* SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,
* SnowFlake每秒能够产生26万ID左右。
*/
@Configuration
public class SnowflakeIdWorker {
// ==============================Fields===========================================
/**
* 开始时间截 (2015-01-01)
*/
private final long twepoch = 1420041600000L;
/**
* 机器id所占的位数
*/
private final long workerIdBits = 5L;
/**
* 数据标识id所占的位数
*/
private final long datacenterIdBits = 5L;
/**
* 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)
*/
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
/**
* 支持的最大数据标识id,结果是31
*/
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
/**
* 序列在id中占的位数
*/
private final long sequenceBits = 12L;
/**
* 机器ID向左移12位
*/
private final long workerIdShift = sequenceBits;
/**
* 数据标识id向左移17位(12+5)
*/
private final long datacenterIdShift = sequenceBits + workerIdBits;
/**
* 时间截向左移22位(5+5+12)
*/
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
/**
* 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095)
*/
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
/**
* 工作机器ID(0~31)
*/
private long workerId;
/**
* 数据中心ID(0~31)
*/
private long datacenterId;
/**
* 毫秒内序列(0~4095)
*/
private long sequence = 0L;
/**
* 上次生成ID的时间截
*/
private long lastTimestamp = -1L;
// ==============================Constructors=====================================
/**
* 构造函数
*
* @param workerId 工作ID (0~31)
* @param datacenterId 数据中心ID (0~31)
*/
public SnowflakeIdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(
String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
// ==============================Methods==========================================
/**
* 获得下一个ID (该方法是线程安全的)
*
* @return SnowflakeId
*/
public synchronized long nextId() {
long timestamp = timeGen();
// 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format(
"Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
// 如果是同一时间生成的,则进行毫秒内序列
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
// 毫秒内序列溢出
if (sequence == 0) {
// 阻塞到下一个毫秒,获得新的时间戳
timestamp = tilNextMillis(lastTimestamp);
}
}
// 时间戳改变,毫秒内序列重置
else {
sequence = 0L;
}
// 上次生成ID的时间截
lastTimestamp = timestamp;
// 移位并通过或运算拼到一起组成64位的ID
return ((timestamp - twepoch) << timestampLeftShift) //
| (datacenterId << datacenterIdShift) //
| (workerId << workerIdShift) //
| sequence;
}
/**
* 阻塞到下一个毫秒,直到获得新的时间戳
*
* @param lastTimestamp 上次生成ID的时间截
* @return 当前时间戳
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
/**
* 返回以毫秒为单位的当前时间
*
* @return 当前时间(毫秒)
*/
protected long timeGen() {
return System.currentTimeMillis();
}
}
16.multipartfile转file,inputStream转file
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.car.oa.utils; /** * Created by TongGuoBo on 2019/6/19. */ import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; /** * @ClassName MultipartFileToFile * @Description MultipartFile转fie * @Author TongGuoBo * @Date 2019/6/19 13:48 **/ public class MultipartFileToFile { /** * MultipartFile 转 File * * @param file * @throws Exception */ public static File multipartFileToFile(MultipartFile file) throws Exception { File toFile = null; if (file.equals("") || file.getSize() <= 0) { file = null; } else { InputStream ins = null; ins = file.getInputStream(); toFile = new File(file.getOriginalFilename()); inputStreamToFile(ins, toFile); ins.close(); } return toFile; } //获取流文件 private static void inputStreamToFile(InputStream ins, File file) { try { OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 删除本地临时文件 * @param file */ public static void delteTempFile(File file) { if (file != null) { File del = new File(file.toURI()); del.delete(); } } }