I have a MultipartFile file on server side. I would like to change the original file name of this file, yet the class only support getOriginalFilename().
Can anyone help me with this? PS: It is an image file uploaded.
Thanks alot.
You can't change original name of MultipleFile . But you can save this file with another name, when will pass File to transferTo() method.
You can change the name with MockMultipartFile class. For example, to add a timestamp to multipart file.
MultipartFile multipartFile = new MockMultipartFile(FilenameUtils.getBaseName(oldMultipartFile.getOriginalFilename()).concat(new SimpleDateFormat("yyyyMMddHHmm").format(new Date())) + "." + FilenameUtils.getExtension(oldMultipartFile.getOriginalFilename()), oldMultipartFile.getInputStream());
and then use multipartFile with new name or you can just rename file before save like this
String currentDate = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
file.getOriginalFilename().replace(file.getOriginalFilename(), FilenameUtils.getBaseName(file.getOriginalFilename()).concat(currentDate) + "." + FilenameUtils.getExtension(file.getOriginalFilename())).toLowerCase())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With