File upload

Create a new project and add UploadController

@RestController

public class UploadController {

    @PostMapping("upload")
    public String post(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return "Upload failed, please select file";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "d:\\files\\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            return "Upload successful";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Upload failed!"; }}Copy the code

Then simulate sending the request on Postman as shown below:

Click Upload. Failure! What’s going on? Can we check the error message in the background

Under Caused by: Java. IO. FileNotFoundException: d: \ files \ electronic invoice. PDF (system can not find the specified path.Copy the code

It turned out that we could not find the file. There was no files folder in disk D, and the system did not create it automatically. There was an error. Ok, the program determines that if there is no specified folder, we will automatically create it, as shown below:

If you run postman again, return success, D has automatically created a files folder, selected to upload the file is already in the specified directory

Well, upload a file is OK, that business often upload multiple requirements, how to do, click many times?? NO!!!!!! And then we look down.

@PostMapping("uploads")

public String posts(@RequestParam("file") List<MultipartFile> files){// The file argument is received as a list array
    if (files.isEmpty()) {
        return "Upload failed, please select file";
    }

    // If the directory does not exist, it will be created automatically
    String filePath = "d:\\files\\";
    if (!new File(filePath).exists()) {
        new File(filePath).mkdirs();
    }

    // Walk through the files and save them to the server one by one
    for (MultipartFile file : files) {
        String fileName = file.getOriginalFilename();
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return "Upload failed!"; }}return "Upload successful";
}
Copy the code

Select multiple file uploads and click postman’s [Send] button to send the request.

For better maintenance, you can also add the file’s upload directory to the YAML configuration file.

Change the application.properties file to application.yaml (I prefer yamL format, please feel free). Then add the following to the file

fileRoot: d:\\abc\\
Copy the code

Note: there is a space after the colon

After postman requests it again, an ABC folder is automatically created on disk D, and the files are already stored in the new folder

We can also add the following configuration to the YAML file:

spring:
  servlet:
    multipart:
      max-request-size: 10MB Limit the total size of multiple files
      max-file-size: 1MB # Limit the maximum value of a single file
Copy the code

Postman uploads a file larger than 1MB and returns an error:

{
    "timestamp""The 2021-01-16 T02:32:01. 774 + 00:00"."status"500."error""Internal Server Error"."message"""."path""/upload"
}
Copy the code

More original Java reading: javawu.com