文件重名问题

文件重名问题

在文件上传时,多个用户上传相同文件名的文件时,会将文件进行覆盖。解决该问题可以创建一个Java类自定义一个方法,先获取同名文件的后缀名,将文件名替换成随机生成的一段字符串。

代码实现:

import java.util.UUID;

public class UploadUtils {
    
    public static String getUuidFileName(String fileName){
        //先获取同名文件的后缀名,再将其文件名替换成随机字符串
        int idx = fileName.lastIndexOf(".");
        String exName = fileName.substring(idx);
        // 生成随机字符串:
        String uuidFileName = UUID.randomUUID().toString().replace("-", "")+exName;
        return uuidFileName;
    }
}

猜你喜欢

转载自www.cnblogs.com/jascen/p/11317867.html