In operations such as file uploading, We will rename the file to prevent file name conflicts. Here we will introduce the implementation of IP + timestamp naming:
There is nothing to say about the Code directly. The implementation is quite simple, but practical.
Package COM. xidian. BBS. util; import Java. text. simpledateformat; import Java. util. date; import Java. util. random; public class iptimestamp {private simpledateformat SIM = NULL; // used to obtain the time private string IP = NULL; Public iptimestamp () {} public iptimestamp (string IP) {This. IP = IP;} Public String getiptimerand () {stringbuffer SBF = new stringbuffer (); If (this. IP address! = NULL) {string a [] = This. IP. split ("\\. "); // split the IP address based on the point, but the point must be escaped for (INT I = 0; I <. length; I ++) {SBF. append (this. addzero (A [I], 3); // call the zero-padding method. If there are less than three IP addresses, the system automatically complements the three IP addresses} SBF. append (this. gettimestamp (); // use this to call the external Method random = new random (); // to generate a random number for (INT I = 0; I <3; I ++) {// generates a random number SBF. append (random. nextint (10); // each random number cannot exceed 10} return SBF. tostring () ;}@ suppresswarnings ("UNUSED") Private string getdate () {// Date and Time Implementation this. sim = new simpledateformat ("yyyy-mm-dd hh: mm: Ss. SSS "); return this. sim. format (new date ();} private string gettimestamp () {// returns the timestamp this. sim = new simpledateformat ("yyyymmddhhmmsssss"); return this. sim. format (new date ();} private string addzero (string STR, int Len) {// Method for auto-zeroing, the parameter is the specified string and length stringbuffer S = new stringbuffer (); S. append (STR); While (S. length () <Len) {S. insert (0, "0"); // perform the zero position fill operation} return S. tostring () ;}// test public static void main (string [] ary) {iptimestamp = new iptimestamp ("172.163.222 "); // call the Construction Method System with parameters. out. println (iptimestamp. getiptimerand ());}}