當前日期+隨機數+檔名(採用兩種方法實現的工具類)
多檔案上傳下載時,常常需要考慮上傳統一檔案多次,怎來精準的區別檔案的名字,下面兩種方法都可以實現
1.第一種實現方式
//從服務端傳過來的path路徑擷取檔名
String name = path.substring(39);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
String strFileName =format.format(new Date())+ new Random().nextInt();
File file = new File(sdFile + "/video", strFileName+"-"+name); // 在android的sdcard上建立檔案物件
2.第二種實現方式
* 生成隨機檔名
*/
public String generateRandomFilename(){
String RandomFilename = "";
Random rand = new Random();//生成隨機數
int random = rand.nextInt();
Calendar calCurrent = Calendar.getInstance();
int intDay = calCurrent.get(Calendar.DATE);
int intMonth = calCurrent.get(Calendar.MONTH) + 1;
int intYear = calCurrent.get(Calendar.YEAR);
String now = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" +
String.valueOf(intDay) + "_";
log.debug("生成於今日的檔名字首為:"+now);
RandomFilename = now + String.valueOf(random > 0 ? random : ( -1) * random) + ".";
return RandomFilename;
}
相關文章
- 獲取當前時間戳和隨機數的獲取、Java Random、ThreadLocalRandom、UUID類中的方法應用(隨機數)時間戳隨機JavarandomthreadUI
- 運用JS 實現隨機點名 (隨機點名)JS隨機
- 用JS實現隨機點名小案例JS隨機
- 幾種生成隨機數方法隨機
- numpy各種生成隨機數的方法隨機
- 6_Linux修改主機名的兩種方法(20190115)Linux
- Java之獲取隨機數的4種方法Java隨機
- Matlab 隨機生成兩個數值之間的隨機數Matlab隨機
- python之建立類的兩種方法Python
- html隨意拖動內容位置的兩種實現方式HTML
- Python實現"數字轉換為十六進位制"的兩種方法
- 獲取當前時間往前的日期
- 兩種方法使vue實現jQuery呼叫VuejQuery
- RabbitMQ實現延時訊息的兩種方法MQ
- 手機直播原始碼,當前頁卡指示器的簡單實現方法原始碼
- Android開發:獲取當前系統時間和日期的方法Android
- 批處理 求得到當前資料夾下指定字尾的檔名的各種形式
- 第 9 篇:實現分類、標籤、歸檔日期介面
- android檢視當前應用的的包名和activityAndroid
- Java實現隨機抽獎的方法有哪些Java隨機
- 批處理以當前時間為檔名建立檔案
- 教你在Nodejs中如何獲取當前函式被呼叫的行數及檔名NodeJS函式
- Golang兩種方法實現MD5加密Golang加密
- 使用JavaScript實現獲取當前日期JavaScript
- SAP ABAP 字串內數字去前導零的兩種方法(正則/拼接)字串
- JS實現圖片的淡入和淡出的兩種方法,如有不足,還請前輩多多指導^-^~JS
- JavaScript隨機數實現防止快取JavaScript隨機快取
- 用js獲取當前月份的天數JS
- 一個隨機數的類c++隨機C++
- 採用 SwiftNIO 實現一個類似 Express 的 Web 框架SwiftExpressWeb框架
- [Leetcode]315.計算右側小於當前元素的個數 (6種方法)LeetCode
- JavaScript隨機數的應用JavaScript隨機
- 使用Math類生成隨機數隨機
- 隨機數種子(random seed)隨機random
- 關於如何產生隨機數的幾種方法彙總!(2018.07.08)隨機
- Java 建立 PDF 檔案包的兩種方法Java
- 下載GitHub上檔案的兩種方法Github
- C#程式實現軟體開機自動啟動的兩種常用方法C#
- ts - 兩種方法實現忽略大小寫的字串排序字串排序