Generate a random file name based on the time random number. SpringMVC uploads the file.

Source: Internet
Author: User

Public static String generateRandomFilename (){
String fourRandom = "";
// Generate a random number of four digits (less than four digits before adding zero)
Int randomNum = (int) (Math. random () * 10000 );
FourRandom = randomNum + "";
Int randLength = fourRandom. length ();
If (randLength <4 ){
For (int I = 1; I <= 4-randLength; I ++)
FourRandom = fourRandom + "0 ";
}
StringBuilder sb = new StringBuilder ("");
Sb. append (CalendarUtil. getYear ())
. Append (twoNumbers (CalendarUtil. getMonth ()))
. Append (twoNumbers (CalendarUtil. getDayOfMonth ()))
. Append (twoNumbers (CalendarUtil. getHour ()))
. Append (twoNumbers (CalendarUtil. getMinute ()))
. Append (twoNumbers (CalendarUtil. getSecond ()))
. Append (fourRandom );
Return sb. toString ();
}
Private static String twoNumbers (int number ){
String _ number = number + "";
If (_ number. length () <2 ){
_ Number = "0" + _ number;
}
Return _ number;
}
 
------------------------------------------------------------------- SpringMVC uploads files
@ RequestMapping (value = "/doAdd", method = RequestMethod. POST)
Public String doAdd (@ RequestParam ("imgFile") MultipartFile file, HttpServletRequest request, WaterSingle waterSingle ){
If (null! = File ){
If (StringUtils. isEmpty (file. getOriginalFilename ())){
WaterSingle. setWaterName ("");
} Else {
String realFileName = file. getOriginalFilename ();
RealFileName = Utils. generateRandomFilename () + realFileName. substring (realFileName. lastIndexOf ("."));
String path = GlobalStatic. uploadFilePath5 + realFileName;
Utils. uploadfile (file, path );
User user = (User) getSession (). getAttribute ("currentUser ");
WaterSingle. setWaterName (realFileName );
WaterSingle. setUserName ("Login Name:" + user. getLoginName () + ", real name:" + user. getTrueName ());
WaterSingle. setUploadTime (new Date ());
}
WaterSingleService. addWaterSingle (waterSingle );
}
Return "redirect:/watersingle/list ";
}
 
Public static boolean uploadfile (MultipartFile file, String fileFullName ){
Try {
FileOutputStream fileOutputStream = new FileOutputStream (new java. io. File (fileFullName ));
InputStream FCM = file. getInputStream ();
Byte [] buff = new byte [1024];
@ SuppressWarnings ("unused ")
Int length = 0;
While (length = FS. read (buff)> 0 ){
FileOutputStream. write (buff );
}
FileOutputStream. flush ();
FileOutputStream. close ();
FCM. close ();
Return true;
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
Throw new RuntimeException (e );
} Catch (IOException e)
{
Throw new RuntimeException (e );
} Www.2cto.com
// Return false;
 
}
Public static void copyFile (File src, File dst ){
InputStream in = null;
OutputStream out = null;
Try {
In = new BufferedInputStream (new FileInputStream (src), BUFFER_SIZE );
Out = new BufferedOutputStream (new FileOutputStream (dst ),
BUFFER_SIZE );
Byte [] buffer = new byte [BUFFER_SIZE];
Int len = 0;
While (len = in. read (buffer)> 0 ){
Out. write (buffer, 0, len );
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (null! = In ){
Try {
In. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
If (null! = Out ){
Try {
Out. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}
Author: gdn_wolf

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.