[JAVA]文件上传
- 静态资源需要放到static文件下。
- form表单提交属性为enctype=”multipart/form-data”。
- 设置上传文件的默认大小值。
1.index.html
static文件下建立index.htnl页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
2.编写Contorller
package xyz.xioaxin12.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* @author 爱生活爱技术
*/
@RestController
public class FileUploadController {
@RequestMapping("/upload")
public String fileUpload(@RequestParam("fileupload") MultipartFile file) throws IOException {
System.out.println(file.getOriginalFilename());
//保存到本地g盘
file.transferTo(new File("g://"+file.getOriginalFilename()));
return "OK!";
}
}
3.启动类
package xyz.xioaxin12.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootUploadApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootUploadApplication.class, args);
}
}
关于文件大小
当上传的文件超出时,需要自己设置最大值。
application.properties设置如下配置
#设置单个上传文件的大小 spring.servlet.multipart.max-file-size=100MB #设置一次请求上传文件的总容量 spring.servlet.multipart.max-request-size=100MB
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xiaoxin1218@qq.com
文章标题:[JAVA]文件上传
文章字数:327
本文作者:周信
发布时间:2019-10-22, 22:40:28
最后更新:2023-05-03, 10:25:35
原始链接:http://zx21.xyz/2019/10/22/JAVA-文件上传/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。