SoFunction
Updated on 2025-03-01

java MultipartFile file upload and rename detailed code example

Preface

When uploading files, we need to consider the problem of overwriting the duplicate name. For the logical rigorous, we need to splice the UUID or timestamp in front of the file name to distinguish files with the same name, but therefore how to modify the file name passed to the backend.

Code example:

At this time, you need to add the file name UUID to this type of MultipartFile, but the front-end is not very convenient to handle, so the back-end processing method is as follows:

String fileSuffix = ();
//Rename the fileString fileName0 = (0,("."));
String fileName1 = ((".")+1);
//RenameString newName = uuid+"_"+fileName0;
//Temporary filesFile file=(newName , "."+fileName1);
(file);
InputStream inputStream = new FileInputStream(file);
//Convert File to MultipartFilemultipartFile = new MockMultipartFile((), (),(),inputStream);
//Delete temporary files after the program exits();

Notice:

File file=(newName , “.”+fileName1);

When creating a file, the file name (newName) cannot be less than two Chinese characters

Replenish:

1. The introduced dependency

import ;

2. Pom dependency

        <dependency>
            <groupId></groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

Summarize

This is the article about uploading and renaming of java MultipartFile multipartFile file. For more related contents of uploading and renaming of java MultipartFile multipartFile file, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!