1.Hutool
描述:一个Java基础工具类,对文件、流、加密解密、转码、正则、线程、XML等JDK方法进行封装,组成各种Util工具类
<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.11</version>
</dependency>
可以根据需求对每个模块单独引入,也可以通过引入hutool-all
方式引入所有模块。
模块 | 介绍 |
---|---|
hutool-aop | JDK动态代理封装,提供非IOC下的切面支持 |
hutool-bloomFilter | 布隆过滤,提供一些Hash算法的布隆过滤 |
hutool-cache | 简单缓存实现 |
hutool-core | 核心,包括Bean操作、日期、各种Util等 |
hutool-cron | 定时任务模块,提供类Crontab表达式的定时任务 |
hutool-crypto | 加密解密模块,提供对称、非对称和摘要算法封装 |
hutool-db | JDBC封装后的数据操作,基于ActiveRecord思想 |
hutool-dfa | 基于DFA模型的多关键字查找 |
hutool-extra | 扩展模块,对第三方封装(模板引擎、邮件、Servlet、二维码、Emoji、FTP、分词等) |
hutool-http | 基于HttpUrlConnection的Http客户端封装 |
hutool-log | 自动识别日志实现的日志门面 |
hutool-script | 脚本执行封装,例如Javascript |
hutool-setting | 功能更强大的Setting配置文件和Properties封装 |
hutool-system | 系统参数调用封装(JVM信息等) |
hutool-json | JSON实现 |
hutool-captcha | 图片验证码实现 |
hutool-poi | 针对POI中Excel和Word的封装 |
hutool-socket | 基于Java的NIO和AIO的Socket封装 |
hutool-jwt | JSON Web Token (JWT)封装实现 |
2.DESUtils
描述:JAVA版的DES加密解密
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* JAVA版的DES加密解密
*/
public class DESUtils {
/**
* 加密
*
* @param strKey
* @param strToEncrypt
* @return
*/
public static String encrypt(String strKey, String strToEncrypt) {
String result = "";
try {
// 设置为DES/ECB/PKCS5Padding的Cipher
// ECB对应.Net的CipherMode.ECB
// PKCS5Padding对应为.NET和JAVA的默认填充方式
DESKeySpec ks = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
Cipher cip = Cipher.getInstance("DES/ECB/PKCS5Padding");//Cipher.getInstance("DES");
// 设置为加密模式, 並设置解密的key
cip.init(Cipher.ENCRYPT_MODE, sk);
//考虑复杂字符所以加密时用UTF-8转换为byte[]
result = byteToHexStr(cip.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 解密
*
* @param strKey 长度大于8
* @param strToDecrypt
* @return
*/
public static String decrypt(String strKey, String strToDecrypt) {
String result = "";
try {
// 建立解密所需的Key
DESKeySpec ks = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
// 设置为DES/ECB/PKCS5Padding的Cipher
// ECB对应.Net的CipherMode.ECB
// PKCS5Padding对应为.NET和JAVA的默认填充方式
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 设置为解密模式, 並设置解密的key
cipher.init(Cipher.DECRYPT_MODE, sk);
// 输出解密后的字串. 考虑复杂字符所以加密时用UTF-8转换为byte[], 解密时用UTF-8转换回去
// 考虑加密时可能有字符补位,String的trim()将这些空字符刪掉
result = new String(cipher.doFinal(hexStr2Byte(strToDecrypt)), "UTF-8").trim();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] hexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
//对应VB方法中的2位分段格式转换
int hex = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
result[i] = (byte) (hex);
}
return result;
}
/**
* 将二进制 转换为16进制
*
* @param byteStr
* @return
*/
public static String byteToHexStr(byte[] byteStr) {
String str = "";
for (int i = 0; i < byteStr.length; i++) {
//考虑VB中的byte与JAVA中的byte取值范围差异,这里需要进行转换
String hex = Integer.toHexString(byteStr[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
str += hex;
}
return str.toUpperCase();
}
public static void main(String[] args) {
String key = "";
String value = "";
System.out.println("解密后:" + decrypt(key, value));
System.out.println("加密后:"+encrypt(key,value));
}
}
3.DateUtils
描述:获取各种日期或时间
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 日期工具类
* @author crazyang
*/
public class DateUtils {
/**
* 获得当前日期 yyyy-MM-dd HH:mm:ss
*
* @return 2019-08-27 14:12:40
*/
public static String getCurrentTime() {
// 小写的hh取得12小时,大写的HH取的是24小时
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return df.format(date);
}
/**
* 获取系统当前时间戳
*
* @return 1566889186583
*/
public static String getSystemTime() {
String current = String.valueOf(System.currentTimeMillis());
return current;
}
/**
* 获取当前日期 yy-MM-dd
*
* @return 2019-08-27
*/
public static String getDateByString() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
/**
* 得到两个时间差 格式yyyy-MM-dd HH:mm:ss
*
* @param start 2019-06-27 14:12:40
* @param end 2019-08-27 14:12:40
* @return 5270400000
*/
public static long dateSubtraction(String start, String end) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = df.parse(start);
Date date2 = df.parse(end);
return date2.getTime() - date1.getTime();
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
/**
* 得到两个时间差
*
* @param start 开始时间
* @param end 结束时间
* @return
*/
public static long dateTogether(Date start, Date end) {
return end.getTime() - start.getTime();
}
/**
* 转化long值的日期为yyyy-MM-dd HH:mm:ss.SSS格式的日期
*
* @param millSec 日期long值 5270400000
* @return 日期,以yyyy-MM-dd HH:mm:ss.SSS格式输出 1970-03-03 08:00:00.000
*/
public static String transferLongToDate(String millSec) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = new Date(Long.parseLong(millSec));
return sdf.format(date);
}
/**
* 获得当前日期 yyyy-MM-dd HH:mm:ss
*
* @return
*/
public static String getOkDate(String date) {
try {
if (StringUtils.isEmpty(date)) {
return null;
}
Date date1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(date);
//格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date1);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前日期是一个星期的第几天
*
* @return 2
*/
public static int getDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
return cal.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
*
* @param nowTime 当前时间
* @param dateSection 时间区间 2018-01-08,2019-09-09
* @return
* @author jqlin
*/
public static boolean isEffectiveDate(Date nowTime, String dateSection) {
try {
String[] times = dateSection.split(",");
String format = "yyyy-MM-dd";
Date startTime = new SimpleDateFormat(format).parse(times[0]);
Date endTime = new SimpleDateFormat(format).parse(times[1]);
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (isSameDay(date, begin) || isSameDay(date, end)) {
return true;
}
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 判断是否两天是否是同一天
* @param cal1
* @param cal2
* @return
*/
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 != null && cal2 != null) {
return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
} else {
throw new IllegalArgumentException("The date must not be null");
}
}
/**
* 日期转时间戳(毫秒)long
* @param time
* @return
*/
public static long getTimeByDate(String time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(time);
//日期转时间戳(毫秒)
return date.getTime();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取当前小时 :2019-08-23 17
*
* @return 2019-08-27 17
*/
public static String getCurrentHour() {
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour < 10) {
return DateUtils.getCurrentTime() + " 0" + hour;
}
return DateUtils.getDateByString() + " " + hour;
}
/**
* 获取当前时间一个小时前
* @return 2019-08-27 16
*/
public static String getCurrentHourBefore() {
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour > 0) {
hour = calendar.get(Calendar.HOUR_OF_DAY) - 1;
if (hour < 10) {
return DateUtils.getDateByString() + " 0" + hour;
}
return DateUtils.getDateByString() + " " + hour;
}
//获取当前日期前一天
return DateUtils.getBeforeDay() + " " + 23;
}
/**
* 获取当前日期前一天
*
* @return 2019-08-26
*/
public static String getBeforeDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
return sdf.format(date);
}
/**
* 获取最近七天
*
* @return 2019-08-20
*/
public static String getServen() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
Date monday = c.getTime();
String preMonday = sdf.format(monday);
return preMonday;
}
/**
* 获取最近一个月
*
* @return 2019-07-27
*/
public static String getOneMonth() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
Date monday = c.getTime();
String preMonday = sdf.format(monday);
return preMonday;
}
/**
* 获取最近三个月
*
* @return 2019-05-27
*/
public static String getThreeMonth() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -3);
Date monday = c.getTime();
String preMonday = sdf.format(monday);
return preMonday;
}
/**
* 获取最近一年
*
* @return 2018-08-27
*/
public static String getOneYear() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
Date start = c.getTime();
String startDay = sdf.format(start);
return startDay;
}
private static int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
/**
* 获取今年月份数据
* 说明 有的需求前端需要根据月份查询每月数据,此时后台给前端返回今年共有多少月份
*
* @return [1, 2, 3, 4, 5, 6, 7, 8]
*/
public static List getMonthList(){
List list = new ArrayList();
for (int i = 1; i <= month; i++) {
list.add(i);
}
return list;
}
/**
* 返回当前年度季度list
* 本年度截止目前共三个季度,然后根据1,2,3分别查询相关起止时间
* @return [1, 2, 3]
*/
public static List getQuartList(){
int quart = month / 3 + 1;
List list = new ArrayList();
for (int i = 1; i <= quart; i++) {
list.add(i);
}
return list;
}
public static void main(String[] args) {
System.out.println(DateUtils.getQuartList());
}
}
4.JsonUtils
描述:json类型转换
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
</dependency>
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;
}
}
5.CookieUtils
描述:对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;
}
}
6.FtpUtil
描述:对ftp服务器文件进行操作
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.pool2.ObjectPool;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Description : Ftp工具类
* @Author : sunyiqiang
* @Date : 2021/8/30 14:21
*/
@Slf4j
public class FtpUtil {
/**
* ftpClient连接池初始化标志
*/
private static volatile boolean hasInit = false;
/**
* ftpClient连接池
*/
private static ObjectPool<FTPClient> ftpClientPool;
/**
* 初始化ftpClientPool
*
* @param ftpClientPool
*/
public static void init(ObjectPool<FTPClient> ftpClientPool) {
if (!hasInit) {
synchronized (FtpUtil.class) {
if (!hasInit) {
FtpUtil.ftpClientPool = ftpClientPool;
hasInit = true;
}
}
}
}
/**
* 按行读取FTP文件
*
* @param remoteFilePath 文件路径(path+fileName)
* @return
* @throws IOException
*/
public static List<String> readFileByLine(String remoteFilePath) throws IOException {
FTPClient ftpClient = getFtpClient();
try (InputStream in = ftpClient.retrieveFileStream(encodingPath(remoteFilePath));
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
return br.lines().map(line -> StringUtils.trimToEmpty(line))
.filter(line -> StringUtils.isNotEmpty(line)).collect(Collectors.toList());
} finally {
ftpClient.completePendingCommand();
releaseFtpClient(ftpClient);
}
}
/**
* 获取指定路径下FTP文件
*
* @param remotePath 路径
* @return FTPFile数组
* @throws IOException
*/
public static FTPFile[] retrieveFTPFiles(String remotePath) throws IOException {
FTPClient ftpClient = getFtpClient();
try {
return ftpClient.listFiles(encodingPath(remotePath + "/"),
file -> file != null && file.getSize() > 0);
} finally {
releaseFtpClient(ftpClient);
}
}
/**
* 获取指定路径下FTP文件 并 写入临时文件的outputStream中
* @param remotePath
* @param outputStream
* @return
* @throws IOException
*/
public static Boolean retrieveFile(String remotePath, OutputStream outputStream) throws IOException {
FTPClient ftpClient = getFtpClient();
try {
return ftpClient.retrieveFile(remotePath,outputStream);
} finally {
releaseFtpClient(ftpClient);
}
}
/**
* 获取指定路径下FTP文件名称
*
* @param remotePath 路径
* @return ftp文件名称列表 和 文件操作时间
* @throws IOException
*/
public static Map<String, Date> retrieveFileNames(String remotePath) throws IOException {
Map<String, Date> files = new HashMap<>();
FTPFile[] ftpFiles = retrieveFTPFiles(remotePath);
if (ArrayUtils.isEmpty(ftpFiles)) {
return new HashMap<>();
}
//防止中文乱码
for (FTPFile ftpFile:ftpFiles) {
String fileName = new String(ftpFile.getName().getBytes("iso-8859-1"), "gb2312");
Calendar fileTime = ftpFile.getTimestamp();
Date date = fileTime.getTime();
//Date 类型转换为 String 类型
String time = DateUtil.formatDateTime(date);
files.put(fileName,date);
}
return files;
}
/**
* 编码文件路径
*/
private static String encodingPath(String path) throws UnsupportedEncodingException {
// FTP协议里面,规定文件名编码为iso-8859-1,所以目录名或文件名需要转码
return new String(path.replaceAll("//", "/").getBytes("GBK"), "iso-8859-1");
}
/**
* 获取ftpClient
*
* @return
*/
private static FTPClient getFtpClient() {
checkFtpClientPoolAvailable();
FTPClient ftpClient = null;
Exception ex = null;
// 获取连接最多尝试3次
for (int i = 0; i < 3; i++) {
try {
ftpClient = ftpClientPool.borrowObject();
ftpClient.changeWorkingDirectory("/");
break;
} catch (Exception e) {
ex = e;
}
}
if (ftpClient == null) {
throw new RuntimeException("Could not get a ftpClient from the pool", ex);
}
return ftpClient;
}
/**
* 释放ftpClient
*/
private static void releaseFtpClient(FTPClient ftpClient) {
if (ftpClient == null) {
return;
}
try {
ftpClientPool.returnObject(ftpClient);
} catch (Exception e) {
log.error("Could not return the ftpClient to the pool", e);
// destoryFtpClient
if (ftpClient.isAvailable()) {
try {
ftpClient.disconnect();
} catch (IOException io) {
}
}
}
}
/**
* 检查ftpClientPool是否可用
*/
private static void checkFtpClientPoolAvailable() {
Assert.state(hasInit, "FTP未启用或连接失败!");
}
/**
* 上传Excel文件到FTP
* @param workbook
* @param remoteFilePath
* @throws IOException
*/
public static boolean uploadExcel2Ftp(Workbook workbook, String remoteFilePath)
throws IOException {
Assert.notNull(workbook, "workbook cannot be null.");
Assert.hasText(remoteFilePath, "remoteFilePath cannot be null or blank.");
FTPClient ftpClient = getFtpClient();
try (OutputStream out = ftpClient.storeFileStream(encodingPath(remoteFilePath))) {
workbook.write(out);
workbook.close();
return true;
} finally {
ftpClient.completePendingCommand();
releaseFtpClient(ftpClient);
}
}
/**
* 从ftp下载excel文件
* @param remoteFilePath
* @param response
* @throws IOException
*/
public static void downloadExcel(String remoteFilePath, HttpServletResponse response)
throws IOException {
String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
fileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
FTPClient ftpClient = getFtpClient();
try (InputStream in = ftpClient.retrieveFileStream(encodingPath(remoteFilePath));
OutputStream out = response.getOutputStream()) {
int size = 0;
byte[] buf = new byte[10240];
while ((size = in.read(buf)) > 0) {
out.write(buf, 0, size);
out.flush();
}
} finally {
ftpClient.completePendingCommand();
releaseFtpClient(ftpClient);
}
}
}
7.ServerResponse
描述:自定义响应体
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
</dependency>
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;
/**
* @Description : 自定义响应体
*/
public class ServerResponse<T> implements Serializable {
private int code;
private String msg;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private T data;
private ServerResponse(int code) {
this.code = code;
}
private ServerResponse(int code, T data) {
this.code = code;
this.data = data;
}
private ServerResponse(int code, String msg) {
this.code = code;
this.msg = msg;
}
private ServerResponse(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
/**
* 使之不在json序列化结果当中
*/
@JsonIgnore
public boolean isSuccess() {
return this.code == ResponseCode.SUCCESS.getCode();
}
public int getCode() {
return code;
}
public T getData() {
return data;
}
public String getMsg() {
return msg;
}
public static <T> ServerResponse<T> createResult(int code, String msg, T data) {
return new ServerResponse<T>(code, msg, data);
}
public static <T> ServerResponse<T> createBySuccess() {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg());
}
public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
}
public static <T> ServerResponse<T> createBySuccess(T data) {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), data);
}
public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
}
public static <T> ServerResponse<T> createByError() {
return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg());
}
public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
}
public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
return new ServerResponse<T>(errorCode, errorMessage);
}
}
/**
* @Description : 响应体枚举类
*/
public enum ResponseCode {
SUCCESS(200,"SUCCESS"),
ERROR(500,"ERROR");
private final int code;
private final String msg;
ResponseCode(int code,String msg){
this.code = code;
this.msg = msg;
}
public int getCode(){
return code;
}
public String getMsg(){
return msg;
}
}