Springboot文件上传功能简单测试
时间:2020-10-13 13:21:04|栏目:JAVA代码|点击: 次
在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";
}
}
选择文件

发送
找到该图片

在application配置文件中 可以配置 文件的大小和request请求的大小
#配置单个文件的大小
spring.servlet.multipart.max-file-size=5MB
#配置一次请求总容量大小
spring.servlet.multipart.max-request-size=10MB
栏 目:JAVA代码
下一篇:SpringCloud用Zookeeper搭建配置中心的方法
本文标题:Springboot文件上传功能简单测试
本文地址:http://www.codeinn.net/misctech/10749.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




