作用:当我们在进行上传图片时,这些图片都是要存在数据库当中。它们是以地址的方式存在数据库之中;这个地址可能会重复,假如说在数据库中已经有一个图片是xxxxxxxx地址,再存进来一个这样的地址的话,就会进行覆盖。会丢失之前的图片,所以:我们需要对上传图片进行重新命名,防止我们的图片丢失
下面直接上代码:
package com.util;
import java.util.Random;
/**
* @author life
* @create 2020-01-06 13:38
*/
public class GenerateIdUtil {
private static char[] constant =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
};
public static String GenerateRandomNumber(int Length){
StringBuilder newRandom = new StringBuilder(50);
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.append(constant[rd.nextInt(62)]);
}
return newRandom.toString();
}
public static void main(String[] args) {
System.out.println(GenerateRandomNumber(60));
}
}
package com.util;
/**
* @author life
* @create 2020-01-06 13:40
*/
public class GenerateIdUtil1 {
private static String randomStr ( int len )
{
if (len == 0)
{
return "";
}
int a = (int) ( Math.random () * 3 );
if (a == 0)
{
return ( (int) ( Math.random () * 10 ) ) + randomStr (len - 1);
}
else if (a == 1)
{
return ( (char) ( (int) ( Math.random () * 26 ) + 65 ) ) + randomStr (len - 1);
}
else
{
return ( (char) ( (int) ( Math.random () * 26 ) + 97 ) ) + randomStr (len - 1);
}
}
public static void main ( String[] args )
{
System.out.println (randomStr (20));
}
}
package test;
import java.util.Date;
/**
* @author life
* @create 2020-01-06 14:02
* 图片上传测试
*/
public class ImgText {
public static void main(String[] args) {
String filePath="upload/";
String fileName="aa.jpg";
filePath+=new Date().getTime()+fileName.substring(fileName.lastIndexOf('.'));
System.out.println(filePath);
}
}