java 随机流读取文件名_关于Java:如何将一个随机文件名提供给我正在上传的文件...

在Java中上传文件时,为了防止新文件覆盖已有文件,可以通过生成随机文件名来实现。可以使用UUID.randomUUID().toString()或者结合当前时间戳和随机数生成唯一文件名。如果需要确保文件名不重复,可以检查文件是否存在,若存在则重新生成。例如,使用Integer.toString(new Random().nextInt(1000000000)) + ".jpg"生成随机整数作为文件名。
摘要由CSDN通过智能技术生成

本问题已经有最佳答案,请猛点这里访问。

我正在将一个文件上载到一个文件夹中,我将文件名命名为"1.jpg",因此当我上载一个新文件时,它将覆盖现有文件,所以我如何给上传的文件随机命名

我的上传代码在这里

@RequestMapping(value ="/event/uploadFile",headers=("content-type=multipart/*"), method = RequestMethod.POST,consumes ={"application/x-www-form-urlencoded"})

//String quote_upload=C:\fakepath\images.jpg

public @ResponseBody

String uploadFileHandler(

@RequestParam MultipartFile file) {

System.out.println("Creating the directory to store file");

if (!file.isEmpty()) {

try {

byte[] bytes = file.getBytes();

// Creating the directory to store file

String rootPath = System.getProperty("catalina.home");

File dir = new File(rootPath + File.separator +"tmpFiles");

if (!dir.exists())

dir.mkdirs();

// Create the file on server

File serverFile = new File(dir.getAbsolutePath()

+ File.separator+"1.jpg");

BufferedOutputStream stream = new BufferedOutputStream(

new FileOutputStream(serverFile));

stream.write(bytes);

stream.close();

System.out.println("************Server File Location="

+ serverFile.getAbsolutePath());

//return"You successfully uploaded file=" + name;

} catch (Exception e) {

System.out.println("************failes"+ e.getMessage());

//return"You failed to upload" + name +" =>" + e.getMessage();

}

//return"You failed to upload" + name

//+" because the file was empty.";

}

System.out.println("hello");

return"hello";

}

一种选择是附加当前时间(最多秒)和来自Random的随机数生成器。

可以使用java.util.UUID.randomUUID()方法为serverFile创建唯一的名称。

您可以使用:

File.createTempFile(String prefix, String suffix, File directory)

正如JavaDoc所说:

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:

The file denoted by the returned abstract pathname did not exist before this method was invoked, and

Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.

你可以使用

Calendar.getInstance().getTimeInMillis();

它将返回以毫秒为单位的时间。如果您不确定这一点,请在上面附加一些随机数。

如果你不希望这些名字有任何合理的顺序,我可能会选择UUID。像这样的事情应该可以做到:

String filename = UUID.randomUUID().toString();

如果您担心uuid的独特性,请看一下这个线程。简而言之,您不太可能获得两个相同的ID。

为新上载的文件生成随机名称

String fileName = UUID.randomUUID().toString()+YOUR_FILE_EXTENSION;

检查正在上载的目录中是否存在文件

if(serverFile.exists())

如果文件存在,请从步骤1重新开始,直到获得服务器中不存在的文件名。

注意:这不是最佳解决方案,但会满足您的要求。

您可以简单地得到一个随机的大整数,并给您的文件命名,如下所示:

String fileExtension =".jpg";

String fileName = Integer.toString(new Random().nextInt(1000000000)) + fileExtension;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值