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

发布时间:2021-08-11 13:55:33 作者:小新
来源:亿速云 阅读:444

这篇文章给大家分享的是有关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测试多文件上传接口

感谢各位的阅读!关于“springboot多文件上传如何实现使用postman测试多文件上传接口”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. postman进行接口测试
  2. springboot实现多文件上传功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot postman

上一篇:android中怎么自定义手表效果

下一篇:JavaScript如何定位当前的地理位置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》