如何保留文件扩展名重命名文件?
在我的情况下,我想要上传文件时重命名文件。我正在使用Apache commons fileupload库。
以下是我的代码片段:
File uploadedFile = new File(path + "/" + fileName);
item.write(uploadedFile);
//renaming uploaded file with unique value.
String id = UUID.randomUUID().toString();
File newName = new File(path + "/" + id);
if(uploadedFile.renameTo(newName)) {
} else {
System.out.println("Error");
}
上述代码也会更改文件扩展名。如何保留它? 是否有使用apache commons file upload库的好方法?