springMVC实现 MultipartFile 多文件上传
需要的jar包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
配置文件
#文件单个限制大小
spring.http.multipart.max-file-size=30MB
spring.http.multipart.max-request-size=300MB
前端上传下载代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<h2>文件上传示例</h2>
<hr/>
<form method="POST" enctype="multipart/form-data" action="http://127.0.0.1:8080/upload/fileupload">
<p>
文件:<input type="file" name="file" />
</p>
<p>
<input type="submit" value="上传" />
</p>
</form>
<input type="button" value="下载文件" onclick="doDownload()"/>
<form id="download" method="get" action="http://127.0.0.1:8080/upload/filedowload">
</form>
<script type="text/javascript">
function doUpload() {
var upl = document.getElementById("upload");
upl.submit();
}
function doDownload() {
var upl = document.getElementById("download");
upl.submit();
}
</script>
</body>
</html>
controller层
@Controller
@RequestMapping("/file")
public class JadFileController {
@PostMapping("/fileupload")
public Object upload(MultipartFile file) throws IOException {
String path = "E:/upload/";
if (!file.isEmpty()) {
File filepath = new File(path);
if (!filepath.exists()) {
filepath.mkdir();
}
String savePath=path+file.getOriginalFilename();
file.transferTo(new File(savePath));
return ResultResponseUtils.resultSuccess(true);
}
return ResultResponseUtils.resultFail("上传失败");
}
}
结果展示