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