From 0546530fe7e1696cf3ae4eaf9e79a5e7c4e203fe Mon Sep 17 00:00:00 2001 From: ANDRU-PC\Andru <769883346@qq.com> Date: 星期四, 21 七月 2022 18:43:26 +0800 Subject: [PATCH] Merge branch 'master' of http://1.15.4.62/r/~chenjiahe/hx_common --- src/main/java/com/hx/util/ExcelUtil.java | 91 +++++++++++++ src/main/java/com/hx/util/COSUtil.java | 140 +++++++++++++++++++ src/main/java/com/hx/util/DownFileUtil.java | 35 +++++ src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java | 103 ++++++++++++++ 4 files changed, 364 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java b/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java index 2035142..e3e2562 100644 --- a/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java +++ b/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java @@ -605,4 +605,107 @@ return obj; } + + + /**鐢熸垚鏃犻檺浜岀淮鐮�,杩斿洖涓存椂鏂� + * @param at 寰俊token + * @param scene 鍙傛暟锛屽彧鑳�32浣嶏紝鏈�濂戒笉瑕佷腑鏂� + * @param page 璺宠浆閾炬帴 + * @param width 瀹藉害 + * @param autoColor 榛樿false + * @param lineColor 榛樿null + * @param isHyaline 榛樿false + * @return + */ + public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, + JSONObject lineColor, boolean isHyaline, String env_version) throws IOException { + String imgUrl = null; + InputStream in = null; + HttpURLConnection conn = null; + + //鍒涘缓涓存椂鏂囦欢 + File file = File.createTempFile("temp", ".jpg"); + + try { + //鐢熸垚鍙戦�佹暟鎹� + JSONObject obj = new JSONObject(); + obj.put("scene", scene); + obj.put("width", width); + obj.put("page", page); + obj.put("auto_color", autoColor); + obj.put("line_color", lineColor); + obj.put("is_hyaline", isHyaline); + obj.put("env_version", env_version); + // 鍒涘缓url璧勬簮 + URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); + // 寤虹珛http杩炴帴 + conn = (HttpURLConnection) url.openConnection(); + // 璁剧疆鍏佽杈撳嚭 + conn.setDoOutput(true); + conn.setDoInput(true); + // 璁剧疆涓嶇敤缂撳瓨 + conn.setUseCaches(false); + // 璁剧疆浼犻�掓柟寮� + conn.setRequestMethod("POST"); + // 璁剧疆缁存寔闀胯繛鎺� + conn.setRequestProperty("Connection", "Keep-Alive"); + // 璁剧疆鏂囦欢瀛楃闆�: + conn.setRequestProperty("Charset", "UTF-8"); + // 璁剧疆鏂囦欢绫诲瀷: + conn.setRequestProperty("contentType", "application/json"); + // 寮�濮嬭繛鎺ヨ姹� + conn.connect(); + OutputStream out = conn.getOutputStream(); + // 鍐欏叆璇锋眰鐨勫瓧绗︿覆 + out.write((obj.toString()).getBytes()); + out.flush(); + out.close(); + + // 璇锋眰杩斿洖鐨勭姸鎬� + if (conn.getResponseCode() == 200) { + // 璇锋眰杩斿洖鐨勬暟鎹� + in = conn.getInputStream(); + //杈撳叆鍒颁复鏃舵枃浠� + /*OutputStream os = new FileOutputStream(file); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + }*/ + FileUtils.copyInputStreamToFile(in, file); + + conn.disconnect(); + conn = null; + } + + if (in != null) { + in.close(); + in = null; + } + + if (conn != null) { + conn.disconnect(); + conn = null; + } + }catch (Exception e) { + e.printStackTrace(); + if (in != null) { + try { + in.close(); + }catch (Exception ep) { + ep.printStackTrace(); + } + in = null; + } + + if (conn != null) { + conn.disconnect(); + conn = null; + } + }finally { + file.deleteOnExit(); + } + return file; + } + } diff --git a/src/main/java/com/hx/util/COSUtil.java b/src/main/java/com/hx/util/COSUtil.java index 62cc1fe..a10b1e1 100644 --- a/src/main/java/com/hx/util/COSUtil.java +++ b/src/main/java/com/hx/util/COSUtil.java @@ -1,22 +1,26 @@ package com.hx.util; -import com.aliyun.oss.OSSClient; -import com.aliyun.oss.model.OSSObject; +import com.hx.exception.TipsException; 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.exception.CosClientException; +import com.qcloud.cos.exception.CosServiceException; +import com.qcloud.cos.http.HttpProtocol; +import com.qcloud.cos.model.GetObjectRequest; 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 com.qcloud.cos.transfer.Download; +import com.qcloud.cos.transfer.TransferManager; 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; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /**鑵捐浜� COS * @author ChenJiaHe @@ -109,4 +113,130 @@ } + /**涓嬭浇鏂囦欢 + * @param key 涓婁紶璺緞锛堝寘鎷浘鐗囧悕绉板拰鍜屽悗缂�锛夛紝鎸囧畾瑕佷笂浼犲埌 COS 涓婂璞¢敭 + * @param secretId 鐢ㄦ埛id + * @param secretKey 鐢ㄦ埛绉橀挜 + * @param regionName 瀛樺湪鍩燂紝鍙傝�冭吘璁簯 + * @param bucketName 鎸囧畾瑕佷笂浼犲埌鐨勫瓨鍌ㄦ《 + */ + public static File download(String key,String secretId, String secretKey,String regionName,String bucketName){ + + // 浣跨敤楂樼骇鎺ュ彛蹇呴』鍏堜繚璇佹湰杩涚▼瀛樺湪涓�涓� TransferManager 瀹炰緥锛屽鏋滄病鏈夊垯鍒涘缓 + // 璇︾粏浠g爜鍙傝鏈〉锛氶珮绾ф帴鍙� -> 鍒涘缓 TransferManager + TransferManager transferManager = createTransferManager( secretId, secretKey,regionName); + // 鏈湴鏂囦欢璺緞 + File downloadFile = null; + try { + GetObjectRequest getObjectRequest; + System.out.println("key:"+key); + System.out.println("bucketName:"+bucketName); + System.out.println("regionName:"+regionName); + System.out.println("regionName:"+regionName+".myqcloud.com"); + //鎴彇鏂囦欢鍚嶇О + String[] datas = key.split(regionName+".myqcloud.com/"); + if(datas.length ==1){ + getObjectRequest = new GetObjectRequest(bucketName, datas[0]); + }else if(datas.length ==2){ + System.out.println("datas[1]:"+datas[1]); + getObjectRequest = new GetObjectRequest(bucketName, datas[1]); + }else{ + throw new TipsException("鏂囦欢璺緞閿欒銆恔ey銆�"); + } + + ////鐢熸垚涓存椂鏂囦欢 + //鑾峰彇鍚庣紑鍚嶇О + String suffix = key.substring(key.lastIndexOf(".")); + downloadFile = File.createTempFile("temp", suffix); + + // 杩斿洖涓�涓紓姝ョ粨鏋� Donload, 鍙悓姝ョ殑璋冪敤 waitForCompletion 绛夊緟涓嬭浇缁撴潫, 鎴愬姛杩斿洖 void, 澶辫触鎶涘嚭寮傚父 + Download download = transferManager.download(getObjectRequest, downloadFile); + download.waitForCompletion(); + } catch (CosServiceException e) { + e.printStackTrace(); + } catch (CosClientException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + }catch (IOException e){ + e.printStackTrace(); + }finally { + // 纭畾鏈繘绋嬩笉鍐嶄娇鐢� transferManager 瀹炰緥涔嬪悗锛屽叧闂箣 + // 璇︾粏浠g爜鍙傝鏈〉锛氶珮绾ф帴鍙� -> 鍏抽棴 TransferManager + shutdownTransferManager(transferManager); + } + + return downloadFile; + } + + /** + * 鍒涘缓 TransferManager 瀹炰緥锛岃繖涓疄渚嬬敤鏉ュ悗缁皟鐢ㄩ珮绾ф帴鍙� + * @param secretId + * @param secretKey + * @return + */ + public static TransferManager createTransferManager(String secretId, String secretKey,String regionName) { + // 鍒涘缓涓�涓� COSClient 瀹炰緥锛岃繖鏄闂� COS 鏈嶅姟鐨勫熀纭�瀹炰緥銆� + // 璇︾粏浠g爜鍙傝鏈〉: 绠�鍗曟搷浣� -> 鍒涘缓 COSClient + COSClient cosClient = createCOSClient( secretId, secretKey,regionName); + + // 鑷畾涔夌嚎绋嬫睜澶у皬锛屽缓璁湪瀹㈡埛绔笌 COS 缃戠粶鍏呰冻锛堜緥濡備娇鐢ㄨ吘璁簯鐨� CVM锛屽悓鍦板煙涓婁紶 COS锛夌殑鎯呭喌涓嬶紝璁剧疆鎴�16鎴�32鍗冲彲锛屽彲杈冨厖鍒嗙殑鍒╃敤缃戠粶璧勬簮 + // 瀵逛簬浣跨敤鍏綉浼犺緭涓旂綉缁滃甫瀹借川閲忎笉楂樼殑鎯呭喌锛屽缓璁噺灏忚鍊硷紝閬垮厤鍥犵綉閫熻繃鎱紝閫犳垚璇锋眰瓒呮椂銆� + ExecutorService threadPool = Executors.newFixedThreadPool(32); + + // 浼犲叆涓�涓� threadpool, 鑻ヤ笉浼犲叆绾跨▼姹狅紝榛樿 TransferManager 涓細鐢熸垚涓�涓崟绾跨▼鐨勭嚎绋嬫睜銆� + TransferManager transferManager = new TransferManager(cosClient, threadPool); + + return transferManager; + } + + /** + * 鍏抽棴绠$悊 + * @param transferManager + */ + public static void shutdownTransferManager(TransferManager transferManager) { + // 鎸囧畾鍙傛暟涓� true, 鍒欏悓鏃朵細鍏抽棴 transferManager 鍐呴儴鐨� COSClient 瀹炰緥銆� + // 鎸囧畾鍙傛暟涓� false, 鍒欎笉浼氬叧闂� transferManager 鍐呴儴鐨� COSClient 瀹炰緥銆� + transferManager.shutdownNow(true); + } + + /** + * 鍒涘缓 COSClient 瀹炰緥锛岃繖涓疄渚嬬敤鏉ュ悗缁皟鐢ㄨ姹� + * @param secretId + * @param secretKey + * @return + */ + public static COSClient createCOSClient(String secretId, String secretKey,String regionName) { + // 璁剧疆鐢ㄦ埛韬唤淇℃伅銆� + // SECRETID 鍜� SECRETKEY 璇风櫥褰曡闂鐞嗘帶鍒跺彴 https://console.cloud.tencent.com/cam/capi 杩涜鏌ョ湅鍜岀鐞� + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); + + // ClientConfig 涓寘鍚簡鍚庣画璇锋眰 COS 鐨勫鎴风璁剧疆锛� + ClientConfig clientConfig = new ClientConfig(); + + // 璁剧疆 bucket 鐨勫湴鍩� + // COS_REGION 璇峰弬鐓� https://cloud.tencent.com/document/product/436/6224 + clientConfig.setRegion(new Region(regionName)); + + // 璁剧疆璇锋眰鍗忚, http 鎴栬�� https + // 5.6.53 鍙婃洿浣庣殑鐗堟湰锛屽缓璁缃娇鐢� https 鍗忚 + // 5.6.54 鍙婃洿楂樼増鏈紝榛樿浣跨敤浜� https + clientConfig.setHttpProtocol(HttpProtocol.https); + + // 浠ヤ笅鐨勮缃紝鏄彲閫夌殑锛� + + // 璁剧疆 socket 璇诲彇瓒呮椂锛岄粯璁� 30s + clientConfig.setSocketTimeout(300*1000); + // 璁剧疆寤虹珛杩炴帴瓒呮椂锛岄粯璁� 30s + clientConfig.setConnectionTimeout(30*1000); + + // 濡傛灉闇�瑕佺殑璇濓紝璁剧疆 http 浠g悊锛宨p 浠ュ強 port + //clientConfig.setHttpProxyIp("httpProxyIp"); + //clientConfig.setHttpProxyPort(80); + + // 鐢熸垚 cos 瀹㈡埛绔�� + return new COSClient(cred, clientConfig); + } + + } diff --git a/src/main/java/com/hx/util/DownFileUtil.java b/src/main/java/com/hx/util/DownFileUtil.java index 28542ac..a6535b0 100644 --- a/src/main/java/com/hx/util/DownFileUtil.java +++ b/src/main/java/com/hx/util/DownFileUtil.java @@ -187,4 +187,39 @@ } } + /** + * 涓嬭浇鏂囦欢宸ュ叿锛堟彁绀洪�夋嫨璺緞锛� + * + * @param downfile + * 瀵煎嚭鐨勬枃浠� + * @param fileName + * 瀵煎嚭鐨勬枃浠跺悕绉� + */ + public static void DownFileTips( HttpServletResponse response, File downfile, String fileName) { + try { + if(StringUtils.isEmpty(fileName)){ + fileName = downfile.getName(); + } + String filename = ""; + 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 5e9c4d6..a6dfaac 100644 --- a/src/main/java/com/hx/util/ExcelUtil.java +++ b/src/main/java/com/hx/util/ExcelUtil.java @@ -498,6 +498,97 @@ * @return * @throws Exception */ + public static List<List<String>> getExcelDataCompatible(File file,boolean header) throws Exception { + try { + + String fileName = file.getName(); + if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { + throw new TipsException("涓婁紶鏂囦欢鏍煎紡涓嶆纭�"); + } + + // 缁撴灉闆� + List<List<String>> list = new ArrayList<>(); + Workbook book = create(new BufferedInputStream(new FileInputStream(file))); + + // 閬嶅巻璇ヨ〃鏍间腑鎵�鏈夌殑宸ヤ綔琛紝i琛ㄧず宸ヤ綔琛ㄧ殑鏁伴噺 getNumberOfSheets琛ㄧず宸ヤ綔琛ㄧ殑鎬绘暟 + for(int s=0;s<book.getNumberOfSheets();s++) { + Sheet hssfsheet = book.getSheetAt(s); + int col = 0; + // 閬嶅巻璇ヨ鎵�鏈夌殑琛�,j琛ㄧず琛屾暟 getPhysicalNumberOfRows琛岀殑鎬绘暟 鍘婚櫎鏍囬 + for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { + Row hssfrow = hssfsheet.getRow(j); + if(hssfrow!=null){ + if(j == 0) { + col = hssfrow.getPhysicalNumberOfCells(); + if(!header) { + //涓嶅寘鎷〃澶� + continue; + } + } + // 鍗曡鏁版嵁 + List<String> arrayString = new ArrayList<>(); + for (int i = 0; i < col; i++) { + Cell cell = hssfrow.getCell(i); + if (cell == null) { + arrayString.add(""); + } else if (cell.getCellType() == 0) { + // arrayString[i] = new Double(cell.getNumericCellValue()).toString(); + if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) { + short format = cell.getCellStyle().getDataFormat(); + if(format == 14 || format == 31 || format == 57 || format == 58){ + //鏃ユ湡锛堜腑鏂囨椂闂存牸寮忕殑锛� + Date d = cell.getDateCellValue(); + DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + // DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + arrayString.add(formater.format(d)); + //arrayString[i] = formater.format(d); + }else if (HSSFDateUtil.isCellDateFormatted(cell)) { + Date d = cell.getDateCellValue(); + //DateFormat formater = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃� HH鏃秏m鍒唖s绉�"); + DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + arrayString.add(formater.format(d)); + //arrayString[i] = formater.format(d); + } else { + if(HSSFCell.CELL_TYPE_STRING == cell.getCellType()){ + arrayString.add(cell.getStringCellValue()); + //arrayString[i] =cell.getStringCellValue(); + }else if(HSSFCell.CELL_TYPE_FORMULA==cell.getCellType()){ + arrayString.add(cell.getCellFormula()); + //arrayString[i] =cell.getCellFormula(); + }else if(HSSFCell.CELL_TYPE_NUMERIC== cell.getCellType()){ + HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); + arrayString.add(dataFormatter.formatCellValue(cell)); + //arrayString[i] =dataFormatter.formatCellValue(cell); + } + } + } + } else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){ + arrayString.add(""); + //arrayString[i] = ""; + } else { // 濡傛灉EXCEL琛ㄦ牸涓殑鏁版嵁绫诲瀷涓哄瓧绗︿覆鍨� + arrayString.add(cell.getStringCellValue().trim()); + //arrayString[i] = cell.getStringCellValue().trim(); + } + } + list.add(arrayString); + } + } + } + return list; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + + + /**璇诲彇excel鏂囦欢锛屽吋瀹�2003鍜�2007 + * 閫氳繃娴佽鍙朎xcel鏂囦欢 + * @return + * @throws Exception + */ public static List<List<String>> getExcelDataCompatible(MultipartFile file,boolean header) throws Exception { try { -- Gitblit v1.8.0