From 98118c2bf5343dde3dbf48c20bc8a0fd69cfedbd Mon Sep 17 00:00:00 2001 From: E1ED922C1E9526DD63272D7EC5C6CB77 <E1ED922C1E9526DD63272D7EC5C6CB77@i-search.com.cn> Date: 星期一, 14 十二月 2020 10:12:42 +0800 Subject: [PATCH] Merge branch 'master' of https://gitee.com/huoxiong/hx_common --- src/main/java/com/hx/util/gaode/AddressCode.java | 42 +++ src/main/java/com/hx/common/service/impl/CommonServiceImpl.java | 6 src/main/java/com/hx/util/FileUtils.java | 80 ++++++ src/main/java/com/hx/util/StringUtils.java | 10 src/main/java/com/hx/common/service/impl/CommonDaoImpl.java | 12 src/main/java/com/hx/util/DownFileUtil.java | 149 +++++++++++ src/main/java/com/hx/exception/LoginException.java | 35 ++ src/main/java/com/hx/util/QRCodeUtil.java | 42 +++ pom.xml | 7 src/main/java/com/hx/common/dao/CommonDao.java | 7 src/main/java/com/hx/util/ExcelUtil.java | 67 +++++ src/main/java/com/hx/util/ImagesAddDomain.java | 6 src/main/java/com/hx/util/COSUtil.java | 70 +++++ src/main/java/com/hx/util/HttpUtil.java | 30 + src/main/java/com/hx/util/SimpleTool.java | 17 src/main/java/com/hx/util/IPUtils.java | 40 +++ src/main/java/com/hx/util/DateUtil.java | 49 +++ src/main/java/com/hx/common/service/CommonService.java | 8 src/main/java/com/hx/util/gaode/GaoDeMapUtil.java | 53 ++++ hx-common.iml | 12 src/main/java/com/hx/util/AesUtil.java | 1 21 files changed, 716 insertions(+), 27 deletions(-) diff --git a/hx-common.iml b/hx-common.iml index 4afd238..1348670 100644 --- a/hx-common.iml +++ b/hx-common.iml @@ -19,6 +19,13 @@ </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="library" name="Maven: com.qcloud:cos_api:5.6.32" level="project" /> + <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> + <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" /> + <orderEntry type="library" name="Maven: joda-time:joda-time:2.9.9" level="project" /> + <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.6" level="project" /> + <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" /> + <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.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" /> @@ -29,7 +36,6 @@ <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.13" level="project" /> <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.12" level="project" /> <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" /> - <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" /> <orderEntry type="library" name="Maven: cglib:cglib:3.1" level="project" /> <orderEntry type="library" name="Maven: org.ow2.asm:asm:4.2" level="project" /> <orderEntry type="library" name="Maven: com.google.zxing:core:3.3.0" level="project" /> @@ -41,9 +47,6 @@ <orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" /> <orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.5.0" level="project" /> <orderEntry type="library" name="Maven: com.auth0:java-jwt:3.10.3" level="project" /> - <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.6" level="project" /> - <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" /> - <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.6" level="project" /> <orderEntry type="library" name="Maven: io.jsonwebtoken:jjwt:0.9.1" level="project" /> <orderEntry type="library" name="Maven: javax.servlet.jsp.jstl:jstl-api:1.2" level="project" /> <orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.5" level="project" /> @@ -87,7 +90,6 @@ <orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.0" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.0.5.RELEASE" level="project" /> <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:2.7.9" level="project" /> - <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.0.9.RELEASE" level="project" /> <orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:5.1.47" level="project" /> <orderEntry type="library" name="Maven: com.alibaba:druid:1.1.9" level="project" /> diff --git a/pom.xml b/pom.xml index 5900039..1be7491 100644 --- a/pom.xml +++ b/pom.xml @@ -13,9 +13,14 @@ <artifactId>hx-common</artifactId> <packaging>jar</packaging> - <dependencies> + <!--鑵捐浜� cos--> + <dependency> + <groupId>com.qcloud</groupId> + <artifactId>cos_api</artifactId> + </dependency> + <!--鍙戦�侀偖浠�--> <dependency> <groupId>com.sun.mail</groupId> diff --git a/src/main/java/com/hx/common/dao/CommonDao.java b/src/main/java/com/hx/common/dao/CommonDao.java index 138a0c5..3a4ccad 100644 --- a/src/main/java/com/hx/common/dao/CommonDao.java +++ b/src/main/java/com/hx/common/dao/CommonDao.java @@ -27,6 +27,13 @@ */ <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence); + /**鏌ヨ鏉℃暟 + * @param mapperClass mapper绫� + * @param sqlSentence 鏌ヨ鍙傛暟绫� + * @return 杩斿洖鏉℃暟 + */ + <T extends Serializable> int selectCountSql(Class<?> mapperClass, SqlSentence sqlSentence); + /**鏌ヨ鍒楄〃锛岃繑鍥炲疄浣撶被鐨凩ist * @param mapperClass mapper绫� * @param sqlSentence 鏌ヨ鍙傛暟绫� diff --git a/src/main/java/com/hx/common/service/CommonService.java b/src/main/java/com/hx/common/service/CommonService.java index 64164ec..2d35f9f 100644 --- a/src/main/java/com/hx/common/service/CommonService.java +++ b/src/main/java/com/hx/common/service/CommonService.java @@ -27,6 +27,14 @@ */ <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence); + /**鏌ヨ鏉℃暟 + * @param mapperClass mapper绫� + * @param sqlSentence 鏌ヨ鍙傛暟绫� + * @return 杩斿洖鏉℃暟 + */ + <T extends Serializable> int selectCountSql(Class<?> mapperClass, SqlSentence sqlSentence); + + /**鏌ヨ鍒楄〃锛岃繑鍥炲疄浣撶被鐨凩ist * @param mapperClass mapper绫� * @param sqlSentence 鏌ヨ鍙傛暟绫� diff --git a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java index 388fcd3..16ef362 100644 --- a/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java +++ b/src/main/java/com/hx/common/service/impl/CommonDaoImpl.java @@ -28,12 +28,20 @@ return sqlSessionFactory.openSession().insert(getStatement(mapperClass,"insert"),entity); } - /**鑾峰彇鏉℃暟*/ + /**鑾峰彇鏉℃暟 + * @return*/ @Override - public <T extends Serializable> int selectCount(Class<?> mapperClass,SqlSentence sqlSentence) { + public <T extends Serializable> int selectCount(Class<?> mapperClass, SqlSentence sqlSentence) { return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectCount"),sqlSentence); } + /**鑾峰彇鏉℃暟 + * @return*/ + @Override + public <T extends Serializable> int selectCountSql(Class<?> mapperClass, SqlSentence sqlSentence) { + return sqlSessionFactory.openSession().selectOne(getStatement(mapperClass,"selectCountSql"),sqlSentence); + } + /**鑾峰彇鍒楄〃*/ @Override public <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence) { diff --git a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java index 7d911eb..c2822ec 100644 --- a/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java +++ b/src/main/java/com/hx/common/service/impl/CommonServiceImpl.java @@ -31,6 +31,12 @@ return commonDao.selectCount(mapperClass,sqlSentence); } + /**鑾峰彇鏉℃暟*/ + @Override + public <T extends Serializable> int selectCountSql(Class<?> mapperClass,SqlSentence sqlSentence) { + return commonDao.selectCountSql(mapperClass,sqlSentence); + } + /**鑾峰彇鍒楄〃*/ @Override public <T extends Serializable> List<T> selectList(Class<?> mapperClass, SqlSentence sqlSentence) { 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/COSUtil.java b/src/main/java/com/hx/util/COSUtil.java new file mode 100644 index 0000000..f0ae88f --- /dev/null +++ b/src/main/java/com/hx/util/COSUtil.java @@ -0,0 +1,70 @@ +package com.hx.util; + +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.OSSObject; +import com.qcloud.cos.COSClient; +import com.qcloud.cos.ClientConfig; +import com.qcloud.cos.auth.BasicCOSCredentials; +import com.qcloud.cos.auth.COSCredentials; +import com.qcloud.cos.model.ObjectMetadata; +import com.qcloud.cos.model.PutObjectRequest; +import com.qcloud.cos.model.PutObjectResult; +import com.qcloud.cos.region.Region; +import org.springframework.web.multipart.MultipartFile; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; + +/**鑵捐浜� COS + * @author ChenJiaHe + * @date 2020-12-03 + */ +public class COSUtil { + + /** 鍚庣璋冪敤涓婁紶鍥剧墖 + * @param key 涓婁紶璺緞锛堝寘鎷浘鐗囧悕绉板拰鍜屽悗缂�锛夛紝鎸囧畾瑕佷笂浼犲埌 COS 涓婂璞¢敭 + * @param localFile + * @param secretId 鐢ㄦ埛id + * @param secretKey 鐢ㄦ埛绉橀挜 + * @param regionName 瀛樺湪鍩燂紝鍙傝�冭吘璁簯 + * @param bucketName 鎸囧畾瑕佷笂浼犲埌鐨勫瓨鍌ㄦ《 + * @return + * @throws IOException + */ + public static String uploadImg(String key,MultipartFile localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException { + + // 1 鍒濆鍖栫敤鎴疯韩浠戒俊鎭紙secretId, secretKey锛夈�� + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); + // 2 璁剧疆 bucket 鐨勫尯鍩�, COS 鍦板煙鐨勭畝绉拌鍙傜収 https://cloud.tencent.com/document/product/436/6224 + // clientConfig 涓寘鍚簡璁剧疆 region, https(榛樿 http), 瓒呮椂, 浠g悊绛� set 鏂规硶, 浣跨敤鍙弬瑙佹簮鐮佹垨鑰呭父瑙侀棶棰� Java SDK 閮ㄥ垎銆� + Region region = new Region(regionName); + ClientConfig clientConfig = new ClientConfig(region); + // 3 鐢熸垚 cos 瀹㈡埛绔�� + COSClient cosClient = new COSClient(cred, clientConfig); + + //寮�濮嬩笂浼� + ObjectMetadata objectMetadata = new ObjectMetadata(); + // 璁剧疆杈撳叆娴侀暱搴︿负500 + objectMetadata.setContentLength(500); + // 璁剧疆 Content type, 榛樿鏄� application/octet-stream,瀵逛簬鏈湴鏂囦欢涓婁紶锛岄粯璁ゆ牴鎹湰鍦版枃浠剁殑鍚庣紑杩涜鏄犲皠 + // 锛屼緥濡� jpg 鏂囦欢鏄犲皠 涓篿mage/jpeg瀵逛簬娴佸紡涓婁紶 榛樿鏄� application/octet-stream + //objectMetadata.setContentType("application/pdf"); + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key,localFile.getInputStream(),objectMetadata); + PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); + + //鎷兼帴璺緞 + StringBuilder imgUrl = new StringBuilder(); + imgUrl.append("https://"+bucketName+"-"+secretId+".cos."+regionName+".myqcloud.com"); + if(key.startsWith("/")){ + imgUrl.append(key); + }else{ + imgUrl.append("/"+key); + } + return imgUrl.toString(); + + } + +} diff --git a/src/main/java/com/hx/util/DateUtil.java b/src/main/java/com/hx/util/DateUtil.java index f387926..a2484b8 100644 --- a/src/main/java/com/hx/util/DateUtil.java +++ b/src/main/java/com/hx/util/DateUtil.java @@ -295,6 +295,19 @@ return c.getTime(); } + /**鏃堕棿涓婂勾鏁板彔鍔� + * @Author: ChenJiaHe + * @param dateTime 鏃堕棿 + * @param dayNum 澶╂暟 + * @return + */ + public static Date addYear(Date dateTime,int dayNum){ + Calendar c = Calendar.getInstance(); + c.setTime(dateTime); + c.add(Calendar.YEAR, dayNum); + return c.getTime(); + } + /** * 杞崲鎴愬瓧绗︿覆鍒版寚瀹氱殑鏃ユ湡 * @@ -354,6 +367,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/FileUtils.java b/src/main/java/com/hx/util/FileUtils.java index 0dc968c..5089158 100644 --- a/src/main/java/com/hx/util/FileUtils.java +++ b/src/main/java/com/hx/util/FileUtils.java @@ -6,6 +6,8 @@ import org.springframework.web.multipart.MultipartFile; import java.io.*; +import java.net.FileNameMap; +import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; @@ -218,6 +220,57 @@ } } + /**瑙嗛涓婁紶鐨勬柟娉� + * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨� + * @param platformIconFile 瑙嗛鏂囦欢 + * @param unifiedFolder NG鎸囧悜鐨勫墠绔枃浠跺す(缁熶竴鏂囦欢澶�)锛屽锛歶ser/local/images/ + * @param saveFolder 淇濆瓨鍒扮殑鏂囦欢澶�,濡傦細/bananer/ + * @param autoDateFolder 鏄惁鐢熸垚鏃ユ湡鏂囦欢澶� + * @return 鍥剧墖璺緞 + * 2020-06-29 ChenJiaHe + */ + public static String videoFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder + ,boolean autoDateFolder) { + String fileName = ""; + try { + if(platformIconFile == null) { + throw new TipsException("璇蜂笂浼犺棰戞枃浠讹紒"); + } + if(!getMimeType(platformIconFile.getOriginalFilename())){ + throw new TipsException("璇蜂笂浼犺棰戞牸寮忕殑鏂囦欢锛�"); + } + + //璁剧疆鍥剧墖澶у皬 + // String.format("%.1f",platformIconFile.getSize()/1024.0); + if(autoDateFolder){ + if(saveFolder.endsWith("/")){ + saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/"; + }else{ + saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/"; + } + } + fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS"); + if(unifiedFolder.endsWith("/")){ + if(saveFolder.startsWith("/")){ + saveFolder = saveFolder.replaceFirst("/",""); + unifiedFolder = unifiedFolder + saveFolder; + }else{ + unifiedFolder = unifiedFolder+saveFolder; + } + }else{ + if(saveFolder.startsWith("/")){ + unifiedFolder = unifiedFolder + saveFolder; + }else{ + unifiedFolder = unifiedFolder+"/"+saveFolder; + } + } + fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName); + } catch (RuntimeException e) { + e.printStackTrace(); + } + return fileName; + } + /**鍥剧墖涓婁紶鐨勬柟娉� * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨� * @param platformIconFile 鍥剧墖鏂囦欢 @@ -339,4 +392,31 @@ return targetFile; } + /**鍒ゆ柇鏄笉鏄棰戞枃浠� + * @param fileName 鏂囦欢鍚嶇О + * @return boolean true鏄棰戞枃浠� + */ + public static boolean getMimeType(String fileName) { + boolean b = false; + FileNameMap fileNameMap = URLConnection.getFileNameMap(); + String type = fileNameMap.getContentTypeFor(fileName); + //鏄棰憈ype鏄负绌虹殑 + if(StringUtils.isEmpty(type)) { + b = true; + } + return b; + } + + /** + * 鍒ゆ柇鏄惁涓鸿棰� + * @param fileName + * @return + */ + public static String isVideo(String fileName) { + FileNameMap fileNameMap = URLConnection.getFileNameMap(); + String type = fileNameMap.getContentTypeFor(fileName); + return type; + } + + } diff --git a/src/main/java/com/hx/util/HttpUtil.java b/src/main/java/com/hx/util/HttpUtil.java index 26caf6f..f64532c 100644 --- a/src/main/java/com/hx/util/HttpUtil.java +++ b/src/main/java/com/hx/util/HttpUtil.java @@ -6,6 +6,8 @@ import org.springframework.web.multipart.MultipartFile; import javax.activation.MimetypesFileTypeMap; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; @@ -23,6 +25,27 @@ /**鍥炶溅鎹㈣,鐢ㄤ簬涓�琛岀殑缁撳熬*/ private static final String LINE_END = "\r\n"; + public static String getInputStream(HttpServletRequest request) throws Exception { + ServletInputStream stream = null; + BufferedReader reader = null; + StringBuffer sb = new StringBuffer(); + try { + stream = request.getInputStream(); + // 鑾峰彇鍝嶅簲 + reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + //logger.error(e); + throw new RuntimeException("璇诲彇杩斿洖鏀粯鎺ュ彛鏁版嵁娴佸嚭鐜板紓甯革紒"); + } finally { + reader.close(); + } + //logger.info("杈撳叆娴佽繑鍥炵殑鍐呭锛�" + sb.toString()); + return sb.toString(); + } public static String post(String requestUrl, String accessToken, String params) throws Exception { @@ -162,13 +185,6 @@ // //System.out.println("HttpURLUtil:"+wxMsgXml); } catch (Exception e) { e.printStackTrace(); - obj = new JSONObject(); - try { - obj.put("status", 1); - obj.put("errMsg", e.getMessage()); - } catch (JSONException e1) { - e1.printStackTrace(); - } } finally { if (con != null) { con.disconnect(); diff --git a/src/main/java/com/hx/util/IPUtils.java b/src/main/java/com/hx/util/IPUtils.java new file mode 100644 index 0000000..b3e2b82 --- /dev/null +++ b/src/main/java/com/hx/util/IPUtils.java @@ -0,0 +1,40 @@ +package com.hx.util; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author ChenJiaHe + * @date 2020-12-2 + */ +public class IPUtils { + + /**鑾峰彇璁块棶ip鍦板潃*/ + public static String getIpAddr(HttpServletRequest request) { + String ip=request.getHeader("x-forwarded-for"); + if(null!=ip && ip.length()!=0 && !"unknown".equalsIgnoreCase(ip)) { + if(ip.indexOf(",")!=-1) { + ip=ip.split(",")[0]; + } + } + if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("Proxy-Client-Ip"); + } + if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("WL-Proxy-Client-IP"); + } + if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("HTTP_CLIENT_IP"); + } + if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("HTTP_X_FORWARDED_FOR"); + } + if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("X-Real-IP"); + } + if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) { + ip=request.getRemoteAddr(); + } + return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; + + } +} 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 杩斿洖鎬绘潯鏁� diff --git a/src/main/java/com/hx/util/StringUtils.java b/src/main/java/com/hx/util/StringUtils.java index 0a43d73..78d4e36 100644 --- a/src/main/java/com/hx/util/StringUtils.java +++ b/src/main/java/com/hx/util/StringUtils.java @@ -228,6 +228,16 @@ return null == str || str.trim().length() == 0 || "undefined".equals(str); } + /**鍒ゆ柇绌�*/ + public static boolean isNull(String str) { + return null == str || str.trim().length() == 0 || "undefined".equals(str); + } + + /**鍒ゆ柇闈炵┖*/ + public static boolean noNull(String str) { + return null != str && str.trim().length() != 0 && !"undefined".equals(str); + } + /** * 鍒ゆ柇瀛楃鍐呭涓槸鍚﹀瓨鍦ㄤ竴涓负绌虹殑瀛楃锟�? * <p> diff --git a/src/main/java/com/hx/util/gaode/AddressCode.java b/src/main/java/com/hx/util/gaode/AddressCode.java new file mode 100644 index 0000000..c634312 --- /dev/null +++ b/src/main/java/com/hx/util/gaode/AddressCode.java @@ -0,0 +1,42 @@ +package com.hx.util.gaode; + +/** + * 鑾峰彇鍦板潃缁忕含搴� + * 鍝嶅簲瀹炰綋鍖� + */ +public class AddressCode { + + /**缁忓害*/ + private String lng; + /**缁村害*/ + private String lat; + /**鍩庡競缂栫爜*/ + private String cityCode; + + public AddressCode() { + } + + public String getLng() { + return lng; + } + + public void setLng(String lng) { + this.lng = lng; + } + + public String getLat() { + return lat; + } + + public void setLat(String lat) { + this.lat = lat; + } + + public String getCityCode() { + return cityCode; + } + + public void setCityCode(String cityCode) { + this.cityCode = cityCode; + } +} diff --git a/src/main/java/com/hx/util/gaode/GaoDeMapUtil.java b/src/main/java/com/hx/util/gaode/GaoDeMapUtil.java new file mode 100644 index 0000000..cdf9cf4 --- /dev/null +++ b/src/main/java/com/hx/util/gaode/GaoDeMapUtil.java @@ -0,0 +1,53 @@ +package com.hx.util.gaode; + +import com.hx.util.HttpUtil; +import com.hx.util.StringUtils; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +/** + * 楂樺痉鍦板浘宸ュ叿 + * @author mgchen + * + */ +public class GaoDeMapUtil { + + /**鑾峰彇缁忕含搴�*/ + public static String URL_CODE = "https://restapi.amap.com/v3/geocode/geo"; + + /** + * 鑾峰彇鍦板潃鐨勭粡绾害 + * @param address + * @param key + * @return + */ + public static AddressCode addressCode(String address,String key) { + AddressCode addressCode = null; + //URL_CODE = URL_CODE+"?key="+key+"&address="+URLEncoder.encode(address,"UTF-8");//鍦板潃缂栬瘧 + URL_CODE = URL_CODE+"?key="+key+"&address="+address; + JSONObject data = HttpUtil.HttpURLUtil(URL_CODE,null); + if(data.optInt("status",0) == 1){ + JSONArray geocodes = data.getJSONArray("geocodes"); + if(geocodes.size()==1){ + JSONObject locationObj = geocodes.getJSONObject(0); + String location = locationObj.optString("location"); + if(StringUtils.noNull(location)){ + String[] locations = location.split(","); + if(locations.length != 2){ + return null; + } + addressCode = new AddressCode(); + addressCode.setLng(locations[0]); + addressCode.setLat(locations[1]); + addressCode.setCityCode(locationObj.optString("citycode")); + } + } + } + return addressCode; + } + + +} -- Gitblit v1.8.0