整理收集的一些常用java工具类

28个java常用的工具类 java工具类,包含了字符串类型,数据类型,MD5,日期时间类型,文件上传工具类,正则表达式帮助类等28个java开发的常用工具。 立即下载

1.json转换工具


 
package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * json转换工具类 */ public class JsonUtils { // 定义jackson对象  private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 * <p>Title: pojoToJson</p> * <p>Description: </p> * @param data * @return */ public static String objectToJson(Object data) { try { String string = MAPPER.writeValueAsString(data); return string; } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } /** * 将json结果集转化为对象 * @param jsonData json数据 * @param clazz 对象中的object类型 * @return */ public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list * <p>Title: jsonToList</p> * <p>Description: </p> * @param jsonData * @param beanType * @return */ public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); . return list; } catch (Exception e) { . e.printStackTrace(); } return null; } } 

2.cookie的读写


 
package com.taotao.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * . * Cookie 工具类 * */ public final class CookieUtils { /** * 得到Cookie的值, 不编码 . * * @param request . * @param cookieName * @return */ public static String getCookieValue(HttpServletRequest request, String cookieName) { return getCookieValue(request, cookieName, false); } /** . * 得到Cookie的值, * * @param request * @param cookieName * @return . */ public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) { Cookie[] cookieList = request.getCookies(); if (cookieList == null || cookieName == null) { return null; } String retValue = null; try { for (int i = 0; i < cookieList.length; i++) { . if (cookieList[i].getName().equals(cookieName)) { if (isDecoder) { retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8"); } else { retValue = cookieList[i].getValue(); } break; } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return retValue; } /** * 得到Cookie的值, ** @param request * @param cookieName * @return */ public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) { Cookie[] cookieList = request.getCookies(); if (cookieList == null || cookieName == null) { return null; } String retValue = null; try { for (int i = 0; i < cookieList.length; i++) { if (cookieList[i].getName().equals(cookieName)) { retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString); break; } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return retValue; } /** * 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue) { setCookie(request, response, cookieName, cookieValue, -1); . } /** * 设置Cookie的值 在指定时间内生效,但不编码 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage) { setCookie(request, response, cookieName, cookieValue, cookieMaxage, false); } /** * 设置Cookie的值 不设置生效时间,但编码 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, boolean isEncode) { setCookie(request, response, cookieName, cookieValue, -1, isEncode); } /** * 设置Cookie的值 在指定时间内生效, 编码参数 */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) { doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode); } /** * 设置Cookie的值 在指定时间内生效, 编码参数(指定编码) */ public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, String encodeString) { doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString); } /** * 删除Cookie带cookie域名 */ public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) { doSetCookie(request, response, cookieName, "", -1, false); } /** * 设置Cookie的值,并使其在指定时间内生效 * * @param cookieMaxage cookie生效的最大秒数 */ private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) { try { if (cookieValue == null) { cookieValue = ""; } else if (isEncode) { cookieValue = URLEncoder.encode(cookieValue, "utf-8"); } Cookie cookie = new Cookie(cookieName, cookieValue); if (cookieMaxage > 0) cookie.setMaxAge(cookieMaxage); if (null != request) {// 设置域名的cookie  String domainName = getDomainName(request); System.out.println(domainName); if (!"localhost".equals(domainName)) { cookie.setDomain(domainName); } } cookie.setPath("/"); response.addCookie(cookie); } catch (Exception e) { e.printStackTrace(); } } /** * 设置Cookie的值,并使其在指定时间内生效 * * @param cookieMaxage cookie生效的最大秒数 */ private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, String encodeString) { try { if (cookieValue == null) { cookieValue = ""; } else { cookieValue = URLEncoder.encode(cookieValue, encodeString); } Cookie cookie = new Cookie(cookieName, cookieValue); if (cookieMaxage > 0) cookie.setMaxAge(cookieMaxage); if (null != request) {// 设置域名的cookie  String domainName = getDomainName(request); System.out.println(domainName); if (!"localhost".equals(domainName)) { cookie.setDomain(domainName); } } cookie.setPath("/"); response.addCookie(cookie); } catch (Exception e) { e.printStackTrace(); } } /** * 得到cookie的域名 */ private static final String getDomainName(HttpServletRequest request) { String domainName = null; String serverName = request.getRequestURL().toString(); if (serverName == null || serverName.equals("")) { domainName = ""; } else { serverName = serverName.toLowerCase(); serverName = serverName.substring(7); final int end = serverName.indexOf("/"); serverName = serverName.substring(0, end); final String[] domains = serverName.split("\\."); int len = domains.length; if (len > 3) { // www.xxx.com.cn  domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1]; } else if (len <= 3 && len > 1) { // xxx.com or xxx.cn  domainName = "." + domains[len - 2] + "." + domains[len - 1]; } else { domainName = serverName; } } if (domainName != null && domainName.indexOf(":") > 0) { String[] ary = domainName.split("\\:"); domainName = ary[0]; } return domainName; } } 

3.HttpClientUtil


 
package com.taotao.utils; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; 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); 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; } } 

4.FastDFSClient工具类


 
package cn.itcast.fastdfs.cliennt; import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient1; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; public class FastDFSClient { private TrackerClient trackerClient = null; private TrackerServer trackerServer = null; private StorageServer storageServer = null; private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception { if (conf.contains("classpath:")) { conf = conf.replace("classpath:", this.getClass().getResource("/").getPath()); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } /** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileName 文件全路径 * @param extName 文件扩展名,不包含(.) * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileName, extName, metas); return result; } public String uploadFile(String fileName) throws Exception { return uploadFile(fileName, null, null); } public String uploadFile(String fileName, String extName) throws Exception { return uploadFile(fileName, extName, null); } /** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileContent 文件的内容,字节数组 * @param extName 文件扩展名 * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas); return result; } public String uploadFile(byte[] fileContent) throws Exception { return uploadFile(fileContent, null, null); } public String uploadFile(byte[] fileContent, String extName) throws Exception { return uploadFile(fileContent, extName, null); } } <span style="font-size:14px;font-weight:normal;">public class FastDFSTest { @Test public void testFileUpload() throws Exception { // 1、加载配置文件,配置文件中的内容就是tracker服务的地址。  ClientGlobal.init("D:/workspaces-itcast/term197/taotao-manager-web/src/main/resources/resource/client.conf"); // 2、创建一个TrackerClient对象。直接new一个。  TrackerClient trackerClient = new TrackerClient(); // 3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。  TrackerServer trackerServer = trackerClient.getConnection(); // 4、创建一个StorageServer的引用,值为null  StorageServer storageServer = null; // 5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用  StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 6、使用StorageClient对象上传图片。  //扩展名不带“.”  String[] strings = storageClient.upload_file("D:/Documents/Pictures/images/200811281555127886.jpg", "jpg", null); // 7、返回数组。包含组名和图片的路径。  for (String string : strings) { System.out.println(string); } } }</span> # ![image](http://upload-images.jianshu.io/upload_images/2509688-45370c9d24b87e31?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 

5.获取异常的堆栈信息


 
package com.taotao.utils; import java.io.PrintWriter; import java.io.StringWriter; public class ExceptionUtil { /** * 获取异常的堆栈信息 * * @param t * @return */ public static String getStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { t.printStackTrace(pw); return sw.toString(); } finally { pw.close(); } } } 

#6.easyUIDataGrid对象返回值


 
package com.taotao.result; import java.util.List; /** * easyUIDataGrid对象返回值 * <p>Title: EasyUIResult</p> * <p>Description: </p> * <p>Company: www.itcast.com</p> * @author 入云龙 * @date 2019年7月21日下午4:12:52 * @version 1.0 */ public class EasyUIResult { private Integer total; private List<?> rows; public EasyUIResult(Integer total, List<?> rows) { this.total = total; this.rows = rows; } public EasyUIResult(long total, List<?> rows) { this.total = (int) total; this.rows = rows; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public List<?> getRows() { return rows; } public void setRows(List<?> rows) { this.rows = rows; } } 

7.ftp上传下载工具类


 
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /** * ftp上传下载工具类 * <p>Title: FtpUtil</p> * <p>Description: </p> * <p>Company: www.itcast.com</p> * @author 入云龙 * @date 2015年7月29日下午8:11:51 * @version 1.0 */ public class FtpUtil { /** * Description: 向FTP服务器上传文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param basePath FTP服务器基础目录 * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath * @param filename 上传到FTP服务器上的文件名 * @param input 输入流 * @return 成功返回true,否则返回false */ public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port);// 连接FTP服务器  // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  ftp.login(username, password);// 登录 reply = ftp.getReplyCode();  if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //切换到上传目录 if (!ftp.changeWorkingDirectory(basePath+filePath)) {  //如果目录不存在创建目录 String[] dirs = filePath.split("/");  String tempPath = basePath; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } //设置上传文件的类型为二进制类型  ftp.setFileType(FTP.BINARY_FILE_TYPE); //上传文件  if (!ftp.storeFile(filename, input)) { return result; . } input.close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: 从FTP服务器下载文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param remotePath FTP服务器上的相对路径 * @param fileName 要下载的文件名 . * @param localPath 下载后保存到本地的路径 * @return */ public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  ftp.login(username, password);// 登录 reply = ftp.getReplyCode();  if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录  FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static void main(String[] args) { try { FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg")); 147. boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } } } 

8.各种id生成策略


 
package com.taotao.utils; import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: </p> * @date 2015年7月22日下午2:32:10 * @version 1.0 */ public class IDUtils { /** * 图片名生成 */ public static String genImageName() { //取当前时间的长整形值包含毫秒  long millis = System.currentTimeMillis(); //long millis = System.nanoTime(); //加上三位随机数  Random random = new Random(); int end3 = random.nextInt(999); //如果不足三位前面补0  String str = millis + String.format("%03d", end3); return str; } /** * 商品id生成 */ public static long genItemId() { //取当前时间的长整形值包含毫秒 long millis = System.currentTimeMillis();  //long millis = System.nanoTime(); //加上两位随机数  Random random = new Random(); int end2 = random.nextInt(99); //如果不足两位前面补0  String str = millis + String.format("%02d", end2); long id = new Long(str); return id; } public static void main(String[] args) { for(int i=0;i< 100;i++) System.out.println(genItemId()); } } 

##9.上传图片返回值


 
package com.result; /** * 上传图片返回值 * <p>Title: PictureResult</p> * <p>Description: </p> * <p>Company: www.itcast.com</p> * @author 入云龙 * @date 2015年7月22日下午2:09:02 * @version 1.0 */ public class PictureResult { /** * 上传图片返回值,成功:0 失败:1 */ private Integer error; /** * 回显图片使用的url */ private String url; /** * 错误时的错误消息 */ private String message; public PictureResult(Integer state, String url) { this.url = url; this.error = state; } public PictureResult(Integer state, String url, String errorMessage) { this.url = url; this.error = state; this.message = errorMessage; } public Integer getError() { return error; } public void setError(Integer error) { this.error = error; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 

10.自定义响应结构


 
package com.result; import java.util.List; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * 自定义响应结构 */ public class TaotaoResult { // 定义jackson对象  private static final ObjectMapper MAPPER = new ObjectMapper(); // 响应业务状态 private Integer status;  // 响应消息 private String msg;  // 响应中的数据 private Object data;  public static TaotaoResult build(Integer status, String msg, Object data) { return new TaotaoResult(status, msg, data); } public static TaotaoResult ok(Object data) { return new TaotaoResult(data); } public static TaotaoResult ok() { return new TaotaoResult(null); } public TaotaoResult() { } public static TaotaoResult build(Integer status, String msg) { return new TaotaoResult(status, msg, null); } public TaotaoResult(Integer status, String msg, Object data) { this.status = status; this.msg = msg; this.data = data; } public TaotaoResult(Object data) { this.status = 200; this.msg = "OK"; this.data = data; } // public Boolean isOK() { . // return this.status == 200; // }  public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } /** * 将json结果集转化为TaotaoResult对象 * * @param jsonData json数据 * @param clazz TaotaoResult中的object类型 * @return */ public static TaotaoResult formatToPojo(String jsonData, Class<?> clazz) { try { if (clazz == null) { return MAPPER.readValue(jsonData, TaotaoResult.class); } JsonNode jsonNode = MAPPER.readTree(jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (clazz != null) { if (data.isObject()) { obj = MAPPER.readValue(data.traverse(), clazz); } else if (data.isTextual()) { obj = MAPPER.readValue(data.asText(), clazz); } } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } /** * 没有object对象的转化 * * @param json * @return */ public static TaotaoResult format(String json) { try { return MAPPER.readValue(json, TaotaoResult.class); } catch (Exception e) { e.printStackTrace(); } return null; } /** * Object是集合转化 * * @param jsonData json数据 * @param clazz 集合中的类型 * @return */ public static TaotaoResult formatToList(String jsonData, Class<?> clazz) { try { JsonNode jsonNode = MAPPER.readTree(jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (data.isArray() && data.size() > 0) { obj = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } } 

##11.jedis操作


 
package com.taotao.jedis; public interface JedisClient { String set(String key, String value); String get(String key); Boolean exists(String key); Long expire(String key, int seconds); Long ttl(String key); Long incr(String key); Long hset(String key, String field, String value); String hget(String key, String field); Long hdel(String key, String... field); } package com.taotao.jedis; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.JedisCluster; public class JedisClientCluster implements JedisClient { @Autowired private JedisCluster jedisCluster; @Override public String set(String key, String value) { return jedisCluster.set(key, value); } @Override public String get(String key) { return jedisCluster.get(key); } @Override public Boolean exists(String key) { return jedisCluster.exists(key); } @Override public Long expire(String key, int seconds) { return jedisCluster.expire(key, seconds); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long hset(String key, String field, String value) { return jedisCluster.hset(key, field, value); } @Override public String hget(String key, String field) { return jedisCluster.hget(key, field); } @Override public Long hdel(String key, String... field) { return jedisCluster.hdel(key, field); } } package com.taotao.jedis; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class JedisClientPool implements JedisClient { @Autowired private JedisPool jedisPool; @Override public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String result = jedis.set(key, value); jedis.close(); return result; } @Override public String get(String key) { Jedis jedis = jedisPool.getResource(); String result = jedis.get(key); jedis.close(); return result; } @Override public Boolean exists(String key) { Jedis jedis = jedisPool.getResource(); Boolean result = jedis.exists(key); jedis.close(); return result; } @Override public Long expire(String key, int seconds) { Jedis jedis = jedisPool.getResource(); Long result = jedis.expire(key, seconds); jedis.close(); return result; } @Override public Long ttl(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.ttl(key); jedis.close(); return result; } @Override public Long incr(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.incr(key); jedis.close(); return result; } @Override public Long hset(String key, String field, String value) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hset(key, field, value); jedis.close(); return result; } @Override public String hget(String key, String field) { Jedis jedis = jedisPool.getResource(); String result = jedis.hget(key, field); jedis.close(); return result; } @Override public Long hdel(String key, String... field) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hdel(key, field); jedis.close(); return result; } } 
工作11年总结的常用java工具类,上百种方法,开发中绝对用得到 工作11年总结的常用java工具类,上百种方法,开发中绝对用得到&&工作11年总结的常用java工具类,上百种方法,开发中绝对用得到 立即下载

相关推荐

Java常用工具类大全,工作5年精心整理(最新版)

提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密解密,如MD5、BASE64,AES、DES等 - 4.文件操作,包括file、image、zip等 - 5.http - 6.jdbc - 7.json - 8.邮件 - 9.二维码、条形码 - 10.web

SpringBoot——关于controller参数校验

springboot参数校验;

FlyLikeButterfly的博客 7269

第10章:位同步与Gardner环路(1)

第10章(1)内容如下:一、位同步与载波同步的区别1、采样时钟相位偏差的影响2、采样时钟频率偏差的影响二、Gardner环路算法与插值方法1、插值滤波器2、Gardner误差检查算法三、Gardner环路中环路滤波器与数字振荡器设计1、环路滤波器2、数控振荡器(NCO)四、Gardner环路的可运行MATLAB代码五、参考资料六、致谢 最近搞懂了Gardner环路的一些知识,于是更新第10章——位同步。 如果你稍不留意的话,这可能是你在网上能看到的介绍Gardner环路及其MATLAB仿真最详细的一篇文章了

weixin_44283855的博客 1万+

精品新年大礼包V2008 CD DVD 全发布

精品新年大礼包V2008 CD DVD 全发布=800) window.open(http://www.0xy.cn/attachment/Mon_0801/9_12915_7e463fc6fe08e90.jpg);" src="http://www.0xy.cn/attachment/Mon_0801/9_12915_7e463fc6fe08e90.jpg" onload="if(thi

井中明月 5805

嵌入式开发也玩“全家桶”?手把手教你用CLion+PyOCD调试APM32,比OpenOCD更简单

本文详细介绍了如何使用CLion和PyOCD组合进行APM32嵌入式开发,提供从环境配置到实战调试的完整指南。相比传统工具如Keil和OpenOCD,这套方案更轻量、易用,特别适合现代开发者需求,涵盖CMake配置、PyOCD调试技巧及常见问题解决。

weixin_42543046的博客 465

Cherry Studio 新手必看:5分钟搞定MCP服务器配置(附常见问题解决)

本文为Cherry Studio新手提供了详细的MCP服务器配置指南。文章从核心概念入手,解释了Cherry Studio作为MCP客户端如何通过MCP协议安全、标准化地扩展AI助手能力。重点讲解了uv和Bun环境的安装与常见网络问题的解决方案,并演示了如何添加首个MCP服务器(如mcp-server-fetch),助你快速构建可扩展的智能工作流。

corn8的博客 596

ミスチルのアルバム『I♥U』

Mr.Children『I♥U』 ~『深海』から9年。いざ、潜水。~  待ちに待ったMr.Childrenの最新アルバムが、ついに発売日を迎えた。前にも書いたことがあるが、最も好きなミュージシャン(バンド)を1人選べと言われたら、僕は迷わず”ミスチル”を選ぶ。ミスチルのアルバムをはじめて通して聴く瞬間。それは、僕にとって、最高に胸躍るひととき。だから、ニューアルバムをはじめて聴ける今日という日は、

azikanho的专栏 2万+

Trae AI开发工具:大前端环境配置与避坑指南

大前端开发的第一步往往不是选框架,而是搭建一套可靠的工具链。IDE作为编码、调试与AI能力协同的载体,直接决定后续开发的顺畅度。传统编辑器需要手动拼装插件生态,而新一代AI开发工具将对话、代码生成与项目脚手架能力内置到编辑器底层,降低了环境准备的门槛。Trae基于VS Code内核,兼容其插件体系,同时提供Chat、Builder、内联对话三种AI交互模式,适合从VS Code迁移的开发者,也适合大前端新人快速上手。安装时需认准官方渠道,注意Node.js、Git版本联动,配置自动格式化与手动更新策略,并结

weixin_26833139的博客 282

C#,ADO封装,SqlHelper.cs

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Collections; using System.Data.Common; using System.Text; /// /// 数据库的通用访问代码 ///

angelzjk的专栏 965

PLC的模糊PID控制

模糊PID控制包括模糊化,确定模糊规则,解模糊等组成部分,小车通过传感器采集赛道信息,PLC通过温度传感器获取炉内温度,确定当前实际温度和设定目标值的偏差e(error), e=setpoint-input以及偏差的变化ec(当前偏差-上次偏差),根据给定的模糊规则进行模糊推理,最后对模糊参数进行解模糊,输出PID控制参数 其次我们需要对这2个参数进行模糊化,e 、 ec 这里我对e进行举例,两个输入的标准化范围都在 [-1,1] 范围内。模糊逻辑控制器还产生范围 [-1,1] 内的归一化输出。 博

RXXW_Dor的博客 1万+

小白也能懂!一步步教你用FPGA加速YOLO目标检测(入门篇)

安装到 PYNQ 开发板中。该库为 PYNQ 平台提供了对量化神经网络(如 YOLO)的硬件加速支持。确保你的电脑已连接到外网,如 WiFi 或热点(手机热点理论上也可用,未实测)。当你看到安装成功的提示后,打开 Jupyter Notebook,你会发现在。趁这个机会,泡了杯茶,刷了会儿手机,顺便感慨了一下开源社区的强大……页面中多出了一些新的项目文件夹 —— 这就是我们刚才安装的。参数,浅拷贝只克隆最近一次提交,减少数据量,避免超时问题。初始化,打开该文件,即可运行我们的Tiny YOLO。

FJN110的博客 1599

DA16200嵌入式Wi-Fi模块实现流式JSON解析获取实时天气

在资源受限的嵌入式设备中,Wi-Fi联网与结构化数据处理是物联网终端的核心能力。理解HTTP/HTTPS协议栈、TLS安全握手原理及内存敏感场景下的JSON解析机制,对保障低功耗设备长期稳定运行具有关键价值。尤其在MCU RAM仅128KB、网络缓冲区不足16KB的约束下,传统‘全量加载+库解析’方案失效,必须采用状态机驱动的流式解析技术,在字节流中精准捕获目标字段。该方法兼顾安全性(硬加密支持)、实时性(8秒端到端响应)与鲁棒性(弱网重连、证书OTA),广泛适用于工业传感、电子价签、智能气象站等边缘场景。

weixin_33862993的博客 394

如何使用GitHub API爬取仓库数据:完整教程及Python实现

通过本教程,你已经掌握了如何使用GitHub API爬取仓库数据,并将这些数据存储和清洗以便后续分析。我们从API认证、数据获取、存储、清洗等方面进行了详细介绍,并展示了如何利用Python进行自动化处理。希望这些代码和技术能够帮助你更好地使用GitHub API进行数据分析。

2201_76125261的博客 785

java 工具类_整理收集的一些常用java工具类

摘要: 1.json转换工具 [java] view plain copy package com.taotao.utils; import java.util.List; import com.1.json转换工具[java] view plain copypackage com.taotao.utils;import java.util.List;import com.fa...

weixin_39969953的博客 108

DaVinci配置DID之三:代码实现

本系列主要基于DaVinCi工具链来展开具体DID读写的配置以及最终实现。1,CDD准备2,工具链配置3,代码实现4,验证结果本文讲述流程3话不多说,走你~博主是AutoSar小白,如果哪里表达的有问题,还请大佬们指点指点哈。接下来,让我们在下章关于测试验证的博文再相会哈~

weixin_48498880的博客 5365

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

java工具类怎么写_整理收集的一些常用java工具类

1.json转换工具1.packagecom.taotao.utils;3.importjava.util.List;5.importcom.fasterxml.jackson.core.JsonProcessingException;6.importcom.fasterxml.jackson.databind.JavaType;7.importcom.fasterx...

weixin_36250058的博客 495

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

常用java工具类的镇整理收集

原文链接 *1.json转换工具 1. package com.taotao.utils; 3. import java.util.List; 5. import com.fasterxml.jackson.core.JsonProcessingException; 6. import com.fasterxml.jackson.databind.JavaType; 7. import ...

芒果不是程序猿v: 的博客 215

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

mysql常用的工具类_整理收集的一些常用java工具类

1.json转换工具1. package com.taotao.utils;3. import java.util.List;5. import com.fasterxml.jackson.core.JsonProcessingException;6. import com.fasterxml.jackson.databind.JavaType;7. import com.fasterx...

weixin_39996742的博客 240
上一篇: Java中的IO
下一篇: Java中final、finally和finalize的区别
java1856905
博客等级 码龄8年 100粉丝 249原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值