Duplicate file problem

Duplicate file problem

When the file upload, multiple users upload the same file name, the file will be overwritten. Solve this problem you can create a custom Java class one way to get the same file extension, which will replace the file name as a string randomly generated.

Code:

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;
    }
}

Guess you like

Origin www.cnblogs.com/jascen/p/11317867.html