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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

知识追求者   2021-08-11 我要评论
想了解springboot多文件上传实现使用postman测试多文件上传接口的相关内容吗,知识追求者在本文为您仔细讲解postman测试多文件上传接口的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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测试多文件上传接口(选择多个文件)

在这里插入图片描述

3、查看项目路径

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

在这里插入图片描述

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

在这里插入图片描述

错误原因:

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

解决方法:

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

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

在这里插入图片描述

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们