(393条消息) java 基于springBoot上传文件/文件夹使用实例_乐七_的博客-CSDN博客_springboot上传文件夹
控制层
@RequestMapping(value = "uploadBatch", method = RequestMethod.POST)
public Object uploadBatch(HelloVo helloVo) {
//获取所有文件
List<MultipartFile> files = helloVo.getFile();
//中间的其他处理过程
for (MultipartFile file : files) {
String name = "";
//取得当前上传文件的文件名称
name = file.getOriginalFilename();
String fileName = name == null ? "" : name.split("\\.")[0];
//校验上传文件后缀是否与所选择的文件类型相同
String fileSuffix = name == null ? "" : name.split("\\.")[1];
}
//。。。其他处理并返回
return null;
}
vo
package com.atguigu.yygh.cmn.controller;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HelloVo implements Serializable {
private List<MultipartFile> file;
private String readFileType;
private String relation;
}
配置类
package com.atguigu.yygh.cmn.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Configuration
public class MultipartConfiguration {
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(524288000);
multipartResolver.setMaxInMemorySize(524288000);
return multipartResolver;
}
}
前端页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form action="http://localhost:8082/admin/cmn/dict/uploadBatch" enctype="multipart/form-data" method="post">
<input type="hidden" name="type" value="1"/>
<input type="hidden" name="readFileType" value="jpg"/>
<input id="dir" type="file" name="file" webkitdirectory mozdirectory/>
<input id="uploadDir" type="submit" value="提交文件夹">
</form>
</body>
</html>
版权声明:本文为weixin_61503139原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。