IP address + timestamp rename the file

Source: Internet
Author: User

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 ());}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.