springboot多文件上传,使用postman测试多文件上传接口

1、创建测试类(FileController.java)

package com.jeff.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileController {

	@PostMapping("/upload")
	public String upload(@RequestParam("files") List<MultipartFile> files) {
		if (files.isEmpty()) {
			return "上传失败,未选择文件";
		}
		for (MultipartFile file : files) {
			String fileName = file.getOriginalFilename();
			// 获取文件后缀名
			String suffixName = fileName.substring(fileName.lastIndexOf("."));
			// 重新生成文件名
			String fName = System.currentTimeMillis() + suffixName;
			System.out.println("文件名:" + fName);
			String filePath = "F:\\Jeff\\project\\workspace\\mavenDemo\\src\\main\\resources\\static\\";
			File dest = new File(filePath + fName);
			try {
				file.transferTo(dest);
				System.out.println(fName + "上传成功!");
			} catch (IOException e) {
				System.out.println(fName + "上传异常!" + e);
				return "error";
			}
		}
		return "success";
	}

}

2、使用postman测试多文件上传接口(选择多个文件)
springboot多文件上传,使用postman测试多文件上传接口_第1张图片
3、查看项目路径
springboot多文件上传,使用postman测试多文件上传接口_第2张图片
4、如果报下图错误,请查看 解决方法
springboot多文件上传,使用postman测试多文件上传接口_第3张图片

你可能感兴趣的:(SpringBoot,Postman)