/** * 有重名文件,自动在后面添加(1)、(2)、(3)... * path : C:\Users\Administrator\Desktop\-sitemap.html */ private static String isFileName(String path) { String suffix ="."+ path.substring(path.lastIndexOf(".") + 1); String fileName = path.substring(path.lastIndexOf("\\")); String createFilePath = path.substring(0, path.lastIndexOf("\\") + 1); String name = fileName.substring(fileName.lastIndexOf("\\"),fileName.lastIndexOf(".")); File file = new File(path); if (file.exists()) { int i = 1; while (true) { File file1 = new File(createFilePath + name + '(' + i + ')' + suffix); boolean result = file1.exists(); if (!result) { return createFilePath + name + '(' + i + ')' + suffix; } i++; } } return createFilePath + name + suffix; }
解决下载时文件路径重复,在原文件名后加一
最新推荐文章于 2024-04-18 07:23:46 发布