如何更改 MultipartFile 的原始文件名 [英] How to change originalFilename of MultipartFile
本文介绍了如何更改 MultipartFile 的原始文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务器端有一个 MultipartFile 文件.我想更改这个文件的原始文件名,但该类只支持getOriginalFilename().
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().
谁能帮我解决这个问题?PS:是上传的图片文件.
Can anyone help me with this? PS: It is an image file uploaded.
非常感谢.
推荐答案
您可以使用 MockMultipartFile 类更改名称.例如,为多部分文件添加时间戳.
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());
然后使用具有新名称的 multipartFile或者你可以像这样保存之前重命名文件
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())
这篇关于如何更改 MultipartFile 的原始文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文