java 基于springBoot上传文件/文件夹使用实例

最近项目中遇到一个文件批量上传的需求,对单个的文件、多文件或者文件夹的方式上传文件都可以满足要求,总结一下使用经验。
案例基于springBoot.
1、文件上传请求
这里postman测试了单文件和多文件的上传,同时测试了文件夹方式上传。
postman中可以选择单文件或者Ctrl键选择多文件上传。如下图:
在这里插入图片描述
简单html实现文件夹方式的文件上传示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
</head>
<body>
    <h1>文件上传</h1>
    <form action="http://localhost:8888/hello" 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>

2、编写配置类,springboot默认使用的是StandardServletMultipartResolver来处理Multipart,对应的使用StandardMultipartFile来接收文件数据,但是StandardMultipartFile获取的文件名如果是中文的话,总是乱码。
这里使用CommonMultiPart。

@Configuration
public class MultipartConfiguration {

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver getCommonsMultipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(524288000);
        multipartResolver.setMaxInMemorySize(524288000);
        return multipartResolver;
    }
}

并且在启动类上排除自动配置MultipartAutoConfiguration

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})

3、控制层

@Api(value = "数据接入", tags = "数据导入")
@RestController
@RequestMapping("/hello 这里写自己的请求")
public class OfflineDataController {
@ApiOperation(value = "数据批量上传")
    @AopLog(title = "数据批量上传")
    @RequestMapping(value = "uploadBatch", method = RequestMethod.POST)
    public Object uploadBatch(HelloVo helloVo) {
        ResultMap resultMap = offlineDataService.uploadBatch(helloVo);
        return resultMap;
    }
}

4、数据传输对象,其实就只要把file定义为一个集合就可以了,这样上传的所有文件都会封装到这里。

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("com.sjg.HelloVo")
public class HelloVo implements Serializable {

    @ApiModelProperty("导入多个文件")
    private List<MultipartFile> file;

    @ApiModelProperty("read文件类型")
    private String readFileType;

    @ApiModelProperty("数据相关性")
    private String relation;
}

5、服务层实现类
不需要对文件上传的绝对路径进行递归处理,直接就获取文件名称,使用上传等处理即可。干就完了!

@Service
public class OfflineDataServiceImpl implements OfflineDataService {
	@Override
    public ResultMap 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 ResultMap.ok("导入成功!");
	}
}

可能的问题:上传文件有大小限制,在application.yml中配置

spring: 
  servlet: 
    multipart: 
      # 设置单个文件的大小
      max-file-size: 100MB
      # 总上传文件大小
      max-request-size: 500MB

选择文件夹上传文件即可
在这里插入图片描述
不足之处,各位大佬评论指出,感谢!

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值