这篇文章给大家分享的是有关SpringBoot多文件上传如何实现使用postman测试多文件上传接口的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。使用postman测试多文件上传接口1、创建测试类(FileCon
这篇文章给大家分享的是有关SpringBoot多文件上传如何实现使用postman测试多文件上传接口的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
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;@RestControllerpublic 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";}}
SpringBoot的默认上传文件的大小是1M,如果上传的文件超过了1M就会出现这样的错误
在application.properties配置文件中设置上传的文件大小限制,即可解决
# 上传文件总的最大值spring.servlet.multipart.max-request-size=10MB# 单个文件的最大值spring.servlet.multipart.max-file-size=10MB
感谢各位的阅读!关于“springboot多文件上传如何实现使用postman测试多文件上传接口”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
--结束END--
本文标题: springboot多文件上传如何实现使用postman测试多文件上传接口
本文链接: https://www.lsjlt.com/news/300179.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0