1
头图

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 "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "d:\\files\\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败!";
    }
}

Then postman , as shown in the figure below:

Click upload. failure! What's the matter, let's look at the error message in the background
Caused by: java.io.FileNotFoundException: d:\files\电子发票.pdf (系统找不到指定的路径。)

It turned out that the file could not be found. There was no files folder under our D drive, and the system did not automatically create it. An error was reported. Well, under the judgment of the program, if there is no specified folder, we will automatically create it, as shown below:

files folder has been automatically created under the D drive, and the files selected for upload are already in the specified directory

Okay, uploading a file is ok, but the business often requires multiple uploads at once, what should I do, click multiple times? ? ? NO! ! ! Then look down.

@PostMapping("uploads")

public String posts(@RequestParam("file") List<MultipartFile> files){//参数file以list数组接收
    if (files.isEmpty()) {
        return "上传失败,请选择文件";
    }

    //如果目录不存在,则自动创建
    String filePath = "d:\\files\\";
    if (!new File(filePath).exists()) {
        new File(filePath).mkdirs();
    }

    //遍历文件,一个个保存到服务器
    for (MultipartFile file : files) {
        String fileName = file.getOriginalFilename();
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败!";
        }
    }
    return "上传成功";
}

Select multiple files to upload, and click the [send] button of postman to send the request.

In order to facilitate maintenance, we can also add the file upload directory to the yaml configuration file.

application.properties file to application.yaml (I like the yaml format, please feel free to all customers). Then add the following in the file

fileRoot: d:\\abc\\
Note: there is a space after the colon

Another postman request, an abc folder is automatically created under the D drive, and the file has been stored in the new folder.

We can also add the following configuration in the yaml file:

spring:
  servlet:
    multipart:
      max-request-size: 10MB #限制多个文件总大小
      max-file-size: 1MB #限制单个文件最大值

Postman uploads a file that exceeds 1mb and returns an error:

{
    "timestamp": "2021-01-16T02:32:01.774+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "message": "",
    "path": "/upload"
}
More java original reading: https://javawu.com

大盛玩java
24 声望5 粉丝