hx-common.iml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/exception/LoginException.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/AesUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/DateUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/DownFileUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/ExcelUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/ImagesAddDomain.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/QRCodeUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/SimpleTool.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
hx-common.iml
@@ -19,6 +19,7 @@ </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.6" level="project" /> <orderEntry type="library" name="Maven: com.sun.mail:javax.mail:1.6.2" level="project" /> <orderEntry type="library" name="Maven: javax.activation:activation:1.1" level="project" /> <orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.6" level="project" /> src/main/java/com/hx/exception/LoginException.java
New file @@ -0,0 +1,35 @@ package com.hx.exception; /** * 登录提示异常 * @author ChenJiaHe * @Date 2020-11-17 */ public class LoginException extends RuntimeException { private Integer code; public LoginException(String message) { super(message); } public LoginException(String message, Integer code) { super(message); this.code = code; } public LoginException(String message, Throwable cause, Integer code) { super(message, cause); this.code = code; } public LoginException(Throwable cause, Integer code) { super(cause); this.code = code; } //@Override public int getCode() { return this.code; } } src/main/java/com/hx/util/AesUtil.java
@@ -97,7 +97,6 @@ } catch (Exception e) { e.printStackTrace(); } } return null; } src/main/java/com/hx/util/DateUtil.java
@@ -344,6 +344,42 @@ return weekDays[w]; } /** * endTime比startTime多的天数 * @param startTime 最小时间 * @param endTime 最大时间 * @return */ public static Integer differDay(Date startTime,Date endTime){ Calendar cal1 = Calendar.getInstance(); cal1.setTime(startTime); Calendar cal2 = Calendar.getInstance(); cal2.setTime(endTime); int day1= cal1.get(Calendar.DAY_OF_YEAR); int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); if(year1 != year2) { //同一年 int timeDistance = 0 ; for(int i = year1 ; i < year2 ; i ++) { if(i%4==0 && i%100!=0 || i%400==0) {//闰年 timeDistance += 366; } else {//不是闰年 timeDistance += 365; } } return timeDistance + (day2-day1) ; } else { //不同年 System.out.println("判断day2 - day1 : " + (day2-day1)); return day2-day1; } } /**判断两个时间是不是同一天*/ public static boolean timeEqual(Date startTime,Date endTime){ if(startTime == null || endTime==null){ src/main/java/com/hx/util/DownFileUtil.java
New file @@ -0,0 +1,149 @@ package com.hx.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; public class DownFileUtil { /** * 下载文件工具 * * @param path * 完整路径(可用simpleToof工具获取) */ public static void DownFile(HttpServletRequest request,HttpServletResponse response,String path) { final String userAgent = request.getHeader("USER-AGENT"); try { // AdminUpload adminUpload = dm.find(AdminUpload.class, fileName); // String str =adminUpload.getPath(); String filePath = path; // System.out.println("path"+ServletActionContext.getRequest().getRealPath("/") // + filePath); // File downfile = new // File(ServletActionContext.getRequest().getRealPath("/") + // filePath); File downfile = new File(filePath); String filename = ""; if(userAgent.equals("MSIE")){//IE浏览器 filename = URLEncoder.encode(downfile.getName(),"UTF8"); }else if(userAgent.equals("Mozilla")){//google,火狐浏览器 filename = new String(downfile.getName().getBytes(), "ISO8859-1"); }else{ filename = URLEncoder.encode(downfile.getName(),"UTF8");//其他浏览器 } InputStream fis = new BufferedInputStream(new FileInputStream( downfile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.addHeader("Content-Length", "" + downfile.length()); OutputStream toClient = new BufferedOutputStream( response.getOutputStream()); response.setContentType( "application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 下载文件工具(提示选择路径) * * @param path * 完整路径(可用simpleToof工具获取) */ public static void DownFileTips(HttpServletRequest request,HttpServletResponse response,String path) { final String userAgent = request.getHeader("USER-AGENT"); try { // AdminUpload adminUpload = dm.find(AdminUpload.class, fileName); // String str =adminUpload.getPath(); String filePath = path; // System.out.println("path"+ServletActionContext.getRequest().getRealPath("/") // + filePath); // File downfile = new // File(ServletActionContext.getRequest().getRealPath("/") + // filePath); File downfile = new File(filePath); String filename = ""; if(userAgent.equals("MSIE")){//IE浏览器 filename = URLEncoder.encode(downfile.getName(),"UTF8"); }else if(userAgent.equals("Mozilla")){//google,火狐浏览器 filename = new String(downfile.getName().getBytes(), "ISO8859-1"); }else{ filename = URLEncoder.encode(downfile.getName(),"UTF8");//其他浏览器 } InputStream fis = new BufferedInputStream(new FileInputStream( downfile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes())); OutputStream toClient= new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/vnd.ms-excel;charset=utf-8"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 下载文件工具(提示选择路径) * * @param downfile * 导出的文件 * @param fileName * 导出的文件名称 */ public static void DownFileTips(HttpServletRequest request, HttpServletResponse response, File downfile, String fileName) { final String userAgent = request.getHeader("USER-AGENT"); try { if(StringUtils.isEmpty(fileName)){ fileName = downfile.getName(); } String filename = ""; if(userAgent.equals("MSIE")){//IE浏览器 filename = URLEncoder.encode(fileName,"UTF8"); }else if(userAgent.equals("Mozilla")){//google,火狐浏览器 filename = new String(fileName.getBytes(), "ISO8859-1"); }else{ filename = URLEncoder.encode(fileName,"UTF8");//其他浏览器 } InputStream fis = new BufferedInputStream(new FileInputStream( downfile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.addHeader("Content-Disposition", "attachment;filename="+ filename); OutputStream toClient= new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/vnd.ms-excel;charset=utf-8"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } src/main/java/com/hx/util/ExcelUtil.java
@@ -96,6 +96,73 @@ return filePath; } /**生成临时文件 * @param headList * Excel文件Head标题集合 * @param fieldList * Excel文件Field标题集合 根据field来寻找位置填充表格 * @param dataList * Excel文件数据内容部分 * @throws Exception */ public static File createExcel(String[] headList, String[] fieldList, List<Map<String, Object>> dataList) throws Exception { File file = File.createTempFile("temp", ".xls"); try{ // 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 在Excel工作簿中建一工作表,其名为缺省值 // 如要新建一名为"效益指标"的工作表,其语句为: // HSSFSheet sheet = workbook.createSheet("效益指标"); HSSFSheet sheet = workbook.createSheet(); // 在索引0的位置创建行(最顶端的行) HSSFRow row = sheet.createRow(0); // =============================================================== for (int i = 0; i < headList.length; i++) { // 在索引0的位置创建单元格(左上端) HSSFCell cell = row.createCell(i); // 定义单元格为字符串类型 cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 在单元格中输入一些内容 cell.setCellValue(headList[i]); } // =============================================================== if (dataList != null) { for (int n = 0; n < dataList.size(); n++) { // 在索引1的位置创建行 HSSFRow row_value = sheet.createRow(n + 1); Map<String, Object> dataMap = dataList.get(n); // =============================================================== for (int i = 0; i < fieldList.length; i++) { // 在索引0的位置创建单元格(左上端) HSSFCell cell = row_value.createCell(i); // 定义单元格为字符串类型 cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 在单元格中输入一些内容 cell.setCellValue(objToString(dataMap.get(fieldList[i]))); } // =============================================================== } } // 新建一输出文件流 FileOutputStream fOut = new FileOutputStream(file); // 把相应的Excel 工作簿存盘 workbook.write(fOut); fOut.flush(); // 操作结束,关闭文件 fOut.close(); }catch (Exception e){ }finally { file.deleteOnExit(); } return file; } private static String objToString(Object obj) { if (obj == null) { return ""; src/main/java/com/hx/util/ImagesAddDomain.java
@@ -19,7 +19,7 @@ if(SimpleTool.checkNotNull(arrString)){ JSONArray images = JSONArray.fromObject(arrString); for(int i = 0;i<images.size();i++){ if(images.getString(i).indexOf("http")!=-1){ if(images.getString(i).startsWith("http")){ arr.add(images.getString(i)); }else{ arr.add(urlAddDomain(images.getString(i),domainName)); @@ -39,7 +39,7 @@ JSONArray arr = new JSONArray(); if(SimpleTool.checkNotNull(imagesArr)){ for(int i = 0;i<imagesArr.size();i++){ if(imagesArr.getString(i).indexOf("http")!=-1){ if(imagesArr.getString(i).startsWith("http")){ arr.add(imagesArr.getString(i)); }else{ arr.add(urlAddDomain(imagesArr.getString(i),domainName)); @@ -60,7 +60,7 @@ return imagesUrl; } if(SimpleTool.checkNotNull(imagesUrl)){ if(imagesUrl.indexOf("http")!=-1){ if(imagesUrl.startsWith("http")){ return imagesUrl; } if(domainName.endsWith("/")){ src/main/java/com/hx/util/QRCodeUtil.java
@@ -2,6 +2,7 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; @@ -32,7 +33,46 @@ } /** * zxing方式生成二维码 * zxing方式生成二维码(返回base64) * 注意: * 1,文本生成二维码的方法独立出来,返回image流的形式,可以输出到页面 * 2,设置容错率为最高,一般容错率越高,图片越不清晰, 但是只有将容错率设置高一点才能兼容logo图片 * 3,logo图片默认占二维码图片的20%,设置太大会导致无法解析 * * @param content 二维码包含的内容,文本或网址 * @param size 生成的二维码图片尺寸 可以自定义或者默认(250) * @param logoPath logo的存放位置 */ public static String QRCodeCreate(String content, Integer size, String logoPath) { ByteArrayOutputStream bos = null; if(size == null){ size = 400; } try{ char[] rands = content.toCharArray(); //获取二维码流的形式,写入到目录文件中 BufferedImage image = getBufferedImage(content, size, logoPath); bos = new ByteArrayOutputStream(); ImageIO.write(image, "JPEG", bos); byte[] buf = bos.toByteArray(); return Base64.getEncoder().encodeToString(buf); }catch (Exception e){ e.printStackTrace(); }finally { if(bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } bos = null; } } return null; } /** * zxing方式生成二维码(返回路径) * 注意: * 1,文本生成二维码的方法独立出来,返回image流的形式,可以输出到页面 * 2,设置容错率为最高,一般容错率越高,图片越不清晰, 但是只有将容错率设置高一点才能兼容logo图片 src/main/java/com/hx/util/SimpleTool.java
@@ -22,12 +22,7 @@ import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import java.util.UUID; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -54,7 +49,15 @@ public static SimpleDateFormat myformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static DecimalFormat fmt = new DecimalFormat("0.00"); public static Byte[] lock = new Byte[] {0}; /**对象转map*/ public static Map<?, ?> objectToMap(Object obj) { if(obj == null) return null; return new org.apache.commons.beanutils.BeanMap(obj); } /** * 后台格式构建返回值格式列表-后台获取列表 * @param count 返回总条数