服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot多文件上传实现使用postman测试多文件上传接口

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

2021-11-14 12:09知识追求者 Java教程

这篇文章主要介绍了springboot多文件上传实现使用postman测试多文件上传接口,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用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测试多文件上传接口

3、查看项目路径

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

4、如果报下图错误,请查看 解决方法

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

解决方法:The field files exceeds its maximum permitted size of 1048576 bytes

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

错误原因:

SpringBoot的默认上传文件的大小是1M,如果上传的文件超过了1M就会出现这样的错误

解决方法:

在application.properties配置文件中设置上传的文件大小限制,即可解决

# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_45739720/article/details/103724064

延伸 · 阅读

精彩推荐