From db7fc9f145beb76bbef19b1812d63d2ffc2d0df9 Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期三, 25 十一月 2020 18:25:48 +0800 Subject: [PATCH] 优化 --- src/main/java/com/hx/util/ExcelUtil.java | 67 +++++++++++ src/main/java/com/hx/util/ImagesAddDomain.java | 6 src/main/java/com/hx/util/SimpleTool.java | 17 +- src/main/java/com/hx/util/DownFileUtil.java | 149 ++++++++++++++++++++++++ src/main/java/com/hx/util/DateUtil.java | 36 ++++++ src/main/java/com/hx/exception/LoginException.java | 35 +++++ src/main/java/com/hx/util/QRCodeUtil.java | 42 ++++++ hx-common.iml | 1 src/main/java/com/hx/util/AesUtil.java | 1 9 files changed, 342 insertions(+), 12 deletions(-) diff --git a/hx-common.iml b/hx-common.iml index 4afd238..4676147 100644 --- a/hx-common.iml +++ b/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" /> diff --git a/src/main/java/com/hx/exception/LoginException.java b/src/main/java/com/hx/exception/LoginException.java new file mode 100644 index 0000000..c5e6e18 --- /dev/null +++ b/src/main/java/com/hx/exception/LoginException.java @@ -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; + } +} diff --git a/src/main/java/com/hx/util/AesUtil.java b/src/main/java/com/hx/util/AesUtil.java index d5b869c..ceda343 100644 --- a/src/main/java/com/hx/util/AesUtil.java +++ b/src/main/java/com/hx/util/AesUtil.java @@ -97,7 +97,6 @@ } catch (Exception e) { e.printStackTrace(); } - } return null; } diff --git a/src/main/java/com/hx/util/DateUtil.java b/src/main/java/com/hx/util/DateUtil.java index 724ae27..a7e8001 100644 --- a/src/main/java/com/hx/util/DateUtil.java +++ b/src/main/java/com/hx/util/DateUtil.java @@ -344,6 +344,42 @@ return weekDays[w]; } + /** + * endTime姣攕tartTime澶氱殑澶╂暟 + * @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){ diff --git a/src/main/java/com/hx/util/DownFileUtil.java b/src/main/java/com/hx/util/DownFileUtil.java new file mode 100644 index 0000000..26be2ac --- /dev/null +++ b/src/main/java/com/hx/util/DownFileUtil.java @@ -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 + * 瀹屾暣璺緞锛堝彲鐢╯impleToof宸ュ叿鑾峰彇锛� + */ + 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 + * 瀹屾暣璺緞锛堝彲鐢╯impleToof宸ュ叿鑾峰彇锛� + */ + 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(); + } + } + +} diff --git a/src/main/java/com/hx/util/ExcelUtil.java b/src/main/java/com/hx/util/ExcelUtil.java index 3598df9..c549a08 100644 --- a/src/main/java/com/hx/util/ExcelUtil.java +++ b/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(); + + // 鍦‥xcel宸ヤ綔绨夸腑寤轰竴宸ヤ綔琛紝鍏跺悕涓虹己鐪佸�� + // 濡傝鏂板缓涓�鍚嶄负"鏁堢泭鎸囨爣"鐨勫伐浣滆〃锛屽叾璇彞涓猴細 + // 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 ""; diff --git a/src/main/java/com/hx/util/ImagesAddDomain.java b/src/main/java/com/hx/util/ImagesAddDomain.java index cfbfe27..8a7f0be 100644 --- a/src/main/java/com/hx/util/ImagesAddDomain.java +++ b/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("/")){ diff --git a/src/main/java/com/hx/util/QRCodeUtil.java b/src/main/java/com/hx/util/QRCodeUtil.java index 4ed2f62..5ef9d11 100644 --- a/src/main/java/com/hx/util/QRCodeUtil.java +++ b/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,璁剧疆瀹归敊鐜囦负鏈�楂�,涓�鑸閿欑巼瓒婇珮,鍥剧墖瓒婁笉娓呮櫚, 浣嗘槸鍙湁灏嗗閿欑巼璁剧疆楂樹竴鐐规墠鑳藉吋瀹筶ogo鍥剧墖 + * 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,璁剧疆瀹归敊鐜囦负鏈�楂�,涓�鑸閿欑巼瓒婇珮,鍥剧墖瓒婁笉娓呮櫚, 浣嗘槸鍙湁灏嗗閿欑巼璁剧疆楂樹竴鐐规墠鑳藉吋瀹筶ogo鍥剧墖 diff --git a/src/main/java/com/hx/util/SimpleTool.java b/src/main/java/com/hx/util/SimpleTool.java index 2428dab..8793c5c 100644 --- a/src/main/java/com/hx/util/SimpleTool.java +++ b/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}; - + + /**瀵硅薄杞琺ap*/ + public static Map<?, ?> objectToMap(Object obj) { + if(obj == null) + return null; + + return new org.apache.commons.beanutils.BeanMap(obj); + } + /** * 鍚庡彴鏍煎紡鏋勫缓杩斿洖鍊兼牸寮忓垪琛�-鍚庡彴鑾峰彇鍒楄〃 * @param count 杩斿洖鎬绘潯鏁� -- Gitblit v1.8.0