vue版本上线
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Documented;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class AnnotationUtils {
|
||||
public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> annotationClass) {
|
||||
A res = cls.getAnnotation(annotationClass);
|
||||
if (res == null) {
|
||||
for (Annotation annotation : cls.getAnnotations()) {
|
||||
if (annotation instanceof Documented) {
|
||||
break;
|
||||
}
|
||||
res = getAnnotation(annotation.annotationType(), annotationClass);
|
||||
if (res != null)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static <T, A extends Annotation> A getAnnotation(T obj, Class<A> annotationClass) {
|
||||
return getAnnotation(obj.getClass(), annotationClass);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
public class BaseResponseInfo {
|
||||
public int code;
|
||||
public Object data;
|
||||
|
||||
public BaseResponseInfo() {
|
||||
code = 400;
|
||||
data = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ColumnPropertyUtil {
|
||||
|
||||
/**
|
||||
* 将数据库字段转换成属性
|
||||
*/
|
||||
public static String columnToProperty(String column) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 快速检查
|
||||
if (StringUtil.isEmpty(column)) {
|
||||
// 没必要转换
|
||||
return "";
|
||||
} else if (!column.contains("_")) {
|
||||
// 不做转换
|
||||
return column;
|
||||
} else {
|
||||
// 用下划线将原始字符串分割
|
||||
String[] columns = column.split("_");
|
||||
for (String columnSplit : columns) {
|
||||
// 跳过原始字符串中开头、结尾的下换线或双重下划线
|
||||
if (StringUtil.isEmpty(columnSplit)) {
|
||||
continue;
|
||||
}
|
||||
// 处理真正的驼峰片段
|
||||
if (result.length() == 0) {
|
||||
// 第一个驼峰片段,全部字母都小写
|
||||
result.append(columnSplit.toLowerCase());
|
||||
} else {
|
||||
// 其他的驼峰片段,首字母大写
|
||||
result.append(columnSplit.substring(0, 1).toUpperCase()).append(columnSplit.substring(1).toLowerCase());
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 驼峰转换下划线
|
||||
*/
|
||||
public static String propertyToColumn(String property) {
|
||||
if (StringUtil.isEmpty(property)) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder column = new StringBuilder();
|
||||
column.append(property.substring(0, 1).toLowerCase());
|
||||
for (int i = 1; i < property.length(); i++) {
|
||||
String s = property.substring(i, i + 1);
|
||||
// 在小写字母前添加下划线
|
||||
if (!Character.isDigit(s.charAt(0)) && s.equals(s.toUpperCase())) {
|
||||
column.append("_");
|
||||
}
|
||||
// 其他字符直接转成小写
|
||||
column.append(s.toLowerCase());
|
||||
}
|
||||
|
||||
return column.toString();
|
||||
}
|
||||
|
||||
}
|
||||
155
jshERP-boot/src/main/java/com/jsh/erp/utils/ComputerInfo.java
Normal file
155
jshERP-boot/src/main/java/com/jsh/erp/utils/ComputerInfo.java
Normal file
@@ -0,0 +1,155 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/*
|
||||
* <取网卡物理地址--
|
||||
* 1.在Windows,Linux系统下均可用;
|
||||
* 2.通过ipconifg,ifconfig获得计算机信息;
|
||||
* 3.再用模式匹配方式查找MAC地址,与操作系统的语言无关>
|
||||
*
|
||||
* //* Description: <取计算机名--从环境变量中取>
|
||||
* abstract 限制继承/创建实例
|
||||
*/
|
||||
public abstract class ComputerInfo {
|
||||
private static String macAddressStr = null;
|
||||
private static String computerName = System.getenv().get("COMPUTERNAME");
|
||||
|
||||
private static final String[] windowsCommand = { "ipconfig", "/all" };
|
||||
private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
|
||||
private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* 获取多个网卡地址
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private final static List<String> getMacAddressList() throws IOException {
|
||||
final ArrayList<String> macAddressList = new ArrayList<String>();
|
||||
final String os = System.getProperty("os.name");
|
||||
final String command[];
|
||||
|
||||
if (os.startsWith("Windows")) {
|
||||
command = windowsCommand;
|
||||
} else if (os.startsWith("Linux")) {
|
||||
command = linuxCommand;
|
||||
} else {
|
||||
throw new IOException("Unknow operating system:" + os);
|
||||
}
|
||||
// 执行命令
|
||||
final Process process = Runtime.getRuntime().exec(command);
|
||||
|
||||
BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
for (String line = null; (line = bufReader.readLine()) != null;) {
|
||||
Matcher matcher = macPattern.matcher(line);
|
||||
if (matcher.matches()) {
|
||||
macAddressList.add(matcher.group(1));
|
||||
// macAddressList.add(matcher.group(1).replaceAll("[-:]",
|
||||
// ""));//去掉MAC中的“-”
|
||||
}
|
||||
}
|
||||
|
||||
process.destroy();
|
||||
bufReader.close();
|
||||
return macAddressList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个网卡地址(多个网卡时从中获取一个)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getMacAddress() {
|
||||
if (macAddressStr == null || macAddressStr.equals("")) {
|
||||
StringBuffer sb = new StringBuffer(); // 存放多个网卡地址用,目前只取一个非0000000000E0隧道的值
|
||||
try {
|
||||
List<String> macList = getMacAddressList();
|
||||
for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {
|
||||
String amac = iter.next();
|
||||
if (!amac.equals("0000000000E0")) {
|
||||
sb.append(amac);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
macAddressStr = sb.toString();
|
||||
|
||||
}
|
||||
|
||||
return macAddressStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取电脑名
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getComputerName() {
|
||||
if (computerName == null || computerName.equals("")) {
|
||||
computerName = System.getenv().get("COMPUTERNAME");
|
||||
}
|
||||
return computerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端IP地址
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getIpAddrAndName() throws IOException {
|
||||
return InetAddress.getLocalHost().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端IP地址
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getIpAddr() throws IOException {
|
||||
return InetAddress.getLocalHost().getHostAddress().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取电脑唯一标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getComputerID() {
|
||||
String id = getMacAddress();
|
||||
if (id == null || id.equals("")) {
|
||||
try {
|
||||
id = getIpAddrAndName();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return computerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 限制创建实例
|
||||
*/
|
||||
private ComputerInfo() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.out.println(ComputerInfo.getMacAddress());
|
||||
System.out.println(ComputerInfo.getComputerName());
|
||||
System.out.println(ComputerInfo.getIpAddr());
|
||||
System.out.println(ComputerInfo.getIpAddrAndName());
|
||||
}
|
||||
}
|
||||
34
jshERP-boot/src/main/java/com/jsh/erp/utils/Constants.java
Normal file
34
jshERP-boot/src/main/java/com/jsh/erp/utils/Constants.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* by jishenghua qq-752718920 2018-10-7 12:01:36
|
||||
*/
|
||||
public class Constants {
|
||||
|
||||
//查询参数
|
||||
public final static String PAGE_SIZE = "pageSize";
|
||||
public final static String CURRENT_PAGE = "currentPage";
|
||||
public final static String ORDER = "order";
|
||||
public final static String FILTER = "filter";
|
||||
public final static String SPLIT = ",";
|
||||
public final static String SEARCH = "search";
|
||||
public final static String DEVICE_ID = "deviceId";
|
||||
public final static String OFFSET = "offset";
|
||||
public final static String ROWS = "rows";
|
||||
public final static String IS_RECURSION = "isRecursion";
|
||||
public final static String IS_RECURSION_VALUE = "1";
|
||||
public final static String IS_QUERYBYNODEID = "isquerybyid";
|
||||
public final static String IS_QUERYBYNODEID_VALUE = "1";
|
||||
|
||||
//级联类别
|
||||
public final static String TYPE = "type";
|
||||
|
||||
//转发
|
||||
public final static String TEAM = "team";
|
||||
|
||||
//增加了角色等级常量
|
||||
public final static String LEVEL="level";
|
||||
|
||||
}
|
||||
37
jshERP-boot/src/main/java/com/jsh/erp/utils/ErpInfo.java
Normal file
37
jshERP-boot/src/main/java/com/jsh/erp/utils/ErpInfo.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public enum ErpInfo {
|
||||
//通过构造传递参数
|
||||
OK(200, "成功"),
|
||||
BAD_REQUEST(400, "请求错误或参数错误"),
|
||||
UNAUTHORIZED(401, "未认证用户"),
|
||||
INVALID_VERIFY_CODE(461, "错误的验证码"),
|
||||
ERROR(500, "服务内部错误"),
|
||||
WARING_MSG(201, "提醒信息"),
|
||||
REDIRECT(301, "session失效,重定向"),
|
||||
FORWARD_REDIRECT(302, "转发请求session失效"),
|
||||
FORWARD_FAILED(303, "转发请求失败!"),
|
||||
TEST_USER(-1, "演示用户禁止操作");
|
||||
|
||||
public final int code;
|
||||
public final String name;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义枚举构造函数
|
||||
*/
|
||||
ErpInfo(int code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
240
jshERP-boot/src/main/java/com/jsh/erp/utils/ExcelUtils.java
Normal file
240
jshERP-boot/src/main/java/com/jsh/erp/utils/ExcelUtils.java
Normal file
@@ -0,0 +1,240 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import jxl.Cell;
|
||||
import jxl.Sheet;
|
||||
import jxl.Workbook;
|
||||
import jxl.format.*;
|
||||
import jxl.write.Label;
|
||||
import jxl.write.WritableCellFormat;
|
||||
import jxl.write.WritableFont;
|
||||
import jxl.write.WritableSheet;
|
||||
import jxl.write.WritableWorkbook;
|
||||
|
||||
public class ExcelUtils {
|
||||
|
||||
public static WritableFont arial14font = null;
|
||||
|
||||
public static File exportObjects(String fileName, String[] names,
|
||||
String title, List<String[]> objects) throws Exception {
|
||||
File excelFile = new File("fileName.xls");
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15);
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setAlignment(Alignment.CENTRE);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
// CellView cellView = new CellView();
|
||||
// cellView.setAutosize(true); //设置自动大小
|
||||
format.setAlignment(Alignment.LEFT);
|
||||
format.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
sheet.mergeCells(0, 0, names.length - 1, 0);
|
||||
sheet.addCell(new Label(0, 0, title, wcfFC));
|
||||
int rowNum = 2;
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 1, names[i], format));
|
||||
}
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel,不需要第一行的title
|
||||
*
|
||||
* @param fileName
|
||||
* @param names
|
||||
* @param title
|
||||
* @param objects
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static File exportObjectsWithoutTitle(String fileName,
|
||||
String[] names, String title, List<String[]> objects)
|
||||
throws Exception {
|
||||
File excelFile = new File(fileName);
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
|
||||
// 第一行的格式
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 15,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
// 设置字体以及单元格格式
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15);
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
format.setAlignment(Alignment.LEFT);
|
||||
format.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
|
||||
// 第一行写入标题
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 0, names[i], wcfFC));
|
||||
}
|
||||
|
||||
// 其余行依次写入数据
|
||||
int rowNum = 1;
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile;
|
||||
}
|
||||
|
||||
public static String createTempFile(String[] names, String title, List<String[]> objects) throws Exception {
|
||||
File excelFile = File.createTempFile(System.currentTimeMillis() + "", ".xls");
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15);
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setAlignment(Alignment.CENTRE);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
// CellView cellView = new CellView();
|
||||
// cellView.setAutosize(true); //设置自动大小
|
||||
format.setAlignment(Alignment.LEFT);
|
||||
format.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
sheet.mergeCells(0, 0, names.length - 1, 0);
|
||||
sheet.addCell(new Label(0, 0, title, wcfFC));
|
||||
int rowNum = 2;
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 1, names[i], format));
|
||||
}
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile.getName();
|
||||
}
|
||||
|
||||
public static String createCheckRandomTempFile(String[] names, String title, List<String[]> objects,Map<String,String> infoMap) throws Exception {
|
||||
File excelFile = File.createTempFile(System.currentTimeMillis() + "", ".xls");
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 14);
|
||||
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
format.setBorder(Border.ALL, BorderLineStyle.THIN);
|
||||
format.setAlignment(Alignment.CENTRE);
|
||||
format.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setAlignment(Alignment.LEFT);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
WritableFont nameWfc = new WritableFont(WritableFont.ARIAL, 14,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat nameFormat = new WritableCellFormat(nameWfc);
|
||||
nameFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
|
||||
nameFormat.setAlignment(Alignment.CENTRE);
|
||||
nameFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
WritableCellFormat infoFormat = new WritableCellFormat(wfont);
|
||||
infoFormat.setAlignment(Alignment.LEFT);
|
||||
infoFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
|
||||
sheet.mergeCells(0, 0, names.length - 1, 0);
|
||||
sheet.addCell(new Label(0, 0, infoMap.get("title"), wcfFC));
|
||||
|
||||
sheet.addCell(new Label(0, 2, infoMap.get("info"), infoFormat));
|
||||
sheet.addCell(new Label(2, 2, infoMap.get("dvrnvr"), infoFormat));
|
||||
sheet.addCell(new Label(4, 2, infoMap.get("char"), infoFormat));
|
||||
sheet.addCell(new Label(0, 3, infoMap.get("infoPercent"), infoFormat));
|
||||
sheet.addCell(new Label(2, 3, infoMap.get("dvrnvrPercent"), infoFormat));
|
||||
sheet.addCell(new Label(4, 3, infoMap.get("charPercent"), infoFormat));
|
||||
|
||||
int rowNum = 5;
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 4, names[i], nameFormat));
|
||||
}
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile.getName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getContent(Sheet src, int rowNum, int colNum) {
|
||||
return src.getRow(rowNum)[colNum].getContents().trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从第i行开始到最后检测指定列的唯一性
|
||||
*
|
||||
* @param src
|
||||
* @param colNum
|
||||
* @param fromRow
|
||||
* 起始行
|
||||
* @return
|
||||
*/
|
||||
public static Boolean checkUnique(Sheet src, int colNum, int fromRow) {
|
||||
Cell[] colCells = src.getColumn(colNum);
|
||||
Set<String> set = new HashSet<String>();
|
||||
for (int i = fromRow; i < colCells.length; i++) {
|
||||
if (!StringUtils.isEmpty(colCells[i].getContents())
|
||||
&& !set.add(colCells[i].getContents())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static File getTempFile(String fileName) {
|
||||
String dir = System.getProperty("java.io.tmpdir"); // 获取系统临时目录
|
||||
return new File(dir + File.separator + fileName);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String msg = "12345";
|
||||
System.out.println(msg.indexOf("@"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
public interface ExceptionCodeConstants {
|
||||
/**
|
||||
* 用户错误码定义
|
||||
*/
|
||||
public class UserExceptionCode {
|
||||
/**
|
||||
* 用户不存在
|
||||
*/
|
||||
public static final int USER_NOT_EXIST = 1;
|
||||
|
||||
/**
|
||||
* 用户密码错误
|
||||
*/
|
||||
public static final int USER_PASSWORD_ERROR = 2;
|
||||
|
||||
/**
|
||||
* 被加入黑名单
|
||||
*/
|
||||
public static final int BLACK_USER = 3;
|
||||
|
||||
/**
|
||||
* 可以登录
|
||||
*/
|
||||
public static final int USER_CONDITION_FIT = 4;
|
||||
|
||||
/**
|
||||
* 访问数据库异常
|
||||
*/
|
||||
public static final int USER_ACCESS_EXCEPTION = 5;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ExportExecUtil {
|
||||
|
||||
public static void showExec(File excelFile,String fileName,HttpServletResponse response) throws Exception{
|
||||
response.setContentType("application/octet-stream");
|
||||
fileName = new String(fileName.getBytes("gbk"),"ISO8859_1");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
|
||||
FileInputStream fis = new FileInputStream(excelFile);
|
||||
OutputStream out = response.getOutputStream();
|
||||
|
||||
int SIZE = 1024 * 1024;
|
||||
byte[] bytes = new byte[SIZE];
|
||||
int LENGTH = -1;
|
||||
while((LENGTH = fis.read(bytes)) != -1){
|
||||
out.write(bytes,0,LENGTH);
|
||||
}
|
||||
|
||||
out.flush();
|
||||
fis.close();
|
||||
}
|
||||
|
||||
}
|
||||
141
jshERP-boot/src/main/java/com/jsh/erp/utils/ExtJsonUtils.java
Normal file
141
jshERP-boot/src/main/java/com/jsh/erp/utils/ExtJsonUtils.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
|
||||
import com.alibaba.fastjson.parser.deserializer.FieldDeserializer;
|
||||
import com.alibaba.fastjson.serializer.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ExtJsonUtils {
|
||||
private static class NPFloatCodec extends FloatCodec {
|
||||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
|
||||
SerializeWriter out = serializer.getWriter();
|
||||
|
||||
if (object == null) {
|
||||
if (serializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
|
||||
out.write('0');
|
||||
} else {
|
||||
out.writeNull();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
float floatValue = (Float) object;
|
||||
|
||||
if (Float.isNaN(floatValue)) {
|
||||
out.writeNull();
|
||||
} else if (Float.isInfinite(floatValue)) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
String floatText = Float.toString(floatValue);
|
||||
out.write(floatText);
|
||||
|
||||
if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
|
||||
out.write('F');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class NPDoubleSerializer extends DoubleSerializer {
|
||||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
|
||||
SerializeWriter out = serializer.getWriter();
|
||||
|
||||
if (object == null) {
|
||||
if (!serializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
out.write('0');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
double doubleValue = (Double) object;
|
||||
|
||||
if (Double.isNaN(doubleValue)) {
|
||||
out.writeNull();
|
||||
} else if (Double.isInfinite(doubleValue)) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
String doubleText;
|
||||
doubleText = Double.toString(doubleValue);
|
||||
out.append(doubleText);
|
||||
|
||||
if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
|
||||
out.write('D');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final String EXT_NAME = "ext";
|
||||
|
||||
static class ExtFilter extends AfterFilter implements PropertyFilter {
|
||||
static {
|
||||
SerializeConfig.getGlobalInstance().put(Float.class, new NPFloatCodec());
|
||||
SerializeConfig.getGlobalInstance().put(float.class, new NPFloatCodec());
|
||||
SerializeConfig.getGlobalInstance().put(Double.class, new NPDoubleSerializer());
|
||||
SerializeConfig.getGlobalInstance().put(double.class, new NPDoubleSerializer());
|
||||
}
|
||||
|
||||
private Map<Object, JSONObject> map = new HashMap<>();
|
||||
|
||||
private Map<Object, Set<String>> ignoredKey = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean apply(Object object, String name, Object value) {
|
||||
if (name.equals(EXT_NAME) && value instanceof String) {
|
||||
map.put(object, JSON.parseObject((String) value));
|
||||
return false;
|
||||
}
|
||||
if (!map.containsKey(object)) {
|
||||
ignoredKey.put(object, new HashSet<String>());
|
||||
}
|
||||
ignoredKey.get(object).add(name);
|
||||
// if (value instanceof Float || value instanceof Double) {
|
||||
// if (!floatMap.containsKey(object)) {
|
||||
// floatMap.put(object, new HashMap<String, Object>());
|
||||
// }
|
||||
// floatMap.get(object).put(name, value);
|
||||
// return false;
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeAfter(Object object) {
|
||||
if (map.containsKey(object)) {
|
||||
Set<String> ignoredKeys;
|
||||
if (ignoredKey.containsKey(object)) {
|
||||
ignoredKeys = ignoredKey.get(object);
|
||||
} else {
|
||||
ignoredKeys = new HashSet<>();
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : map.get(object).entrySet()) {
|
||||
if (!ignoredKeys.contains(entry.getKey())) {
|
||||
writeKeyValue(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String toJSONString(Object object) {
|
||||
return JSON.toJSONString(object, new ExtFilter());
|
||||
}
|
||||
|
||||
public interface ExtExtractor {
|
||||
String getExt(Object bean);
|
||||
}
|
||||
}
|
||||
333
jshERP-boot/src/main/java/com/jsh/erp/utils/FileUtils.java
Normal file
333
jshERP-boot/src/main/java/com/jsh/erp/utils/FileUtils.java
Normal file
@@ -0,0 +1,333 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* 文件处理工具类
|
||||
*
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* 功 能: 创建文件夹
|
||||
*
|
||||
* @param path
|
||||
* 参 数:要创建的文件夹名称
|
||||
* @return 返回值: 如果成功true;否则false 如:FileUtils.mkdir("/usr/apps/upload/");
|
||||
*/
|
||||
public static boolean makedir(String path) {
|
||||
File file = new File(path);
|
||||
if (!file.exists())
|
||||
return file.mkdirs();
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
*
|
||||
* @param stream
|
||||
* @param path
|
||||
* 存放路径
|
||||
* @param filename
|
||||
* 文件名
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void SaveFileFromInputStream(InputStream stream, String path, String filename)
|
||||
throws IOException {
|
||||
File file = new File(path);
|
||||
boolean flag=true;
|
||||
if(!file.exists()){
|
||||
flag=file.mkdirs();
|
||||
}
|
||||
if(flag){
|
||||
FileOutputStream fs = new FileOutputStream(new File(path+filename));
|
||||
byte[] buffer = new byte[1024 * 1024];
|
||||
int byteread = 0;
|
||||
while ((byteread = stream.read(buffer)) != -1) {
|
||||
fs.write(buffer, 0, byteread);
|
||||
fs.flush();
|
||||
}
|
||||
fs.close();
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 列出某个目录下的所有文件,子目录不列出
|
||||
* @param folderPath:文件夹路径
|
||||
* @return
|
||||
*/
|
||||
public static List<String> listFile(String folderPath){
|
||||
List<String> fileList = new ArrayList<String>(); //FileViewer.getListFiles(destPath, null, false);
|
||||
File f = new File(folderPath);
|
||||
File[] t = f.listFiles();
|
||||
for(int i = 0; i < t.length; i++){
|
||||
fileList.add(t[i].getAbsolutePath());
|
||||
}
|
||||
return fileList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断文件是否存在
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static boolean exists(String fileName) {
|
||||
File file = new File(fileName);
|
||||
if (file.exists()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取当前路径
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentPath() {
|
||||
File directory = new File(".");
|
||||
String nowPath = "";
|
||||
try {
|
||||
nowPath = directory.getCanonicalFile().toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nowPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件扩展名
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
* */
|
||||
public static String getFileExtendName(String fileName) {
|
||||
if (fileName == null) {
|
||||
return "";
|
||||
} else {
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1, fileName
|
||||
.length());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新文件,如果存在则报错
|
||||
*
|
||||
* @param filePath
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static void createFile(String filePath, String fileName)
|
||||
throws RuntimeException {
|
||||
String file = null;
|
||||
if (filePath == null) {
|
||||
file = fileName;
|
||||
} else {
|
||||
file = filePath + File.separator + fileName;
|
||||
}
|
||||
createFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新文件(含路径),如果存在则报错
|
||||
*
|
||||
* @param fileName
|
||||
* 含有路径的文件名
|
||||
* @return
|
||||
*/
|
||||
public static void createFile(String fileName) throws RuntimeException {
|
||||
File f = new File(fileName);
|
||||
if (f.exists()) {
|
||||
throw new RuntimeException("FILE_EXIST_ERROR");
|
||||
} else {
|
||||
try {
|
||||
File fileFolder = f.getParentFile();
|
||||
if (!fileFolder.exists())
|
||||
fileFolder.mkdirs();
|
||||
f.createNewFile();
|
||||
} catch (IOException ie) {
|
||||
System.out.println("文件" + fileName + "创建失败:" + ie.getMessage());
|
||||
throw new RuntimeException("FILE_CREATE_ERROR");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建目录,如果存在则不创建
|
||||
*
|
||||
* @param path
|
||||
* @return 返回结果null则创建成功,否则返回的是错误信息
|
||||
* @return
|
||||
*/
|
||||
public static String createDir(String path, boolean isCreateSubPah) {
|
||||
String msg = null;
|
||||
File dir = new File(path);
|
||||
|
||||
if (dir == null) {
|
||||
msg = "不能创建空目录";
|
||||
return msg;
|
||||
}
|
||||
if (dir.isFile()) {
|
||||
msg = "已有同名文件存在";
|
||||
return msg;
|
||||
}
|
||||
if (!dir.exists()) {
|
||||
if (isCreateSubPah && !dir.mkdirs()) {
|
||||
msg = "目录创建失败,原因不明";
|
||||
} else if (!dir.mkdir()) {
|
||||
msg = "目录创建失败,原因不明";
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定目录或文件。 如果要删除是目录,同时删除子目录下所有的文件
|
||||
*
|
||||
* @file:File 目录
|
||||
* */
|
||||
public static void delFileOrFolder(String fileName) {
|
||||
if (!exists(fileName))
|
||||
return;
|
||||
File file = new File(fileName);
|
||||
delFileOrFolder(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定目录或文件。 如果要删除是目录,同时删除子目录下所有的文件
|
||||
*
|
||||
* @file:File 目录
|
||||
* */
|
||||
public static void delFileOrFolder(File file) {
|
||||
if (!file.exists())
|
||||
return;
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
} else {
|
||||
File[] sub = file.listFiles();
|
||||
if (sub == null || sub.length <= 0) {
|
||||
file.delete();
|
||||
} else {
|
||||
for (int i = 0; i < sub.length; i++) {
|
||||
delFileOrFolder(sub[i]);
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Properties格式配置文件中获取所有参数并保存到HashMap中。
|
||||
* 配置中的key值即map表中的key值,如果配置文件保存时用的中文,则返回结果也会转成中文。
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static HashMap readPropertyFile(String file, String charsetName) throws IOException {
|
||||
if (charsetName==null || charsetName.trim().length()==0){
|
||||
charsetName="gbk";
|
||||
}
|
||||
HashMap map = new HashMap();
|
||||
InputStream is =null;
|
||||
if(file.startsWith("file:"))
|
||||
is=new FileInputStream(new File(file.substring(5)));
|
||||
else
|
||||
is=FileUtils.class.getClassLoader().getResourceAsStream(file);
|
||||
Properties properties = new Properties();
|
||||
properties.load(is);
|
||||
Enumeration en = properties.propertyNames();
|
||||
while (en.hasMoreElements()) {
|
||||
String key = (String) en.nextElement();
|
||||
String code = new String(properties.getProperty(key).getBytes(
|
||||
"ISO-8859-1"), charsetName);
|
||||
map.put(key, code);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param path
|
||||
* 文件路径
|
||||
* @param suffix
|
||||
* 后缀名
|
||||
* @param isdepth
|
||||
* 是否遍历子目录
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List getListFiles(String path, String suffix, boolean isdepth) {
|
||||
File file = new File(path);
|
||||
return FileUtils.listFile(file, suffix, isdepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param f
|
||||
* @param suffix:后缀名
|
||||
* @param isdepth:是否遍历子目录
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List listFile(File f, String suffix, boolean isdepth) {
|
||||
// 是目录,同时需要遍历子目录
|
||||
List<String> fileList = new ArrayList<String>();
|
||||
if (f.isDirectory() && isdepth == true) {
|
||||
File[] t = f.listFiles();
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
listFile(t[i], suffix, isdepth);
|
||||
}
|
||||
} else {
|
||||
String filePath = f.getAbsolutePath();
|
||||
|
||||
if (suffix != null) {
|
||||
int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引
|
||||
String tempsuffix = "";
|
||||
|
||||
if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件
|
||||
{
|
||||
tempsuffix = filePath.substring(begIndex + 1, filePath
|
||||
.length());
|
||||
}
|
||||
|
||||
if (tempsuffix.equals(suffix)) {
|
||||
fileList.add(filePath);
|
||||
}
|
||||
} else {
|
||||
// 后缀名为null则为所有文件
|
||||
fileList.add(filePath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return fileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法追加文件:使用FileWriter
|
||||
*
|
||||
* @param fileName
|
||||
* @param content
|
||||
*/
|
||||
public static void appendMethod(String fileName, String content) {
|
||||
try {
|
||||
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
|
||||
FileWriter writer = new FileWriter(fileName, true);
|
||||
writer.write(content + "\r\n");
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
88
jshERP-boot/src/main/java/com/jsh/erp/utils/HttpClient.java
Normal file
88
jshERP-boot/src/main/java/com/jsh/erp/utils/HttpClient.java
Normal file
@@ -0,0 +1,88 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.EntityBuilder;
|
||||
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.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.apache.http.HttpStatus.SC_OK;
|
||||
|
||||
public final class HttpClient {
|
||||
private static Logger logger = LoggerFactory.getLogger(HttpClient.class);
|
||||
|
||||
private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(10000).build();
|
||||
|
||||
/**
|
||||
* 采用Get方式发送请求,获取响应数据
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject httpGet(String url){
|
||||
CloseableHttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(REQUEST_CONFIG);
|
||||
try {
|
||||
CloseableHttpResponse chr = client.execute(httpGet);
|
||||
int statusCode = chr.getStatusLine().getStatusCode();
|
||||
if (SC_OK != statusCode) {
|
||||
throw new RuntimeException(String.format("%s查询出现异常", url));
|
||||
}
|
||||
String entity = EntityUtils.toString(chr.getEntity(), StandardCharsets.UTF_8);
|
||||
JSONObject object = JSONObject.parseObject(entity);
|
||||
return object;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(String.format("%s", url) + "查询出现异常");
|
||||
} finally {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 采用Post方式发送请求,获取响应数据
|
||||
*
|
||||
* @param url url地址
|
||||
* @param param 参数值键值对的字符串
|
||||
* @return
|
||||
*/
|
||||
public static String httpPost(String url, String param) {
|
||||
CloseableHttpClient client = HttpClientBuilder.create().build();
|
||||
try {
|
||||
HttpPost post = new HttpPost(url);
|
||||
EntityBuilder builder = EntityBuilder.create();
|
||||
builder.setContentType(ContentType.APPLICATION_JSON);
|
||||
builder.setText(param);
|
||||
post.setEntity(builder.build());
|
||||
|
||||
CloseableHttpResponse response = client.execute(post);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
String data = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
logger.info("状态:"+statusCode+"数据:"+data);
|
||||
return data;
|
||||
} catch(Exception e){
|
||||
throw new RuntimeException(e.getMessage());
|
||||
} finally {
|
||||
try{
|
||||
client.close();
|
||||
}catch(Exception ex){ }
|
||||
}
|
||||
}
|
||||
}
|
||||
22
jshERP-boot/src/main/java/com/jsh/erp/utils/JsonUtils.java
Normal file
22
jshERP-boot/src/main/java/com/jsh/erp/utils/JsonUtils.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by jishenghua 2018-5-11 09:48:08
|
||||
*
|
||||
* @author jishenghua
|
||||
*/
|
||||
public class JsonUtils {
|
||||
|
||||
public static JSONObject ok(){
|
||||
JSONObject obj = new JSONObject();
|
||||
JSONObject tmp = new JSONObject();
|
||||
tmp.put("message", "成功");
|
||||
obj.put("code", 200);
|
||||
obj.put("data", tmp);
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
69
jshERP-boot/src/main/java/com/jsh/erp/utils/OrderUtils.java
Normal file
69
jshERP-boot/src/main/java/com/jsh/erp/utils/OrderUtils.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class OrderUtils {
|
||||
|
||||
/**
|
||||
* 将指定字段排序
|
||||
*
|
||||
* @param orders 格式 属性名,排序方式 例如( name,asc或ip,desc)
|
||||
* @return 排序字符串 例如:(name asc 或 ip desc)
|
||||
*/
|
||||
public static String getOrderString(String orders) {
|
||||
if (StringUtil.isNotEmpty(orders)) {
|
||||
String[] splits = orders.split(Constants.SPLIT);
|
||||
if (splits.length == 2) {
|
||||
String column = ColumnPropertyUtil.propertyToColumn(splits[0]);
|
||||
if (column.equals("audit_status")) {
|
||||
// TODO: 2015/12/24 这么处理不好,得相伴办法调整
|
||||
return "IF(`audit_status`=3,-1,`audit_status`) " + splits[1];
|
||||
} else if (column.equals("create_time") || column.equals("modify_time")) {
|
||||
// TODO: 2015/12/24 这么处理不好,得相伴办法调整
|
||||
return column + " " + splits[1];
|
||||
} else {
|
||||
return "convert(" + column + " using gbk) " + splits[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getJoinTablesOrderString(String orders, String tableName) {
|
||||
if (StringUtil.isNotEmpty(orders)) {
|
||||
String[] splits = orders.split(Constants.SPLIT);
|
||||
if (splits.length == 2) {
|
||||
return "convert(" + tableName + "." + ColumnPropertyUtil.propertyToColumn(splits[0]) + " using gbk) " + splits[1];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将指定字段排序
|
||||
* inet_aton:mysql将IP 转成 long类别函数
|
||||
*
|
||||
* @param orders 格式 属性名,排序方式 例如( name,asc或ip,desc)
|
||||
* @param ipPropertyName 如果需要按IP属性排序,需要将属性名传入(可不传)
|
||||
* @return 排序字符串 例如:(name asc 或 ip desc)
|
||||
*/
|
||||
public static String getOrderString(String orders, String... ipPropertyName) {
|
||||
if (StringUtil.isNotEmpty(orders)) {
|
||||
String[] splits = orders.split(Constants.SPLIT);
|
||||
if (splits.length == 2) {
|
||||
String column = ColumnPropertyUtil.propertyToColumn(splits[0]);
|
||||
if (ipPropertyName != null && ipPropertyName.length > 0) {
|
||||
for (String ip : ipPropertyName) {
|
||||
if (ip.equals(column)) {
|
||||
return "inet_aton(" + column + ") " + splits[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return column + " " + splits[1];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页查询结果
|
||||
*
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class PageQueryInfo {
|
||||
|
||||
private Long total;
|
||||
private List<?> rows;
|
||||
|
||||
public Long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(Long total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public List<?> getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void setRows(List<?> rows) {
|
||||
this.rows = rows;
|
||||
}
|
||||
}
|
||||
57
jshERP-boot/src/main/java/com/jsh/erp/utils/ParamUtils.java
Normal file
57
jshERP-boot/src/main/java/com/jsh/erp/utils/ParamUtils.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ParamUtils {
|
||||
public static String getPageOffset(Integer currentPage, Integer pageSize) {
|
||||
if (currentPage != null && pageSize != null) {
|
||||
int offset = (currentPage - 1) * pageSize;
|
||||
if (offset <= 0) {
|
||||
return "0";
|
||||
} else {
|
||||
return new StringBuffer().append(offset).toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Integer getNumberPageOffset(Integer currentPage, Integer pageSize) {
|
||||
if (currentPage != null && pageSize != null) {
|
||||
int offset = (currentPage - 1) * pageSize;
|
||||
if (offset <= 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Integer getNumberPageRows(Integer currentPage, Integer pageSize) {
|
||||
if (currentPage != null && pageSize != null) {
|
||||
int rows = (currentPage) * pageSize;
|
||||
if (rows <= 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static HashMap<String, String> requestToMap(HttpServletRequest request) {
|
||||
|
||||
HashMap<String, String> parameterMap = new HashMap<String, String>();
|
||||
Enumeration<String> names = request.getParameterNames();
|
||||
if (names != null) {
|
||||
for (String name : Collections.list(names)) {
|
||||
parameterMap.put(name, request.getParameter(name));
|
||||
}
|
||||
}
|
||||
return parameterMap;
|
||||
}
|
||||
}
|
||||
142
jshERP-boot/src/main/java/com/jsh/erp/utils/QueryUtils.java
Normal file
142
jshERP-boot/src/main/java/com/jsh/erp/utils/QueryUtils.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.jsh.erp.utils.Constants.CURRENT_PAGE;
|
||||
import static com.jsh.erp.utils.Constants.PAGE_SIZE;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class QueryUtils {
|
||||
public static String filterSqlSpecialChar(String search) {
|
||||
return search != null ? search
|
||||
.replaceAll("_", "\\\\_")
|
||||
.replaceAll("!", "\\\\!")
|
||||
.replaceAll("\\[", "\\\\[")
|
||||
.replaceAll("\\]", "\\\\]")
|
||||
.replaceAll("\\^", "\\\\^") : null;
|
||||
}
|
||||
|
||||
public static <T> T list2One(List<T> list, String label) {
|
||||
Assert.notNull(label);
|
||||
Assert.notEmpty(list, label + "对应的记录不存在");
|
||||
Assert.isTrue(list.size() == 1, label + "对应的记录不止一个");
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
public static <T> T list2One(List<T> list, String label, T defaultValue) {
|
||||
Assert.notNull(list);
|
||||
Assert.notNull(label);
|
||||
if (list.isEmpty())
|
||||
return defaultValue;
|
||||
else {
|
||||
Assert.isTrue(list.size() == 1, label + "对应的记录不止一个");
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> search(Map<String, String> map) {
|
||||
List<String> search = null;
|
||||
|
||||
String str = map.get(Constants.SEARCH);
|
||||
if (StringUtil.isNotEmpty(str)) {
|
||||
search = StringUtil.searchCondition(str);
|
||||
}
|
||||
return search;
|
||||
}
|
||||
|
||||
public static int rows(Map<String, String> map) {
|
||||
return Integer.parseInt(map.get(PAGE_SIZE));
|
||||
}
|
||||
|
||||
public static int offset(Map<String, String> map) {
|
||||
return (currentPage(map) - 1) * pageSize(map);
|
||||
}
|
||||
|
||||
public static int pageSize(Map<String, String> map) {
|
||||
return Integer.parseInt(map.get(PAGE_SIZE));
|
||||
}
|
||||
|
||||
public static int currentPage(Map<String, String> map) {
|
||||
int val = Integer.parseInt(map.get(CURRENT_PAGE));
|
||||
if (val < 1)
|
||||
throw new RuntimeException("当前页数目:" + val + " 必须大于0");
|
||||
return val;
|
||||
}
|
||||
|
||||
public static String order(Map<String, String> map) {
|
||||
String orderString = OrderUtils.getOrderString(map.get(Constants.ORDER));
|
||||
return orderString.trim().isEmpty() ? null : orderString;
|
||||
}
|
||||
|
||||
public static Integer level(Map<String, String> map) {
|
||||
String levelString = map.get(Constants.LEVEL);
|
||||
return StringUtil.isEmpty(levelString) ? null : Integer.parseInt(levelString);
|
||||
}
|
||||
|
||||
public static boolean isRecursion(Map<String, String> map) {
|
||||
String isRecursion = map.get(Constants.IS_RECURSION);
|
||||
return StringUtil.isNotEmpty(isRecursion) && Constants.IS_RECURSION_VALUE.equals(isRecursion);
|
||||
}
|
||||
|
||||
public static int type(Map<String, String> map) {
|
||||
return Integer.parseInt(map.get(Constants.TYPE));
|
||||
}
|
||||
|
||||
public static String filter(Map<String, String> map) {
|
||||
if (map.containsKey(Constants.FILTER)) {
|
||||
JSONArray array = JSON.parseArray(map.get(Constants.FILTER));
|
||||
if (array.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
boolean first = true;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int idx = 0; idx < array.size(); ++idx) {
|
||||
JSONObject object = array.getJSONObject(idx);
|
||||
if (object.get("value") instanceof JSONArray) {
|
||||
|
||||
JSONArray value = object.getJSONArray("value");
|
||||
|
||||
if (!value.isEmpty()) {
|
||||
if (!first) {
|
||||
builder.append(" AND ");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
|
||||
String key = object.getString("name");
|
||||
|
||||
builder.append("(");
|
||||
|
||||
builder.append("`").append(key).append("`");
|
||||
|
||||
builder.append(" IN ");
|
||||
|
||||
builder.append("(");
|
||||
|
||||
for (int vidx = 0; vidx < value.size(); ++vidx) {
|
||||
if (vidx != 0) {
|
||||
builder.append(",");
|
||||
}
|
||||
builder.append(value.getString(vidx));
|
||||
}
|
||||
builder.append(")");
|
||||
|
||||
builder.append(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
140
jshERP-boot/src/main/java/com/jsh/erp/utils/RandImageUtil.java
Normal file
140
jshERP-boot/src/main/java/com/jsh/erp/utils/RandImageUtil.java
Normal file
@@ -0,0 +1,140 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 登录验证码工具类
|
||||
*/
|
||||
public class RandImageUtil {
|
||||
|
||||
public static final String key = "JEECG_LOGIN_KEY";
|
||||
|
||||
/**
|
||||
* 定义图形大小
|
||||
*/
|
||||
private static final int width = 105;
|
||||
/**
|
||||
* 定义图形大小
|
||||
*/
|
||||
private static final int height = 35;
|
||||
|
||||
/**
|
||||
* 定义干扰线数量
|
||||
*/
|
||||
private static final int count = 200;
|
||||
|
||||
/**
|
||||
* 干扰线的长度=1.414*lineWidth
|
||||
*/
|
||||
private static final int lineWidth = 2;
|
||||
|
||||
/**
|
||||
* 图片格式
|
||||
*/
|
||||
private static final String IMG_FORMAT = "JPEG";
|
||||
|
||||
/**
|
||||
* base64 图片前缀
|
||||
*/
|
||||
private static final String BASE64_PRE = "data:image/jpg;base64,";
|
||||
|
||||
/**
|
||||
* 直接通过response 返回图片
|
||||
* @param response
|
||||
* @param resultCode
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void generate(HttpServletResponse response, String resultCode) throws IOException {
|
||||
BufferedImage image = getImageBuffer(resultCode);
|
||||
// 输出图象到页面
|
||||
ImageIO.write(image, IMG_FORMAT, response.getOutputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成base64字符串
|
||||
* @param resultCode
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String generate(String resultCode) throws IOException {
|
||||
BufferedImage image = getImageBuffer(resultCode);
|
||||
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||
//写入流中
|
||||
ImageIO.write(image, IMG_FORMAT, byteStream);
|
||||
//转换成字节
|
||||
byte[] bytes = byteStream.toByteArray();
|
||||
//转换成base64串
|
||||
String base64 = Base64.getEncoder().encodeToString(bytes).trim();
|
||||
base64 = base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
|
||||
|
||||
//写到指定位置
|
||||
//ImageIO.write(bufferedImage, "png", new File(""));
|
||||
|
||||
return BASE64_PRE+base64;
|
||||
}
|
||||
|
||||
private static BufferedImage getImageBuffer(String resultCode){
|
||||
// 在内存中创建图象
|
||||
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
// 获取图形上下文
|
||||
final Graphics2D graphics = (Graphics2D) image.getGraphics();
|
||||
// 设定背景颜色
|
||||
graphics.setColor(Color.WHITE); // ---1
|
||||
graphics.fillRect(0, 0, width, height);
|
||||
// 设定边框颜色
|
||||
// graphics.setColor(getRandColor(100, 200)); // ---2
|
||||
graphics.drawRect(0, 0, width - 1, height - 1);
|
||||
|
||||
final Random random = new Random();
|
||||
// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
|
||||
for (int i = 0; i < count; i++) {
|
||||
graphics.setColor(getRandColor(150, 200)); // ---3
|
||||
|
||||
final int x = random.nextInt(width - lineWidth - 1) + 1; // 保证画在边框之内
|
||||
final int y = random.nextInt(height - lineWidth - 1) + 1;
|
||||
final int xl = random.nextInt(lineWidth);
|
||||
final int yl = random.nextInt(lineWidth);
|
||||
graphics.drawLine(x, y, x + xl, y + yl);
|
||||
}
|
||||
// 取随机产生的认证码
|
||||
for (int i = 0; i < resultCode.length(); i++) {
|
||||
// 将认证码显示到图象中,调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
|
||||
// graphics.setColor(new Color(20 + random.nextInt(130), 20 + random
|
||||
// .nextInt(130), 20 + random.nextInt(130)));
|
||||
// 设置字体颜色
|
||||
graphics.setColor(Color.BLACK);
|
||||
// 设置字体样式
|
||||
// graphics.setFont(new Font("Arial Black", Font.ITALIC, 18));
|
||||
graphics.setFont(new Font("Times New Roman", Font.BOLD, 24));
|
||||
// 设置字符,字符间距,上边距
|
||||
graphics.drawString(String.valueOf(resultCode.charAt(i)), (23 * i) + 8, 26);
|
||||
}
|
||||
// 图象生效
|
||||
graphics.dispose();
|
||||
return image;
|
||||
}
|
||||
|
||||
private static Color getRandColor(int fc, int bc) { // 取得给定范围随机颜色
|
||||
final Random random = new Random();
|
||||
if (fc > 255) {
|
||||
fc = 255;
|
||||
}
|
||||
if (bc > 255) {
|
||||
bc = 255;
|
||||
}
|
||||
|
||||
final int r = fc + random.nextInt(bc - fc);
|
||||
final int g = fc + random.nextInt(bc - fc);
|
||||
final int b = fc + random.nextInt(bc - fc);
|
||||
|
||||
return new Color(r, g, b);
|
||||
}
|
||||
}
|
||||
154
jshERP-boot/src/main/java/com/jsh/erp/utils/RegExpTools.java
Normal file
154
jshERP-boot/src/main/java/com/jsh/erp/utils/RegExpTools.java
Normal file
@@ -0,0 +1,154 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Adm on 2015/12/14.
|
||||
*
|
||||
* @author yubiao
|
||||
* <p/>
|
||||
* mysql匹配正则表达式
|
||||
*/
|
||||
public class RegExpTools {
|
||||
/**
|
||||
* @param search 模糊匹配字符串数组
|
||||
*/
|
||||
public static String regexp(List<String> search) {
|
||||
if (search == null || search.isEmpty())
|
||||
return null;
|
||||
String regexp = "";
|
||||
for (String s : search) {
|
||||
if (!regexp.isEmpty()) {
|
||||
regexp = regexp + "|";
|
||||
}
|
||||
regexp = regexp + ".*";
|
||||
regexp = regexp + s.replaceAll("\\.", "\\\\.");
|
||||
regexp = regexp + ".*";
|
||||
}
|
||||
return regexp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key json字段key
|
||||
* @param search 模糊匹配字符串数组
|
||||
* json的mysql匹配正则表达式
|
||||
*/
|
||||
public static String regexp(String key, List<String> search) {
|
||||
if (search == null || search.isEmpty())
|
||||
return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String s : search) {
|
||||
if (sb.length() == 0) {
|
||||
sb.append(".*\\\"").append(key).append("\\\":\\\"[a-zA-Z0-9]*(");
|
||||
} else {
|
||||
sb.append("|");
|
||||
}
|
||||
sb.append(s);
|
||||
}
|
||||
sb.append(")[a-zA-Z0-9]*\\\".*");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static class RegExp {
|
||||
public static final String ANY = ".*";
|
||||
public static final String QUOTE = "\\\"";
|
||||
public static final String LFT_PAREN = "(";
|
||||
public static final String RHT_PAREN = ")";
|
||||
public static final String COLON = ":";
|
||||
public static final String OR = "|";
|
||||
|
||||
private final StringBuilder builder = new StringBuilder();
|
||||
|
||||
public RegExp any() {
|
||||
builder.append(ANY);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp lftParen() {
|
||||
builder.append(LFT_PAREN);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp rhtParen() {
|
||||
builder.append(RHT_PAREN);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp colon() {
|
||||
builder.append(COLON);
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
public RegExp quote() {
|
||||
builder.append(QUOTE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp quote(String str) {
|
||||
Assert.notNull(str, "str为空");
|
||||
builder.append(QUOTE).append(str).append(QUOTE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp value(String str) {
|
||||
Assert.notNull(str, "str为空");
|
||||
builder.append(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp or() {
|
||||
builder.append(OR);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegExp or(List<String> values) {
|
||||
Assert.notEmpty(values, "values必须非空");
|
||||
lftParen();
|
||||
boolean first = true;
|
||||
for (String value : values) {
|
||||
if (first) {
|
||||
builder.append(value);
|
||||
first = false;
|
||||
} else {
|
||||
builder.append(OR).append(value);
|
||||
}
|
||||
}
|
||||
rhtParen();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
values.add("310");
|
||||
values.add(String.valueOf(2));
|
||||
values.add(String.valueOf(3));
|
||||
|
||||
RegExp exp = new RegExp();
|
||||
|
||||
exp.any();
|
||||
exp.quote("fullKbNum").colon()
|
||||
.quote()
|
||||
.value("[a-zA-Z0-9]*").or(values).value("[a-zA-Z0-9]*")
|
||||
.quote();
|
||||
exp.or();
|
||||
exp.quote("gbId[a-f0-9-]{36}").colon()
|
||||
.quote()
|
||||
.value("[0-9]*").or(values).value("[0-9]*")
|
||||
.quote();
|
||||
exp.any();
|
||||
|
||||
System.out.println(exp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class ResponseCode {
|
||||
|
||||
public final int code;
|
||||
public final Object data;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param code
|
||||
* @param data
|
||||
*/
|
||||
@JSONCreator
|
||||
public ResponseCode(@JSONField(name = "code") int code, @JSONField(name = "data")Object data) {
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.serializer.ValueFilter;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ResponseJsonUtil {
|
||||
public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
static {
|
||||
FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应过滤器
|
||||
*/
|
||||
public static final class ResponseFilter extends ExtJsonUtils.ExtFilter implements ValueFilter {
|
||||
@Override
|
||||
public Object process(Object object, String name, Object value) {
|
||||
if (name.equals("createTime") || name.equals("modifyTime")||name.equals("updateTime")) {
|
||||
return value;
|
||||
} else if (value instanceof Date) {
|
||||
return FORMAT.format(value);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param responseCode
|
||||
* @return
|
||||
*/
|
||||
public static String backJson4HttpApi(ResponseCode responseCode) {
|
||||
if (responseCode != null) {
|
||||
String result = JSON.toJSONString(responseCode, new ResponseFilter(),
|
||||
SerializerFeature.DisableCircularReferenceDetect,
|
||||
SerializerFeature.WriteNonStringKeyAsString);
|
||||
result = result.replaceFirst("\"data\":\\{", "");
|
||||
return result.substring(0, result.length() - 1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证失败的json串
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
public static String backJson4VerifyFailure(int code) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("message", "未通过验证");
|
||||
return JSON.toJSONString(new ResponseCode(code, map), new ResponseFilter(),
|
||||
SerializerFeature.DisableCircularReferenceDetect,
|
||||
SerializerFeature.WriteNonStringKeyAsString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功的json串
|
||||
* @param responseCode
|
||||
* @return
|
||||
*/
|
||||
public static String backJson(ResponseCode responseCode) {
|
||||
if (responseCode != null) {
|
||||
return JSON.toJSONString(responseCode, new ResponseFilter(),
|
||||
SerializerFeature.DisableCircularReferenceDetect,
|
||||
SerializerFeature.WriteNonStringKeyAsString);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String returnJson(Map<String, Object> map, String message, int code) {
|
||||
map.put("message", message);
|
||||
return backJson(new ResponseCode(code, map));
|
||||
}
|
||||
}
|
||||
257
jshERP-boot/src/main/java/com/jsh/erp/utils/StringUtil.java
Normal file
257
jshERP-boot/src/main/java/com/jsh/erp/utils/StringUtil.java
Normal file
@@ -0,0 +1,257 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author jishenghua qq752718920 2018-10-7 15:26:27
|
||||
*/
|
||||
public class StringUtil {
|
||||
|
||||
private StringUtil() {
|
||||
|
||||
}
|
||||
|
||||
private static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
public static String filterNull(String str) {
|
||||
if (str == null) {
|
||||
return "";
|
||||
} else {
|
||||
return str.trim();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean stringEquels(String source,String target) {
|
||||
if(isEmpty(source)||isEmpty(target)){
|
||||
return false;
|
||||
}else{
|
||||
return source.equals(target);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isEmpty(String str) {
|
||||
return str == null || "".equals(str.trim());
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(String str) {
|
||||
return !isEmpty(str);
|
||||
}
|
||||
|
||||
public static String getSysDate(String format) {
|
||||
if (StringUtil.isEmpty(format)) {
|
||||
format = DEFAULT_FORMAT;
|
||||
}
|
||||
SimpleDateFormat df = new SimpleDateFormat(format);
|
||||
return df.format(new Date());
|
||||
}
|
||||
|
||||
public static Date getDateByString(String date, String format) {
|
||||
if (StringUtil.isEmpty(format)) {
|
||||
format = DEFAULT_FORMAT;
|
||||
}
|
||||
if (StringUtil.isNotEmpty(date)) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
||||
try {
|
||||
return sdf.parse(date);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException("转换为日期类型错误:DATE:" + date + " FORMAT:" + format);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Date getDateByLongDate(Long millis) {
|
||||
if (millis == null) {
|
||||
return new Date();
|
||||
}
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(millis);
|
||||
return cal.getTime();
|
||||
|
||||
}
|
||||
|
||||
public static UUID stringToUUID(String id) {
|
||||
if (StringUtil.isNotEmpty(id)) {
|
||||
return UUID.fromString(id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer parseInteger(String str) {
|
||||
if (StringUtil.isNotEmpty(str)) {
|
||||
return Integer.parseInt(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<UUID> listToUUID(List<String> listStrs) {
|
||||
if (listStrs != null && listStrs.size() > 0) {
|
||||
List<UUID> uuidList = new ArrayList<UUID>();
|
||||
for (String str : listStrs) {
|
||||
uuidList.add(UUID.fromString(str));
|
||||
}
|
||||
return uuidList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<UUID> arrayToUUIDList(String[] uuids) {
|
||||
if (uuids != null && uuids.length > 0) {
|
||||
List<UUID> uuidList = new ArrayList<UUID>();
|
||||
for (String str : uuids) {
|
||||
uuidList.add(UUID.fromString(str));
|
||||
}
|
||||
return uuidList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//是否是JSON
|
||||
public static boolean containsAny(String str, String... flag) {
|
||||
if (str != null) {
|
||||
if (flag == null || flag.length == 0) {
|
||||
flag = "[-{-}-]-,".split("-");
|
||||
}
|
||||
for (String s : flag) {
|
||||
if (str.contains(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getModifyOrgOperateData(UUID resourceId, UUID orgId) {
|
||||
if (resourceId != null && orgId != null) {
|
||||
Map<UUID, UUID> map = new HashMap<UUID, UUID>();
|
||||
map.put(resourceId, orgId);
|
||||
return JSON.toJSONString(map);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String[] listToStringArray(List<String> list) {
|
||||
if (list != null && !list.isEmpty()) {
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
public static Long[] listToLongArray(List<Long> list) {
|
||||
if (list != null && !list.isEmpty()) {
|
||||
return list.toArray(new Long[list.size()]);
|
||||
}
|
||||
return new Long[0];
|
||||
}
|
||||
|
||||
public static List<String> stringToListArray(String[] strings) {
|
||||
if (strings != null && strings.length > 0) {
|
||||
return Arrays.asList(strings);
|
||||
}
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* String字符串转成List<Long>数据格式
|
||||
* String str = "1,2,3,4,5,6" -> List<Long> listLong [1,2,3,4,5,6];
|
||||
*
|
||||
* @param strArr
|
||||
* @return
|
||||
*/
|
||||
public static List<Long> strToLongList(String strArr) {
|
||||
List<Long> idList=new ArrayList<Long>();
|
||||
String[] d=strArr.split(",");
|
||||
for (int i = 0, size = d.length; i < size; i++) {
|
||||
if(d[i]!=null) {
|
||||
idList.add(Long.parseLong(d[i]));
|
||||
}
|
||||
}
|
||||
return idList;
|
||||
}
|
||||
|
||||
/**
|
||||
* String字符串转成List<String>数据格式
|
||||
* String str = "1,2,3,4,5,6" -> List<Long> listLong [1,2,3,4,5,6];
|
||||
*
|
||||
* @param strArr
|
||||
* @return
|
||||
*/
|
||||
public static List<String> strToStringList(String strArr) {
|
||||
if(StringUtils.isEmpty(strArr)){
|
||||
return null;
|
||||
}
|
||||
List<String> idList=new ArrayList<String>();
|
||||
String[] d=strArr.split(",");
|
||||
for (int i = 0, size = d.length; i < size; i++) {
|
||||
if(d[i]!=null) {
|
||||
idList.add(d[i].toString());
|
||||
}
|
||||
}
|
||||
return idList;
|
||||
}
|
||||
|
||||
public static List<String> searchCondition(String search) {
|
||||
if (isEmpty(search)) {
|
||||
return new ArrayList<String>();
|
||||
}else{
|
||||
//String[] split = search.split(" ");
|
||||
String[] split = search.split("#");
|
||||
return stringToListArray(split);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getInfo(String search, String key){
|
||||
String value = null;
|
||||
if(StringUtil.isNotEmpty(search)) {
|
||||
search = search.replace("{}","");
|
||||
if(StringUtil.isNotEmpty(search)) {
|
||||
JSONObject obj = JSONObject.parseObject(search);
|
||||
if (obj.get(key) != null) {
|
||||
value = obj.getString(key);
|
||||
if (value.equals("")) {
|
||||
value = null;
|
||||
}
|
||||
} else {
|
||||
value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static String toNull(String value) {
|
||||
if(("").equals(value)) {
|
||||
value = null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static boolean isExist(Object value) {
|
||||
if(value!=null) {
|
||||
String str = value.toString();
|
||||
if("".equals(str.trim())) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int i = 10/3;
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
696
jshERP-boot/src/main/java/com/jsh/erp/utils/Tools.java
Normal file
696
jshERP-boot/src/main/java/com/jsh/erp/utils/Tools.java
Normal file
@@ -0,0 +1,696 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.UnknownHostException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 工具类
|
||||
*
|
||||
* @author jishenghua qq:7-5-2-7-1-8-9-2-0
|
||||
*/
|
||||
public class Tools {
|
||||
/**
|
||||
* 获得32位唯一序列号
|
||||
*
|
||||
* @return 32为ID字符串
|
||||
*/
|
||||
public static String getUUID_32() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当天时间,格式为yyyy-MM-dd
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getNow() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前月 yyyy-MM
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentMonth() {
|
||||
return new SimpleDateFormat("yyyy-MM").format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期格式 yyyy-MM-dd
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentMonth(Date date) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当天时间,格式为yyyyMMddHHmmss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getNow2(Date date) {
|
||||
return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当天时间,格式为yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getNow3() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定时间,格式为yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getCenternTime(Date date) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定时间,格式为mm:ss
|
||||
*
|
||||
* @return 格式化后的日期格式
|
||||
*/
|
||||
public static String getTimeInfo(Date date) {
|
||||
return new SimpleDateFormat("mm:ss").format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期是星期几
|
||||
* return 星期几
|
||||
*/
|
||||
public static String getWeekDay() {
|
||||
Calendar c = Calendar.getInstance(Locale.CHINA);
|
||||
c.setTime(new Date());
|
||||
int day = c.get(Calendar.DAY_OF_WEEK);
|
||||
String weekDay = "";
|
||||
switch (day) {
|
||||
case 1:
|
||||
weekDay = "星期日";
|
||||
break;
|
||||
case 2:
|
||||
weekDay = "星期一";
|
||||
break;
|
||||
case 3:
|
||||
weekDay = "星期二";
|
||||
break;
|
||||
case 4:
|
||||
weekDay = "星期三";
|
||||
break;
|
||||
case 5:
|
||||
weekDay = "星期四";
|
||||
break;
|
||||
case 6:
|
||||
weekDay = "星期五";
|
||||
break;
|
||||
case 7:
|
||||
weekDay = "星期六";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return weekDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串是否全部为数字
|
||||
*
|
||||
* @param accountWaste
|
||||
* @return boolean值
|
||||
*/
|
||||
public static boolean checkStrIsNum(String checkStr) {
|
||||
if (checkStr == null || checkStr.length() == 0)
|
||||
return false;
|
||||
return Pattern.compile("^[0-9]*.{1}[0-9]*$").matcher(checkStr).matches();
|
||||
// return Pattern.compile(":^[0-9]+(.[0-9])*$").matcher(checkStr).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得前一天的时间
|
||||
*
|
||||
* @return 前一天日期
|
||||
*/
|
||||
public static String getPreviousDate() {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.DATE, -1);
|
||||
return new SimpleDateFormat("yyyy-MM").format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前月份的前6个月(含当前月)
|
||||
* @param size 月数
|
||||
* @return
|
||||
*/
|
||||
public static List<String> getLastMonths(int size) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(new Date());
|
||||
List<String> list = new ArrayList(size);
|
||||
for (int i=0;i<size;i++) {
|
||||
c.setTime(new Date());
|
||||
c.add(Calendar.MONTH, -i);
|
||||
Date m = c.getTime();
|
||||
list.add(sdf.format(m));
|
||||
}
|
||||
Collections.reverse(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取字符串长度
|
||||
*
|
||||
* @param beforeStr
|
||||
* @param cutLeng
|
||||
* @return 截取后的字符串
|
||||
*/
|
||||
public static String subStr(String beforeStr, int cutLeng) {
|
||||
if (beforeStr.length() > cutLeng)
|
||||
return beforeStr.substring(0, cutLeng) + "...";
|
||||
return beforeStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机字符串,字母和数字混合
|
||||
*
|
||||
* @return 组合后的字符串 ^[0-9a-zA-Z]
|
||||
*/
|
||||
public static String getRandomChar() {
|
||||
//生成一个0、1、2的随机数字
|
||||
int rand = (int) Math.round(Math.random() * 1);
|
||||
long itmp = 0;
|
||||
char ctmp = '\u0000';
|
||||
switch (rand) {
|
||||
//生成大写字母 + 1000以内数字
|
||||
case 1:
|
||||
itmp = Math.round(Math.random() * 25 + 65);
|
||||
ctmp = (char) itmp;
|
||||
return String.valueOf(ctmp) + (int) Math.random() * 1000;
|
||||
//生成小写字母
|
||||
case 2:
|
||||
itmp = Math.round(Math.random() * 25 + 97);
|
||||
ctmp = (char) itmp;
|
||||
return String.valueOf(ctmp) + (int) Math.random() * 1000;
|
||||
//生成数字
|
||||
default:
|
||||
itmp = Math.round(Math.random() * 1000);
|
||||
return itmp + "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断首字母以数字开头,字符串包括数字、字母%以及空格
|
||||
*
|
||||
* @param str 检查字符串
|
||||
* @return 是否以数字开头
|
||||
*/
|
||||
public static boolean CheckIsStartWithNum(String str) {
|
||||
return Pattern.compile("^[0-9][a-zA-Z0-9%,\\s]*$").matcher(str).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断首字母以","开头,字符串包括数字、字母%以及空格
|
||||
*
|
||||
* @param str 检查字符串
|
||||
* @return 是否以数字开头
|
||||
*/
|
||||
public static boolean CheckIsStartWithSpec(String str) {
|
||||
return Pattern.compile("^[,][a-zA-Z0-9%,\\s]*$").matcher(str).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符转码
|
||||
*
|
||||
* @param aValue
|
||||
* @return
|
||||
* @see 转码后的字符串
|
||||
*/
|
||||
public static String encodeValue(String aValue) {
|
||||
if (aValue.trim().length() == 0) {
|
||||
return "";
|
||||
}
|
||||
String valueAfterTransCode = null;
|
||||
try {
|
||||
valueAfterTransCode = URLEncoder.encode(aValue, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.getMessage();
|
||||
}
|
||||
return valueAfterTransCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符转码
|
||||
*
|
||||
* @param aValue
|
||||
* @return
|
||||
* @see 转码后的字符串
|
||||
*/
|
||||
public static String decodeValue(String aValue) {
|
||||
if (aValue.trim().length() == 0) {
|
||||
return "";
|
||||
}
|
||||
String valueAfterTransCode = null;
|
||||
try {
|
||||
valueAfterTransCode = URLDecoder.decode(aValue, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.getMessage();
|
||||
}
|
||||
return valueAfterTransCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除str中的'
|
||||
*
|
||||
* @param str
|
||||
* @return 除去'后的字符串
|
||||
* @see [类、类#方法、类#成员]
|
||||
*/
|
||||
public static String afterDealStr(String str) {
|
||||
return str.replace("'", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户IP地址(停用)
|
||||
*
|
||||
* @return 用户IP
|
||||
* @see [类、类#方法、类#成员]
|
||||
*/
|
||||
public static String getCurrentUserIP() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
return "127.0.0.1";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Request对象中获得客户端IP,处理了HTTP代理服务器和Nginx的反向代理截取了ip
|
||||
*
|
||||
* @param request
|
||||
* @return ip
|
||||
*/
|
||||
public static String getLocalIp(HttpServletRequest request) {
|
||||
String remoteAddr = getIpAddr(request);
|
||||
String forwarded = request.getHeader("X-Forwarded-For");
|
||||
String realIp = request.getHeader("X-Real-IP");
|
||||
|
||||
String ip = null;
|
||||
if (realIp == null) {
|
||||
if (forwarded == null) {
|
||||
ip = remoteAddr;
|
||||
} else {
|
||||
ip = remoteAddr + "/" + forwarded.split(",")[0];
|
||||
}
|
||||
} else {
|
||||
if (realIp.equals(forwarded)) {
|
||||
ip = realIp;
|
||||
} else {
|
||||
if (forwarded != null) {
|
||||
forwarded = forwarded.split(",")[0];
|
||||
}
|
||||
ip = realIp + "/" + forwarded;
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
/**
|
||||
* 获取访问者IP
|
||||
*
|
||||
* 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。
|
||||
*
|
||||
* 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割),
|
||||
* 如果还不存在则调用Request .getRemoteAddr()。
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static String getIpAddr(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Real-IP");
|
||||
if (!StringUtils.isEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
ip = request.getHeader("X-Forwarded-For");
|
||||
if (!StringUtils.isEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
// 多次反向代理后会有多个IP值,第一个为真实IP。
|
||||
int index = ip.indexOf(',');
|
||||
if (index != -1) {
|
||||
return ip.substring(0, index);
|
||||
} else {
|
||||
return ip;
|
||||
}
|
||||
} else {
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转化前台批量传入的ID值
|
||||
*
|
||||
* @param data
|
||||
* @return 转化后的ID值数组
|
||||
*/
|
||||
public static int[] changeDataForm(String data) {
|
||||
String[] dataStr = data.split(",");
|
||||
int[] dataInt = new int[dataStr.length];
|
||||
for (int i = 0; i < dataStr.length; i++)
|
||||
dataInt[i] = Integer.parseInt(dataStr[i]);
|
||||
return dataInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解决导出文件中文乱码问题firefox和ie下中文乱码
|
||||
*/
|
||||
public static String changeUnicode(String fileName, String browserType) {
|
||||
String returnFileName = "";
|
||||
try {
|
||||
if (browserType.equalsIgnoreCase("MSIE")) {
|
||||
returnFileName = URLEncoder.encode(fileName, "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
if (returnFileName.length() > 150) {
|
||||
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
}
|
||||
} else if (browserType.equalsIgnoreCase("Firefox")) {
|
||||
returnFileName = new String(fileName.getBytes("ISO8859-1"), "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
} else {
|
||||
returnFileName = URLEncoder.encode(fileName, "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
if (returnFileName.length() > 150) {
|
||||
|
||||
returnFileName = new String(returnFileName.getBytes("GB2312"), "ISO8859-1");
|
||||
returnFileName = returnFileName.replace(" ", "%20");
|
||||
}
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return returnFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写理财日志内容转化特殊字符
|
||||
*
|
||||
* @param str 需要转化的字符
|
||||
* @return 转化后的字符
|
||||
*/
|
||||
public static String htmlspecialchars(String str) {
|
||||
str = str.replaceAll("&", "&");
|
||||
str = str.replaceAll("<", "<");
|
||||
str = str.replaceAll(">", ">");
|
||||
str = str.replaceAll("\"", """);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据消费日期获取消费月
|
||||
*
|
||||
* @param consumeDate 消费日期
|
||||
* @return 返回消费月信息
|
||||
*/
|
||||
public static String getConsumeMonth(String consumeDate) {
|
||||
return consumeDate.substring(0, 7);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期的前XX个月
|
||||
*
|
||||
* @param 之前的第几个月
|
||||
* @return 前XX个月字符串
|
||||
*/
|
||||
public static String getBeforeMonth(int beforeMonth) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.add(Calendar.MONTH, -beforeMonth);
|
||||
return new SimpleDateFormat("yyyy-MM").format(c.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据月份获取当月最后一天
|
||||
* @param monthTime
|
||||
* @return
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static String lastDayOfMonth(String monthTime) throws ParseException {
|
||||
Date date = new SimpleDateFormat("yyyy-MM").parse(monthTime);
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.roll(Calendar.DAY_OF_MONTH, -1);
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取email用户姓名
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static String getEmailUserName(String emailAddress) {
|
||||
return emailAddress.substring(0, emailAddress.lastIndexOf("@"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中文编码,邮件附件乱码问题解决
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String getChineseString(String emailAttchmentTitle) {
|
||||
if (emailAttchmentTitle != null && !emailAttchmentTitle.equals("")) {
|
||||
try {
|
||||
return new String(emailAttchmentTitle.getBytes(), "ISO-8859-1");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return emailAttchmentTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断userTel是否合法,userTel只能是数字
|
||||
*
|
||||
* @param userTel
|
||||
* @return true 合法 false不合法
|
||||
*/
|
||||
public static boolean isTelNumber(String userTel) {
|
||||
String reg_phone = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$";
|
||||
String reg_tel = "^(1[0-9][0-9]|1[0-9][0|3|6|8|9])\\d{8}$";
|
||||
boolean b_phpne = Pattern.compile(reg_phone).matcher(userTel).matches();
|
||||
boolean b_tel = Pattern.compile(reg_tel).matcher(userTel).matches();
|
||||
return (b_phpne || b_tel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊判断电话号码是否合法,只能是数字
|
||||
*
|
||||
* @param macAddress
|
||||
* @return
|
||||
*/
|
||||
public static boolean isTelNumberBySlur(String userTel) {
|
||||
return Pattern.compile("^([\\s0-9]{0,12}$)").matcher(userTel).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间的字符串类型
|
||||
*
|
||||
* @return 处理后的字符串类型
|
||||
*/
|
||||
public static String getNowTime() {
|
||||
return new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 开打指定文件
|
||||
*
|
||||
* @param filePath 文件的绝对路径
|
||||
*/
|
||||
public static void openFile(String filePath) {
|
||||
String viewFilePath = filePath.replace("\\", "/");
|
||||
// Runtime.getRuntime().exec("cmd /c start "+filePath);
|
||||
// 解决路径中带空格问题
|
||||
Runtime r = Runtime.getRuntime();
|
||||
String[] cmdArray = new String[]{"cmd.exe", "/c", viewFilePath};
|
||||
try {
|
||||
r.exec(cmdArray);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串中是否含有中文
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
* @author jishenghua
|
||||
*/
|
||||
public static boolean isContainsChinese(String str) {
|
||||
return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤html文件中的文本
|
||||
*
|
||||
* @param content
|
||||
* @return过滤后的文本
|
||||
*/
|
||||
public static String filterText(String content) {
|
||||
return content.replace("/<(?:.|\\s)*?>/g", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉字符串中所有符号,不论是全角,还是半角的,或是货币符号或者空格等
|
||||
*
|
||||
* @param s
|
||||
* @return
|
||||
* @author jishenghua
|
||||
*/
|
||||
public static String removeSymbolForString(String s) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
char[] chars = s.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if ((chars[i] >= 19968 && chars[i] <= 40869) || (chars[i] >= 97 && chars[i] <= 122) || (chars[i] >= 65 && chars[i] <= 90)) {
|
||||
buffer.append(chars[i]);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个字符串的MD5
|
||||
*
|
||||
* @param msg
|
||||
* @return 加密后的MD5字符串
|
||||
* @throws NoSuchAlgorithmException
|
||||
*/
|
||||
public static String md5Encryp(String msg) throws NoSuchAlgorithmException {
|
||||
// 生成一个MD5加密计算摘要
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
// 计算md5函数
|
||||
md.update(msg.getBytes());
|
||||
return new BigInteger(1, md.digest()).toString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否插件URL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean isPluginUrl(String url) {
|
||||
if (url != null && (url.startsWith("/plugin"))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字符串null值
|
||||
*
|
||||
* @param beforeStr 处理前字符串
|
||||
* @return 处理后的字符串
|
||||
*/
|
||||
public static String dealNullStr(String beforeStr) {
|
||||
if (null == beforeStr || beforeStr.length() == 0)
|
||||
return "";
|
||||
return beforeStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据token截取租户id
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public static Long getTenantIdByToken(String token) {
|
||||
Long tenantId = 0L;
|
||||
if(StringUtil.isNotEmpty(token) && token.indexOf("_")>-1) {
|
||||
String[] tokenArr = token.split("_");
|
||||
if (tokenArr.length == 2) {
|
||||
tenantId = Long.parseLong(tokenArr[1]);
|
||||
}
|
||||
}
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用参数Format将字符串转为Date
|
||||
*
|
||||
* @param strDate
|
||||
* @param pattern
|
||||
* @return
|
||||
* @throws ParseException
|
||||
* @author jishenghua
|
||||
*/
|
||||
public static Date parse(String strDate, String pattern)
|
||||
throws ParseException {
|
||||
return new SimpleDateFormat(pattern).parse(strDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机数字和字母组合
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
public static String getCharAndNum(int length) {
|
||||
Random random = new Random();
|
||||
StringBuffer valSb = new StringBuffer();
|
||||
String charStr = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
int charLength = charStr.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int index = random.nextInt(charLength);
|
||||
valSb.append(charStr.charAt(index));
|
||||
}
|
||||
return valSb.toString();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 过滤html文件中的图片文件
|
||||
// * @param content
|
||||
// * @return
|
||||
// */
|
||||
// public static String filterImg(String content)
|
||||
// {
|
||||
// return content.matches("/<img(?:.|\\s)*?>/g");
|
||||
// }
|
||||
|
||||
public static void main(String[] args) {
|
||||
String aa = "的付的反对法的发的说法";
|
||||
char[] bb = aa.toCharArray();
|
||||
for (char c : bb) {
|
||||
System.out.println(c);
|
||||
}
|
||||
System.out.println(getBeforeMonth(1));
|
||||
|
||||
try {
|
||||
System.out.println(md5Encryp("guest"));
|
||||
System.out.println(md5Encryp("admin"));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String value = "2333";
|
||||
System.out.println(checkStrIsNum(value));
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
System.out.print(getRandomChar() + " || ");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user