Le Kim Trang
Le Kim Trang

Reputation: 459

How to change originalFilename of MultipartFile

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.

Upvotes: 6

Views: 16895

Answers (3)

Maifee Ul Asad
Maifee Ul Asad

Reputation: 4607

public String storeFile(MultipartFile file) {
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
//this will provide the complete filename, let "file.txt";
        File fileSaved = fileRepository.save(new File());
//let's save that on a repo, not file, just some reference, easy to handle
        fileName = fileName.substring(0, fileName.length() - 4) + fileSaved.getFileId() + fileName.substring(fileName.length() - 4);
//change the file name a bit, just put the id from the repo, it will be unique by default, right ? and what's that 4 - ".txt" has 4 alphabets, currently i neeed to handle only txt, so I hard-coded this
        Path targetLocation = this.fileStorageLocation.resolve(fileName);
        Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
//it will be never replaced, even filenames are same,id will be unique
}

Upvotes: 0

Udara Seneviratne
Udara Seneviratne

Reputation: 2493

Just create a new MultipartFile instance by updating its

originalFileName

with new one other files with the same existing file fields.

Just call following method as

getNewFile("newFileName", currentFile)

current file is the MultipartFile object of the existing file.

private MultipartFile getNewFile(String fileName, MultipartFile currentFile){
        return new MultipartFile() {
            @Override
            public String getName() {
                return currentFile.getName();
            }

            @Override
            public String getOriginalFilename() {
                return fileName;
            }

            @Override
            public String getContentType() {
                return currentFile.getContentType();
            }

            @Override
            public boolean isEmpty() {
                return currentFile.isEmpty();
            }

            @Override
            public long getSize() {
                return currentFile.getSize();
            }

            @Override
            public byte[] getBytes() throws IOException {
                return currentFile.getBytes();
            }

            @Override
            public InputStream getInputStream() throws IOException {
                return currentFile.getInputStream();
            }

            @Override
            public void transferTo(File file) throws IOException, IllegalStateException {

            }
        };
    }

Upvotes: 0

Dave Kraczo
Dave Kraczo

Reputation: 908

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())

Upvotes: 8

Related Questions