温馨提示×

温馨提示×

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

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

如何测试Springboot文件上传功能

发布时间:2020-07-28 14:01:37 来源:亿速云 阅读:139 作者:小猪 栏目:编程语言

这篇文章主要讲解了如何测试Springboot文件上传功能,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

在static文件夹中创html页面

内容为:

<html>
<head></head>
<body>
<form action="/fileuploadContorller" method="post" enctype="multipart/form-data">
  <input type="file" name="file"/>
  <input type="submit" value="提交">
</form>
</body>
</html>

创建控制器

package com.mc_74120.springbootfileupload.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
public class FileUpLoadController {
  @PostMapping("/fileuploadContorller")
  public String fileUpLoadController(MultipartFile file) throws IOException {
//MultipartFile对象的名称必须和html中的文件上传标签的名字相同
    System.out.println(file.getOriginalFilename());
    file.transferTo(new File("d:/"+file.getOriginalFilename()));
    return "ok";
  }
}

选择文件

如何测试Springboot文件上传功能

发送

找到该图片

如何测试Springboot文件上传功能

在application配置文件中 可以配置 文件的大小和request请求的大小

#配置单个文件的大小
spring.servlet.multipart.max-file-size=5MB
#配置一次请求总容量大小
spring.servlet.multipart.max-request-size=10MB

看完上述内容,是不是对如何测试Springboot文件上传功能有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

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

AI