From c2976b80e8850be2d28dc2132c6659856b871f3c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期四, 08 五月 2025 21:10:42 +0800 Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV --- pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/fm/FileNameIdUtil.java | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 109 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/fm/FileNameIdUtil.java b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/fm/FileNameIdUtil.java new file mode 100644 index 0000000..96be7b7 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/fm/FileNameIdUtil.java @@ -0,0 +1,109 @@ +package com.dy.pipIrrWebFile.fm; + +import java.util.Calendar; + +public class FileNameIdUtil { + + private static int add = 0 ; + private static int chengShu = 1000 ; + private static int maxAdd = 999 ; + private static long last = 0 ; + + //鍚庣紑 + private static String suffix = "0" ; + + static { + last = current() ; + } + + /** + * 涓鸿嚜瀹炵幇绋嬪簭鎻愪緵鐨処D鐢熸垚鍣� + * 15闀垮害ID锛屽勾搴﹀彇涓や綅锛屽鏋滄槸17浣嶉暱搴D锛屽勾搴﹀彇鍥涗綅锛岄偅17浣嶆暟瀛楄秴鍑轰簡javascript鐨勮〃鏁拌寖鍥� + */ + public String generate(){ + return doGenerate() ; + } + + /** + * 璁剧疆鍚庣紑锛屼笉鍚屽瓙绯荤粺璁剧疆涓嶅悓鐨勫悗缂� + * @param suffix + */ + public static void setSuffix(String suffix)throws Exception{ + if(suffix == null || suffix.trim().equals("")){ + throw new Exception("鍚庣紑涓嶈兘涓虹┖") ; + } + FileNameIdUtil.suffix = suffix.trim() ; + } + + /** + * 鎵ц鐢熸垚 + * @return + */ + private synchronized String doGenerate(){ + Long id = null ; + long now = current() ; + if(now != last){ + //涓婃鐢熸垚ID 涓庢湰娆$敓鎴怚D 涓嶅湪鍚屼竴绉掑唴 + last = now ; + add = 0 ; + id = last * chengShu + add ++; + }else{ + //涓婃鐢熸垚ID 涓庢湰娆$敓鎴怚D 鍦ㄥ悓涓�绉掑唴 + if(add == maxAdd){ + //闄勫姞閲忓凡缁忕敤灏� + waitNextSecond(last) ;//绛夊埌涓嬩竴绉� + id = last * chengShu + add ++ ;//杩斿洖涓婁竴绉掔敓鎴愮殑ID + add = 0 ;//闄勫姞閲忓綊闆讹紝涓轰笅涓�绉掑噯澶� + }else{ + //闄勫姞閲忔湭鐢ㄥ敖 + id = last * chengShu + add ++ ; + } + } + return id + suffix ; + } + /** + * 绛夊緟涓嬩竴绉掑埌鏉� + * @param last + */ + private void waitNextSecond(Long last){ + try { + Thread.sleep(10); + } catch (InterruptedException e) { + }finally{ + long now = current() ; + if(now == last){ + waitNextSecond(last) ; + } + } + } + + + + /** + * 鏍煎紡涓� 150516010203 + * @return + */ + private static long current(){ + Calendar cal = Calendar.getInstance(); + long d = (cal.get(Calendar.YEAR) % 100) * 10000000000L + + (cal.get(Calendar.MONTH) + 1) * 100000000L + + cal.get(Calendar.DAY_OF_MONTH) * 1000000L + + cal.get(Calendar.HOUR_OF_DAY) * 10000L + + cal.get(Calendar.MINUTE) * 100L + + cal.get(Calendar.SECOND) ; + + return d ; + } + + public static void main(String args[]){ + FileNameIdUtil o = new FileNameIdUtil() ; + int total = 800 ; + long start = System.currentTimeMillis() ; + for(int i = 0 ; i < total ; i++){ + System.out.println((String)(o.generate())) ; + } + long end = System.currentTimeMillis() ; + System.out.println("浜х敓" + total + "ID鐢ㄦ椂" + (end - start) + "姣"); + } + +} -- Gitblit v1.8.0