Java之上传图片自动重命名实现

图片自动重命名实现

作用:当我们在进行上传图片时,这些图片都是要存在数据库当中。它们是以地址的方式存在数据库之中;这个地址可能会重复,假如说在数据库中已经有一个图片是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);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值